This repository handles message translations using simple JSON files for Go (Golang) projects. The translations can be specific to a language or a combination of language and region.
Users can use the msgextractor tool to extract messages from your go source files. The messages will be added/removed to/from your translation files.
Note: Only messages using direct strings will be extracted. Messages containing variables will not be extracted. The msgextractor will remove manually added translations if they use variables. To avoid this, use direct strings in your messages.
msgextractor --translations path_to_translation_files --src path_to_go_source_files
// Use --default-language to set the default language as source for the translations. Missing translations for other languages will use this as the source.
msgextractor --translations path_to_translation_files --src path_to_go_source_files --default-language en
// Create a new translator.
tr, err := messages.FromDir("dir")
if err != nil {
// Handle error
log.Fatalf("Failed to create translator: %v", err)
}
// Setting the language context per request.
// Parse the language from a user request. This can be from a header or user settings, for example.
ctx := context.Background()
ctx, err := messages.WithLanguage(ctx, "en")
if err != nil {
// Handle error
log.Fatalf("Failed to set language: %v", err)
}
// Translate the message.
// If the user requested en-US but you only have en translations available, the translator will use the en translations.
msg := tr.Translate(ctx, "welcome.message", map[string]any{"user": "wvell"})
fmt.Println(msg) // prints: Welcome wvell!
Translation files are named based on the language or language-region codes:
- en.json for English
- en_US.json for American English
- es.json for Spanish
Each translation file follows this format:
{
"welcome.message": "Welcome :user!"
}
You can use a capitalized replacement to to capitalize the replacement value:
{
"welcome.message": "Welcome :User!"
}
This will change the replacement value john to John.
Transformers allow you to replace placeholder values, which is particularly useful for validation messages.
Using transformers allows you to reuse translations. The following example illutrates the required validation. Without transformers you would have to create a translation for each field(required.first_name, required.street).
es.json
{
"required" : ":Field es requerido"
"@transform" : {
"field" : {
"first_name": "nombre",
"street": "calle"
}
}
}
tr.Translate(ctx, "required", map[string]any{"field": "first_name"}) // Nombre es requerido
tr.Translate(ctx, "required", map[string]any{"field": "street"}) // Calle es requerido
As you can see this also takes the title case for the translation message into account.