go-intl is a high-performance internationalization library with a simpler API. It should be the definitive answer to internationalization in Go.
I decided to write go-intl while I was building the terminal and desktop clients for a personal agent entirely in Go. When I needed to add internationalization, I took one look at go-i18n’s API and knew it wasn’t what I wanted. I wanted something simpler, so I decided to build a new library with better performance and a simpler API.
If you find this article useful, give go-intl a star ✨. If you have any questions, feel free to open an issue.
go-intl
A high-performance Go internationalization library with a simple API for sharing translations across terminal, desktop, and server applications.
go-intl is all gophers need
English | 简体中文
Pick a language once, then translate by key.
t := catalog.Translations("en") t("Inbox.title") // "Inbox" A Bubble Tea model, a MyGo window, an NSGo view, a Wails app’s local Go backend, and an HTTP handler can each select a language from the same immutable catalog.
Performance
Apple M3 Pro, Go 1.27.1 (darwin/arm64). The ten interpolation cases use medians from the six-run v0.1.1 comparison. The two static lookups and the English plural retain the original three-run medians. Each cell reports ns/op · B/op · allocs/op. Speedup is go-i18n time divided by go-intl time.
v0.1.1 benchmark results · Original benchmarks and methodology
Get started
The core package requires Go 1.25 or later. To try the example in…
Here are the full benchmark results. Overall, go-intl beats go-i18n by a wide margin on translation speed, bytes allocated, and number of allocations.
| Case | go-intl ns/op | go-intl B/op | go-intl allocs/op | go-i18n ns/op | go-i18n B/op | go-i18n allocs/op | go-intl speedup |
|---|---|---|---|---|---|---|---|
One argument, {first} | 27.54 | 8 | 1 | 284.60 | 275 | 7 | 10.33× |
Greeting, Hello, {first}! | 52.34 | 24 | 2 | 300.70 | 288 | 7 | 5.75× |
Integer, Item {id}. with id = 42 | 88.86 | 24 | 3 | 307.60 | 280 | 7 | 3.46× |
Space-separated arguments, {first} {last} | 70.28 | 24 | 2 | 420.15 | 320 | 9 | 5.98× |
Adjacent arguments, {first}{last} | 68.54 | 24 | 2 | 410.20 | 320 | 9 | 5.98× |
| 4 distinct arguments | 97.93 | 24 | 2 | 670.65 | 384 | 13 | 6.85× |
| 8 distinct arguments | 156.70 | 40 | 2 | 1,152.50 | 528 | 21 | 7.35× |
| 16 distinct arguments | 257.10 | 136 | 3 | 2,015.50 | 816 | 37 | 7.84× |
| 32 distinct arguments | 470.55 | 264 | 3 | 3,877.00 | 1,520 | 70 | 8.24× |
| One argument repeated 16 times | 265.55 | 136 | 3 | 2,004.00 | 816 | 37 | 7.55× |
| Repeated lookup of one static key | 12.9 | 0 | 0 | 116.8 | 0 | 0 | 9.05× |
| Cycling through 1,024 static keys | 15.8 | 0 | 0 | 125.8 | 0 | 0 | 7.96× |
English plural, count = 7 | 81.2 | 16 | 2 | 336.5 | 344 | 8 | 4.14× |
Measured on an Apple M3 Pro with Go 1.27.1 (darwin/arm64).
Here’s a minimal example.
package main import ( "fmt" "log" "github.com/waterkyuu/go-intl" ) func main() { catalog, err := intl.New("en", intl.Define("en", map[string]string{ "Inbox.title": "Inbox", "Inbox.hello": "Hello, {name}!", "Inbox.count": "{n, plural, =0 {No unread messages} one {# unread message} other {# unread messages}}", "Inbox.state": "{online, select, true {Online} other {Offline}}", }), intl.Define("fr", map[string]string{ "Inbox.title": "Boîte de réception", }), ) if err != nil { log.Fatal(err) } t := catalog.Translations("en", "Inbox") fmt.Println(t("title")) // Inbox fmt.Println(t("hello", intl.Values{"name": "Ada"})) // Hello, Ada! fmt.Println(t("count", intl.Values{"n": 2})) // 2 unread messages fmt.Println(t("state", intl.Values{"online": true})) // Online } go-intl supports five sources for translations.
| Source | Definition |
|---|---|
| Go | intl.Define("en", map[string]string{"Inbox.title": "Inbox"}) |
| JSON | {"Inbox": {"title": "Inbox"}} |
| TOML | Inbox.title = "Inbox" |
| YAML | Inbox: {title: Inbox} |
| CSV | Header row key,message, followed by Inbox.title,Inbox |
go-intl supports the following message syntax.
| Feature | Example |
|---|---|
| Named argument | Hello, {name} |
| Boolean or enum selection | {role, select, admin {Manage} other {View}} |
| Cardinal plural | {n, plural, =0 {Empty} one {# item} other {# items}} |
| Ordinal | {n, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} |
| Plural offset | {n, plural, offset:1 =1 {Just you} one {You and one guest} other {You and # guests}} |
| Numbers | {price, number}, {n, number, integer}, {share, number, percent} |
| Dates and times | Pass a Go time.Time to {when, date, long} or {when, time, short} |
| Rich text | Use Read <strong>{title}</strong> with intl.Values{"title": "Inbox", "strong": func(s string) string { return "**" + s + "**" }} |
| Escaping | '{name}' produces the literal {name}, including the braces. '' produces one apostrophe. |
You can use go-intl with Bubble Tea, in a local Wails backend, in a Go web backend, and with mygo or nsgo. That covers terminal apps, desktop apps, and backend services.
By the way, Go is the best language for the AI era. It runs fast, compiles fast, and gives AI fast feedback. You should use Go. Go is all you need.
Let’s look at the thinking behind the design.
Overview
Imagine a page with forty pieces of text to translate. Three of them are a status label, a greeting, and the number of items in a cart.
Ready Hello, Ada! 7 items All three are strings, but each is produced differently. Ready is already written out in the translation file. Hello, Ada! needs interpolation to fill in the user’s name. And 7 items depends on the quantity to determine whether to use item or items.
Walking through how these three strings are produced shows where go-intl saves time.
Ready — Do forty labels need forty language matches?
Find English, then find Ready
Suppose an app provides English and Simplified Chinese translations. The same key gives different text in each language.
| Message key | English (en) | Simplified Chinese (zh-CN) |
|---|---|---|
screen.ready | Ready | 就绪 |
screen.cancel | Cancel | 取消 |
From here on, a dictionary means one set of translations. It holds the message keys and their translations for a given language. Look up screen.ready in the English dictionary and you get Ready. Look it up in the Chinese dictionary and you get 就绪.
Before you can look up a key, you need to know which dictionary to open.
Say the browser sends this Accept-Language header.
Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8 The browser prefers American English, followed by English, then Simplified Chinese. The app has no separate en-US dictionary, but it does provide en, so it can select the English dictionary.
This is language matching. The app compares the languages the user prefers with the ones it offers and picks a suitable match.
With English selected, screen.ready resolves to Ready.
go-i18n saves the preferences and matches on each call
With both dictionaries loaded into a go-i18n bundle, a caller can translate two labels like this.
header := "en-US,en;q=0.9,zh-CN;q=0.8" loc := i18n.NewLocalizer(bundle, header) ready := loc.MustLocalize(&i18n.LocalizeConfig{MessageID: "screen.ready"}) cancel := loc.MustLocalize(&i18n.LocalizeConfig{MessageID: "screen.cancel"}) NewLocalizer reads those language preferences. Here’s its implementation.
func NewLocalizer(bundle *Bundle, langs ...string) *Localizer { return &Localizer{ bundle: bundle, tags: parseTags(langs), } } parseTags(langs) parses the input into language tags and stores them in tags. At this point, what the Localizer holds is the browser’s list of preferences.
The quality weights (q=0.9 and q=0.8) determine the order. For this header, the localizer holds the following preferences.
[]language.Tag{ language.MustParse("en-US"), language.MustParse("en"), language.MustParse("zh-CN"), } Dictionary selection happens later. Each call to Localize or MustLocalize reaches getMessageTemplate, which starts with these three lines.
_, i, _ := l.bundle.matcher.Match(l.tags...) // i indexes the matched language in the bundle's list of supported tags. tag := l.bundle.tags[i] // Read the language tag the bundle actually supports. mt := l.bundle.getMessageTemplate(tag, id) // Look up this message ID under that tag. The first line matches the preferences, the second retrieves the supported tag, and the third looks up the message for that language.
Suppose bundle.tags is [zh-CN, en]. When we look up screen.ready, the matcher returns i = 1. We read bundle.tags[1], which is en, then look up screen.ready in the English messages. When we look up screen.cancel, loc.tags still holds the same three tags. The matcher runs again, returns i = 1 again, and we look up screen.cancel in English. The calls share the browser’s preference list. Each call works out which language to use.
That’s two language matches for two labels. Translate all forty labels with this localizer and the matcher runs forty times.
go-intl lets the whole page share one language match
Within this request, the browser’s preferences stay the same and so do the dictionaries. Both lookups will pick English.
go-intl lets the app make that choice at the start of the request and reuse it for later translations. Suppose catalog already contains our two dictionaries and English is the default.
header := "en-US,en;q=0.9,zh-CN;q=0.8" locale := catalog.Match(header) t := locale.Translations() ready := t("screen.ready") cancel := t("screen.cancel") catalog.Match(header) picks English and returns a locale that remembers the choice. The t created from that locale uses the English dictionary directly on later calls, without processing the browser’s preferences again.
That’s what it means to bind a dictionary. The translator remembers which dictionary to look in on subsequent calls. It only needs a reference to the existing data, so there’s no need to copy the English translations.
A language may be missing a message. If Chinese is selected and has no entry for screen.cancel, the lookup should try the default English dictionary next. go-intl builds that fallback order when it loads the catalog. Locale.chain holds the resulting sequence of dictionary indexes.
It takes just a few lines in catalog.go to store the result of that choice.
type Locale struct { catalog *Catalog // All the loaded dictionaries live in this catalog. chain []int // Dictionary indexes in lookup order, preferred language first. prefix string // Current namespace, e.g. "screen". The root uses "". direct map[string]int // Short key to message index in the preferred dictionary's programs. } func (c *Catalog) bind(idx int) Locale { return Locale{catalog: c, chain: c.locales[idx].chain} } Here’s an example of what locale contains after catalog.Match returns.
locale := catalog.Match("en-US,en;q=0.9,zh-CN;q=0.8") // The resulting locale has these field values. Locale{ catalog: catalog, // The same *Catalog we called Match on. chain: []int{0}, // Look in the English dictionary at index 0. prefix: "", // At the root, callers use full keys such as "screen.ready". direct: nil, // Scope("screen") would bind the short-key index. } A lookup starts with the first dictionary in that sequence. Here’s the part of lookup that handles a hit there.
first := &l.catalog.locales[l.chain[0]] // The full-key index is a map[string]int. keys := first.keys if l.prefix != "" { keys = l.direct } if idx, found := keys[key]; found { return &first.programs[idx], first } keys is a Go map from message keys to integer indexes. programs[idx] holds the message at that index. For Ready, the stored text is already the finished string, so it can be returned directly.
map[string]int{ "screen.ready": 0, "screen.cancel": 1, } If the page’s keys all start with screen., you can leave that prefix out of individual calls.
t := locale.Translations("screen") ready := t("ready") cancel := t("cancel") screen is a namespace, a prefix shared by a group of messages. It has its own short-key index, so the translator can look up ready directly without constructing screen.ready first.
At load time, go-intl splits each key at its last dot. For screen.ready, that gives us the namespace screen and the short key ready. If this message is at index 7, both entries point to the same compiled message.
keys["screen.ready"] = 7 // Full-key lookup. namespaces["screen"]["ready"] = 7 // Short-key lookup within screen. At translation time, the scoped translator uses the index stored in Locale.direct.
t := locale.Translations("screen") // Bind the short-key index for screen. ready := t("ready") // direct["ready"] gives 7, so read programs[7]. cancel := t("cancel") // Use the same index for direct["cancel"]. The short-key index gives you a simpler way to call the translator, with no performance gain. A translator that takes full keys can reuse the selected language just as well. The static benchmarks below use full keys.
Figure 1. All the translations on a page can share one language match.
Hello, Ada! — What’s left to do once we’ve found the message?
Parsing, caching, and executing a template are three separate steps
Once you’ve found Ready, you can display it directly. Once you’ve found Hello, {first}!, you still need to replace {first} with the name.
A message with a slot for a value is a template. go-i18n uses Go’s text/template syntax, so its version of the greeting is Hello, {{.first}}!.
Three things happen in sequence.
- Parse the message to identify literal text and references to arguments.
- Cache that structure so later calls can reuse it.
- Execute it with the arguments for this call to produce the string the user sees.
Change Ada to Grace and the structure stays the same. The greeting still has a prefix, a name, and an exclamation mark. Only the output needs to be built again.
In go-i18n, TextParser.Parse in i18n/template/text_parser.go creates the template. At this point the delimiters and options are already in place.
tmpl, err := template.New("").Delims(leftDelim, rightDelim).Option(option).Funcs(te.Funcs).Parse(src) if err != nil { return nil, err } return &parsedTextTemplate{tmpl: tmpl}, nil .Parse(src) parses Hello, {{.first}}! into a Go template, which go-i18n wraps in a parsedTextTemplate.
Plain text gets a shorter path. Before creating a Go template, the parser looks for the opening delimiter, which defaults to {{.
if !strings.Contains(src, leftDelim) { // No opening delimiter. Keep the original text. return &identityParsedTemplate{src: src}, nil } Ready contains no {{, so the parser keeps the original string and skips the Go template parser.
The cache lives in internal/template.go. The default parser allows caching when there are no custom template functions. Here’s Template.Execute.
func (t *Template) Execute(parser template.Parser, data interface{}) (string, error) { var pt template.ParsedTemplate var err error if parser.Cacheable() { t.parseOnce.Do(func() { t.parsedTemplate, t.parseError = parser.Parse(t.Src, t.LeftDelim, t.RightDelim) }) pt, err = t.parsedTemplate, t.parseError } else { pt, err = parser.Parse(t.Src, t.LeftDelim, t.RightDelim) } if err != nil { return "", err } return pt.Execute(data) } On the first call for this message, parseOnce.Do runs parser.Parse and stores the result in t.parsedTemplate. Later calls pick up that result and pass the current data to pt.Execute(data).
When go-i18n displays Ready again, it can reuse the cached result. When it displays the greeting again, it reuses the parsed template as well. The difference in static lookup times comes from the steps that lead up to finding the message, including language selection and the lookup itself.
go-intl records the concatenation order at load time
In go-intl v0.1.0, greetings such as Hello, {first}! were already parsed when the dictionary was loaded. Each time the greeting was displayed, the general-purpose renderer still had to loop over the prefix, the name, and the exclamation mark in order. v0.1.1 built on that implementation to speed up interpolation. Since the compiler already knows where the name goes, it stores the argument name along with the concatenation order.
We tested both versions and go-i18n with the same three inputs on an Apple M3 Pro running Go 1.27.1. We prepared the argument map ahead of time and took the median of six runs for each case. Each cell gives nanoseconds per call · heap allocations per call.
| Message | go-intl v0.1.0 | go-intl v0.1.1 | go-i18n |
|---|---|---|---|
{first} | 47.7 · 2 | 28.6 · 1 | 345.9 · 7 |
Hello, {first}! | 80.1 · 3 | 54.4 · 2 | 363.6 · 7 |
16 distinct placeholders separated by / | 449.4 · 5 | 276.0 · 3 | 2,274.5 · 37 |
Take the greeting in the second row. Filling in the same name went from 80.1 ns to 54.4 ns, and from three heap allocations to two. To see what changed, look at what each version keeps in memory for that greeting.
Suppose the English dictionary has just one message. Both versions load it the same way.
catalog, err := intl.New("en", intl.Define("en", map[string]string{ "screen.hello": "Hello, {first}!", })) if err != nil { return err } At load time, compileMessage parses "Hello, {first}!". It’s the only message, so keys["screen.hello"] is 0 and the compiled result goes in programs[0]. We already know the argument is called first. We’ll get the actual name when someone calls the translator.
After loading in v0.1.0, programs[0] holds three instructions
An instruction is a Go struct. Its kind tells the renderer how to interpret text. With opText, it writes the text as given. With opValue, it uses the text as an argument name.
type instruction struct { text string // Literal text or argument name. end int // End of a branch or block. Unused in this greeting. kind messageOp // Operation for the renderer to execute. } // Contents of programs[0] after loading. compiledMessage{ raw: "Hello, {first}!", code: []instruction{ {kind: opText, text: "Hello, ", end: 0}, {kind: opValue, text: "first", end: 0}, {kind: opText, text: "!", end: 0}, }, } Now bind t := catalog.Translations("en") and call t("screen.hello", intl.Values{"first": "Ada"}). The first instruction writes "Hello, " to a strings.Builder. The second looks up "first" and writes "Ada" to the same builder. The third writes "!". Pass a different name and the renderer walks the same three instructions again.
If we spell out the successful path through (*messageRender).body in message_render.go, it amounts to this.
var b strings.Builder b.WriteString("Hello, ") // First instruction, opText. value := values["first"] // Second instruction, opValue. The lookup gives us "Ada". b.WriteString(messageString(value)) // Write "Ada" to the same builder. b.WriteString("!"...

