i18n
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.
常量
const DefaultLocale = "en"DefaultLocale is the locale everything falls back to.
变量
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
func Canonical(tag string) stringCanonical 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
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
func Load(fsys fs.FS, dir string) errorLoad 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
func Locale() stringLocale 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
func Match(prefs, available []string) stringMatch 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:
- the same tag;
- 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;
- the same language and region ("pt-BR" for "pt-Hant-BR");
- 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
func MatchOK(prefs, available []string) (string, bool)MatchOK is Match without the fallback: ok is false when no preference matches any entry.
func N
func N(key string, count int, args ...any) stringN 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
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
func PluralCategory(locale string, n int) stringPluralCategory returns the CLDR plural category of n in locale.
func Preferred
func Preferred() []stringPreferred 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
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
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
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
func Supported() []stringSupported returns the locales Locale chooses from: those of the loaded catalogs, or Builtin when none is loaded.
func T
func T(key string, args ...any) stringT 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
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
type PluralRule func(n int) stringA PluralRule maps a count to a CLDR plural category: "zero", "one", "two", "few", "many" or "other".