Documentation
¶
Overview ¶
Package i18n formats translated messages. It implements the same message syntax and plural rules as the client runtime (packages/tern/src/runtime/ i18n), and both are tested against testdata/cases.json, so the server and the browser always produce the same text.
Messages support a subset of ICU MessageFormat:
Hello, {name}!
{count, plural, =0 {No items} one {# item} other {# items}}
{role, select, admin {Administrator} other {Member}}
Quote braces with apostrophes ('{'), and write an apostrophe as ”.
Index ¶
- Variables
- func Flatten(raw []byte) (map[string]string, error)
- func Format(locale, msg string, params map[string]any) string
- type Catalog
- func (c *Catalog) Canonical(path string) string
- func (c *Catalog) Has(locale string) bool
- func (c *Catalog) Keys() []string
- func (c *Catalog) Match(acceptLanguage string) string
- func (c *Catalog) Missing() map[string][]string
- func (c *Catalog) RouteSegments(locale string) map[string]string
- func (c *Catalog) T(locale, key string, params map[string]any) string
- func (c *Catalog) TB(locale, key string, params map[string]any) string
Constants ¶
This section is empty.
Variables ¶
var Builtin = func() map[string]string { m := map[string]string{} if err := json.Unmarshal(builtinJSON, &m); err != nil { panic(err) } return m }()
Builtin holds Tern's own texts in English: Tern UI's and the validation messages, under keys starting with "tern.". An app translates them in src/locales like its own messages.
Functions ¶
Types ¶
type Catalog ¶
Catalog holds every locale's messages, flattened to dotted keys.
func Load ¶
Load reads <locale>.json files from fsys. defaultLocale must be one of them; if it is "", "en" is used when present, else the first locale.
func (*Catalog) Canonical ¶ added in v1.2.0
Canonical rewrites a URL path that starts with a locale, such as /de/projekte/42, to the route directories' names, /de/projects/42, so it matches the routes. Other paths are returned unchanged.
func (*Catalog) Match ¶
Match picks the best supported locale for an Accept-Language header, or "" if none fits.
func (*Catalog) Missing ¶
Missing returns, for each other locale, the default locale's keys it lacks.
func (*Catalog) RouteSegments ¶ added in v1.2.0
RouteSegments returns a locale's translations of URL path segments, from route directory names to the words in its URLs.