i18n

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flatten

func Flatten(raw []byte) (map[string]string, error)

Flatten turns nested JSON objects into dotted keys: {"a": {"b": "x"}} becomes {"a.b": "x"}. Values must be strings.

func Format

func Format(locale, msg string, params map[string]any) string

Format formats one message.

Types

type Catalog

type Catalog struct {
	Default string
	Locales []string
	// contains filtered or unexported fields
}

Catalog holds every locale's messages, flattened to dotted keys.

func Load

func Load(fsys fs.FS, defaultLocale string) (*Catalog, error)

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

func (c *Catalog) Canonical(path string) string

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) Has

func (c *Catalog) Has(locale string) bool

Has reports whether locale has a messages file.

func (*Catalog) Keys

func (c *Catalog) Keys() []string

Keys returns the default locale's message keys, sorted.

func (*Catalog) Match

func (c *Catalog) Match(acceptLanguage string) string

Match picks the best supported locale for an Accept-Language header, or "" if none fits.

func (*Catalog) Missing

func (c *Catalog) Missing() map[string][]string

Missing returns, for each other locale, the default locale's keys it lacks.

func (*Catalog) RouteSegments added in v1.2.0

func (c *Catalog) RouteSegments(locale string) map[string]string

RouteSegments returns a locale's translations of URL path segments, from route directory names to the words in its URLs.

func (*Catalog) T

func (c *Catalog) T(locale, key string, params map[string]any) string

T formats key in locale, falling back to the default locale and then to the key itself.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL