Skip to content

Internationalization ​

github.com/tangoui/tango/i18n reads the system's preferred languages, lets the app override its current locale, and looks up messages with named parameters and CLDR plural categories. Native menu roles and default dialog buttons use the current locale.

Message catalogs ​

Use one flat JSON object per language, named after the language tag. Underscores also work in filenames. A plural entry has CLDR category keys (zero, one, two, few, many, other) and requires other.

json
{
  "hello": "Hello, {name}!",
  "files": { "one": "{count} file selected", "other": "{count} files selected" }
}
go
//go:embed locales
var locales embed.FS

// In app startup:
if err := i18n.Load(locales, "locales"); err != nil { log.Fatal(err) }
i18n.SetLocale("en")
i18n.T("hello", "name", "Tom") // Hello, Tom!
i18n.N("files", 3)             // 3 files selected

Pass parameters as name/value pairs. Unknown placeholders stay in the text. Use {{ and }} for literal braces. Lookups try the current language, then English, then return the key. Development builds warn once per missing key. A later load for the same locale overrides existing entries with the same keys. Register additional plural rules with i18n.RegisterPluralRule.

System and current locale ​

go
pref := i18n.Preferred()   // BCP 47 tags in preference order
supported := i18n.Supported()
current := i18n.Locale()
i18n.SetLocale("ja")       // use "" to follow the system again
cancel := i18n.OnChange(func(loc string) { rebuildUI() })

Matching tries an exact tag, then the matching Chinese script, then language and region, then language alone, and falls back to English. Simplified and Traditional Chinese are kept distinct. Without custom catalogs, the built-in locales are en, zh-CN, zh-TW and ja.

PlatformLanguage source
macOSNSLocale.preferredLanguages
WindowsGetUserPreferredUILanguages
LinuxLANGUAGE, LC_ALL, LC_MESSAGES, LANG
iOSNSLocale.preferredLanguages
AndroidLocaleList after native host initialization

macOS, Windows, iOS and Android refresh when their backend reports a language or locale change. On Linux, call i18n.Refresh() when needed. The package also works in tools and tests without a tango.App.

Native controls and app content ​

The native roles and dialog defaults have built-in translations for English, Simplified and Traditional Chinese, and Japanese. After SetLocale, refresh menus with menu.SetAppMenu or Menu.Update(). AppKit-provided items such as dynamic Undo and Redo labels follow the bundle's localization declarations. The loose executable shows those items in English.

WebView pages manage their own strings. Pass i18n.Locale() to the page via a binding or event to keep the languages in sync. uihost.Open follows i18n.Locale() for Go-drawn component text by default. Use uihost.LocaleFixed to manage it yourself, or look up a pack with ui.LocaleByCode and set it with ui.SetDefaultLocale.

See examples/i18n-demo for a working app. It accepts -print and -lang ja.

Released under the MIT License