RapidGo
No results found

Localization

Multi-language support with JSON translation files and template interpolation.

The i18n package provides translation support with JSON files, fallback locales, and template-based interpolation.

Setup

Create JSON translation files in resources/lang/:

// resources/lang/en.json
{
    "welcome": "Welcome, {{.Name}}!",
    "goodbye": "Goodbye!",
    "items_count": "You have {{.Count}} items."
}
// resources/lang/es.json
{
    "welcome": "¡Bienvenido, {{.Name}}!",
    "goodbye": "¡Adiós!",
    "items_count": "Tienes {{.Count}} artículos."
}

Creating a Translator

import "github.com/RAiWorks/RapidGo/v2/core/i18n"

// Create with fallback locale
t := i18n.NewTranslator("en")

// Load a single file
t.LoadFile("en", "resources/lang/en.json")

// Load all files from a directory (filename = locale)
t.LoadDir("resources/lang/")

Translating Messages

// Simple translation
msg := t.Get("en", "goodbye")  // "Goodbye!"

// With interpolation (uses text/template)
msg := t.Get("es", "welcome", map[string]string{"Name": "Rajesh"})
// "¡Bienvenido, Rajesh!"

// Fallback to default locale
msg := t.Get("fr", "goodbye")  // "Goodbye!" (falls back to "en")

// Missing key returns the key itself
msg := t.Get("en", "unknown_key")  // "unknown_key"

Translation File Format

  • One JSON file per locale
  • File name (without extension) is the locale code: en.json, es.json, fr.json
  • Flat key-value pairs (no nesting)
  • Values support Go text/template syntax for interpolation