Why does your Go application need internationalization (i18n)?
\n\nYou wrote a cool service in Go. It's fast, concurrent, compiles in seconds. But there's one problem: all messages, buttons, and tooltips in it are in Russian (or English). And the world wants to use your product in their native language. This is where internationalization of Go applications (i18n) comes into play.
\n\nMany people think that Go localization is a huge pain with JSON files and workarounds. But that's not the case. The Go ecosystem offers elegant solutions that allow you to add support for any number of languages without bloating the code or losing performance. In this article, we'll look at how to properly implement i18n in Go so that your application can speak a hundred languages.
\n\nWhat are i18n and l10n? Understanding the Terminology
\n\nBefore writing code, let's clarify the terms:
\n\n- \n
- i18n (Internationalization) — the process of designing an application so that it can be adapted to different languages and regions without rewriting the code. It's about architecture. \n
- l10n (Localization) — the adaptation of content (text translation, date and currency formats) to a specific locale (e.g., en-US, ru-RU, de-DE). \n
In the context of internationalization of Go applications, i18n is preparing the code (using keys instead of strings), and l10n is filling those keys with translations.
\n\nChoosing a Library: Why go-i18n?
\n\nThere are several popular libraries for working with translations in Go. After testing various options, the best choice for most projects is go-i18n by nicksnyder. Why?
\n\n- \n
- Ease of use — minimal boilerplate code. \n
- Pluralization support — correct declension for "1 message", "2 messages", "5 messages". \n
- TOML/YAML/JSON support — choose the translation format you prefer. \n
- Template support — you can insert variables directly into the translation string. \n
- Built-in key extraction utility — no need to write translations manually. \n
Practical Example: Building a Multilingual Service in Go
\n\nLet's create a simple web server in Go that greets the user in their language.
\n\nStep 1: Installation and Project Structure
\n\nInstall the library and create the directory structure:
\n\ngo get github.com/nicksnyder/go-i18n/v2/i18n\ngo get golang.org/x/text/language\n\nProject structure:
\n\nmyapp/\n├── main.go\n└── locales/\n ├── active.en.toml\n ├── active.ru.toml\n └── active.de.toml\n\nStep 2: Create Translation Files
\n\nlocales/active.en.toml:
\n\n[greeting]\ndescription = "User greeting"\none = "Hello, {{.Name}}!"\nother = "Hello, {{.Name}}!"\n\n[inbox]\ndescription = "Number of messages in the inbox"\none = "You have {{.Count}} new message."\nother = "You have {{.Count}} new messages."\n\nlocales/active.ru.toml:
\n\n[greeting]\ndescription = "User greeting"\none = "Привет, {{.Name}}!"\nother = "Привет, {{.Name}}!"\n\n[inbox]\ndescription = "Number of messages in the inbox"\none = "У вас {{.Count}} новое сообщение."\nfew = "У вас {{.Count}} новых сообщения."\nmany = "У вас {{.Count}} новых сообщений."\nother = "У вас {{.Count}} сообщений."\n\nNote the plural forms in Russian: one, few, many, other. Go pluralization is a powerful feature that correctly handles complex declension rules.
\n\nStep 3: Write the Go Code
\n\nNow for the most interesting part — internati