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 Fyne or 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.
Use the go-intl CLI to extract, check and merge translation catalogs.
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
The design behind go-intl
Get started
The core package requires Go 1.25 or later. To try the example in a new module, run the commands below. In an existing Go module, run just the go get command.
mkdir intl-demo
cd intl-demo
go mod init example.com/intl-demo
go get github.com/waterkyuu/go-intl
Save this program as main.go in the demo module and run go run .. The import path ends in go-intl and the package name is intl.
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
}
To omit the namespace, pass the full key. This example uses the same catalog:
fr := catalog.Translations("fr")
fmt.Println(fr("Inbox.title")) // Boîte de réception
fmt.Println(fr("Inbox.count", intl.Values{"n": 2})) // 2 unread messages
The first argument to intl.New sets the fallback language. Here, "en" supplies the missing French translation for Inbox.count. To change the fallback, pass the desired language tag and include a source for that language.
Translation files
Put native definitions in ordinary .go files with intl.Define. The function snapshots the map. Load data files from disk or embed them in the application binary. Each filename's stem is its BCP 47 language tag, such as fr-CA.toml. Multiple files for one language can contribute disjoint keys.
Place locales/en.json next to this Go file, then add more locales to the directory as needed.
package translations
import (
"embed"
"github.com/waterkyuu/go-intl"
)
//go:embed locales/*
var files embed.FS
// Load compiles the embedded translations at application startup.
func Load() (*intl.Catalog, error) {
sources, err := intl.LoadFS(files, "locales/*")
if err != nil {
return nil, err
}
return intl.New("en", sources...)
}
| Source | Example defining the same key |
|---|
| Go | intl.Define("en", map[string]string{"Inbox.title": "Inbox"}) |
| JSON | {"Inbox": {"title": "Inbox"}} |
| TOML | Inbox.title = "Inbox" |
| YAML | Inbox: {title: Inbox} |
| CSV | Header key,message, followed by a row Inbox.title,Inbox |
JSON, TOML and YAML also accept dotted string keys. CSV supports quoted commas and multiline messages. Empty translations are valid. File decoding errors identify the file. Catalog construction reports the locale, key and source for invalid patterns and overlapping definitions.
Languages, namespaces and errors