Internationalization (i18n) in Go (Golang) with the go-i18n library
Internationalization (i18n) is the process of designing software so that it can be easily adapted for different languages and regions without changes to the source code. In the Go language (Golang), one of the most popular and powerful libraries for implementing i18n is go-i18n. This library provides developers with convenient tools for managing translations, supporting pluralization, and working with context.
In this article, we will take a detailed look at how to use go-i18n to internationalize your Go application: from installation to creating a full-fledged localization system.
Installing the go-i18n library
To get started with go-i18n, you need to install the package using the Go dependency manager:
go get github.com/nicksnyder/go-i18n/v2/i18nYou will also need a package for parsing translation files (e.g., JSON or YAML):
go get github.com/nicksnyder/go-i18n/v2/i18n/bundlego get github.com/nicksnyder/go-i18n/v2/i18n/localizerLocalization file structure
The go-i18n library supports storing translations in JSON, YAML, or TOML formats. It is recommended to create separate files for each language. Example directory structure:
locales/ active.en.json # English active.ru.json # Russian active.fr.json # FrenchExample translation file (active.en.json)
Let's create a file for English with one message and support for plural forms:
{ "hello_world": { "other": "Hello, World!" }, "messages_count": { "one": "You have {{.Count}} new message.", "few": "You have {{.Count}} new messages.", "many": "You have {{.Count}} new messages.", "other": "You have {{.Count}} new messages." }}Example translation file (active.ru.json)
A similar file for Russian, taking into account plural rules (one, few, many):
{ "hello_world": { "other": "Привет, мир!" }, "messages_count": { "one": "У вас {{.Count}} новое сообщение.", "few": "У вас {{.Count}} новых сообщения.", "many": "У вас {{.Count}} новых сообщений.", "other": "У вас {{.Count}} сообщений." }}Initialization and usage in code
Now let's write Go code that loads translation files and uses them to display messages depending on the selected language.
package main
import ( "fmt" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language")
func main() { // Create a bundle (translation storage) bundle := i18n.NewBundle(language.English) bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
// Load translation files bundle.MustLoadMessageFile("locales/active.en.json") bundle.MustLoadMessageFile("locales/active.ru.json")
// Create a localizer for Russian localizer := i18n.NewLocalizer(bundle, language.Russian.String())
// Example usage with a simple message helloMsg, _ := localizer.Localize(&i18n.LocalizeConfig{ MessageID: "hello_world", }) fmt.Println(helloMsg) // Output: Привет, мир!
// Example usage with plural forms for _, count := range []int{1, 2, 5, 10} { msg, _ := localizer.Localize(&i18n.LocalizeConfig{ MessageID: "messages_count", PluralCount: count, TemplateData: map[string]interface{}{ "Count": count, }, }) fmt.Println(msg) }}Pluralization support
One of the key features of go-i18n is the correct handling of plural forms. The library uses Unicode
Also in library
Quartz.NET for C# (.NET): Complete Guide with Examples
CSVImporter: Working with CSV in Swift
Database Migrations in Node.js: Tool Overview and Examples