Skip to content

i18n ​

go
import "github.com/tangoui/tango/i18n"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package i18n gives a Tango app the user's system languages, a current locale the app may override, and a small message catalog with named arguments and CLDR plural categories.

Everything works without a tango.App: the desktop backend packages register their system-language source when imported (the root tango package imports the one for the build platform), and without any backend — unit tests, command-line tools — the POSIX variables LANGUAGE, LC_ALL, LC_MESSAGES and LANG are used. Init(app) is only needed where the answer comes from the running Backend value (the mobile backends).

All functions are safe for concurrent use.

常量 ​

go
const DefaultLocale = "en"

DefaultLocale is the locale everything falls back to.

变量 ​

go
var Builtin = []string{"en", "zh-CN", "zh-TW", "ja"}

Builtin are the locales Tango itself ships strings for (native menu roles, dialog buttons). Locale picks from them until the app loads a catalog.

函数 ​

func Canonical ​

go
func Canonical(tag string) string

Canonical normalizes a BCP 47 tag or POSIX locale name: "zh_hans_cn.UTF-8" -> "zh-Hans-CN", "EN-us" -> "en-US". It returns "" for "", "C" and "POSIX". It does not validate the tag.

func Init ​

go
func Init(app AppBackend)

Init makes the app's backend the source of system languages when it implements platform.Locales, and the watcher of their changes when it implements platform.LocaleWatcher. Optional on desktop (see the package doc). It re-reads the system list and notifies OnChange listeners if the effective locale changed.

func Load ​

go
func Load(fsys fs.FS, dir string) error

Load reads every "<locale>.json" file in dir of fsys into the catalog, e.g. "locales/en.json", "locales/zh-CN.json" (the file name is the BCP 47 tag; "zh_CN" is accepted too). Each file is one flat JSON object mapping a key to either a string or, for plurals, an object of CLDR categories:

{
  "hello": "Hello, {name}!",
  "files": {"one": "{count} file", "other": "{count} files"}
}

Keys of a locale loaded again are replaced one by one, so an app can load a base set and then overrides. Load can be called at any time; listeners are notified if the effective locale changes because Supported did.

func Locale ​

go
func Locale() string

Locale returns the effective locale: the SetLocale override if any, otherwise the best match of Preferred among Supported. It is always one of Supported (or the override as given, canonicalized).

func Match ​

go
func Match(prefs, available []string) string

Match returns the entry of available that best serves the user's preference list prefs (most preferred first). Each preference is tried in turn against available:

  1. the same tag;
  2. for Chinese, the same script family: zh, zh-CN, zh-SG, zh-Hans-* go to the Simplified entry (zh-CN), zh-TW, zh-HK, zh-MO, zh-Hant-* to the Traditional one (zh-TW); the two are never substituted;
  3. the same language and region ("pt-BR" for "pt-Hant-BR");
  4. the same language, preferring an entry without a region ("en" for "en-GB").

If no preference matches, Match returns DefaultLocale when available has it, else available[0], else DefaultLocale.

func MatchOK ​

go
func MatchOK(prefs, available []string) (string, bool)

MatchOK is Match without the fallback: ok is false when no preference matches any entry.

func N ​

go
func N(key string, count int, args ...any) string

N returns the plural form of key for count in the current locale, with "count" and the name/value pairs in args available as arguments:

i18n.N("files", 3) // {"one": "{count} file", "other": "{count} files"} -> "3 files"

The category comes from the plural rule of the locale the message was found in (see PluralCategory); a missing category uses "other". A plain string entry is used for every count.

func OnChange ​

go
func OnChange(fn func(locale string)) (cancel func())

OnChange registers fn to run with the new effective locale whenever it changes (SetLocale, Refresh, Init, or Load changing Supported). fn runs on the goroutine that caused the change; for a change of the system languages that the platform reports, the main thread on macOS and Windows. The returned function unregisters.

func PluralCategory ​

go
func PluralCategory(locale string, n int) string

PluralCategory returns the CLDR plural category of n in locale.

func Preferred ​

go
func Preferred() []string

Preferred returns the user's preferred languages as BCP 47 tags, most preferred first ("zh-Hans-CN", "en-US"). The list is read once and cached; Refresh reads it again. It may be empty.

func Refresh ​

go
func Refresh()

Refresh re-reads the system languages, e.g. after the user changed them in the system settings, and notifies listeners if the effective locale changed. On macOS and Windows Tango calls it itself when the system reports a change (see watch.go); elsewhere (Linux) the app calls it, for example when its window regains focus.

func RegisterPluralRule ​

go
func RegisterPluralRule(lang string, rule PluralRule)

RegisterPluralRule sets the rule for a base language ("ru", "ar", ...), replacing a built-in one. Languages without a rule use English's.

func SetLocale ​

go
func SetLocale(tag string)

SetLocale overrides the system languages for this app. tag is matched against Supported ("zh-Hans" -> "zh-CN"); a tag nothing matches is used as given. An empty tag goes back to following the system. OnChange listeners run (on the calling goroutine) if the effective locale changed.

func Supported ​

go
func Supported() []string

Supported returns the locales Locale chooses from: those of the loaded catalogs, or Builtin when none is loaded.

func T ​

go
func T(key string, args ...any) string

T returns the message for key in the current locale with named arguments filled in. Arguments are name/value pairs:

i18n.T("hello", "name", "Tom") // "Hello, {name}!" -> "Hello, Tom!"

A missing key falls back to DefaultLocale and then to the key itself; in development builds (-tags tango_dev, as tan dev builds) each missing key is reported once on the log. For a plural entry T uses the "other" form.

类型 ​

AppBackend ​

go
type AppBackend interface {
	Backend() platform.Backend
}

AppBackend is satisfied by *tango.App. It is an interface so that i18n does not import the root package.

PluralRule ​

go
type PluralRule func(n int) string

A PluralRule maps a count to a CLDR plural category: "zero", "one", "two", "few", "many" or "other".

以 MIT 许可发布