Internationalization of Go Applications: From $ to € Without a Headache

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why does your Go application need internationalization (i18n)?

\n\n

You 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\n

Many 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\n

What are i18n and l10n? Understanding the Terminology

\n\n

Before 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
\n\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\n

Choosing a Library: Why go-i18n?

\n\n

There 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
\n\n

Practical Example: Building a Multilingual Service in Go

\n\n

Let's create a simple web server in Go that greets the user in their language.

\n\n

Step 1: Installation and Project Structure

\n\n

Install the library and create the directory structure:

\n\n
go get github.com/nicksnyder/go-i18n/v2/i18n\ngo get golang.org/x/text/language
\n\n

Project structure:

\n\n
myapp/\n├── main.go\n└── locales/\n    ├── active.en.toml\n    ├── active.ru.toml\n    └── active.de.toml
\n\n

Step 2: Create Translation Files

\n\n

locales/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\n

locales/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\n

Note the plural forms in Russian: one, few, many, other. Go pluralization is a powerful feature that correctly handles complex declension rules.

\n\n

Step 3: Write the Go Code

\n\n

Now for the most interesting part — internati

Blogs

Book Recommendations