Documentation
¶
Overview ¶
Package i18n translates user-interface text.
Each language is one JSON catalog:
{
"meta": {"locale": "es", "name": "Español", "english_name": "Spanish"},
"messages": {
"session.resumed": "Sesión {id} reanudada",
"files.count.one": "{count} archivo",
"files.count.other": "{count} archivos"
}
}
Catalogs are embedded for the shipped languages and also loaded from extra directories (e.g. ~/.blitz/locales), so a language can be added, or a shipped translation corrected, without rebuilding. Lookups fall back along the locale chain (fr-CA → fr → en-US), so a partial catalog still works.
Placeholders are {name}; values are passed as name/value pairs:
i18n.T("session.resumed", "id", id)
Plurals use ".one"/".other" suffixed keys selected by N.
Index ¶
- Constants
- Variables
- func CheckCatalog(data []byte) error
- func N(key string, count int, args ...any) string
- func Placeholders(msg string) []string
- func ReplyInstruction(l *Localizer) string
- func SetCurrent(l *Localizer)
- func T(key string, args ...any) string
- type Bundle
- type Catalog
- type Localizer
- func (l *Localizer) HasCatalog() bool
- func (l *Localizer) IsEnglish() bool
- func (l *Localizer) LanguageName() string
- func (l *Localizer) Lookup(key string) (string, bool)
- func (l *Localizer) N(key string, count int, args ...any) string
- func (l *Localizer) NativeName() string
- func (l *Localizer) T(key string, args ...any) string
- func (l *Localizer) Tag() language.Tag
- type Meta
Constants ¶
const DefaultLocale = "en-US"
DefaultLocale is the source language; every key must exist in it.
const PseudoLocale = "en-XA"
PseudoLocale renders English text with accents and brackets, to spot text that was never moved into a catalog.
Variables ¶
var ErrUnknownLocale = errors.New("unknown locale")
ErrUnknownLocale is returned when input can't be understood as a locale.
Functions ¶
func CheckCatalog ¶
CheckCatalog reports why data isn't a catalog a Bundle would load.
func Placeholders ¶
Placeholders returns the {names} used in msg.
func ReplyInstruction ¶
ReplyInstruction tells the model to answer in the user's language; empty for English.
Types ¶
type Bundle ¶
type Bundle struct {
// contains filtered or unexported fields
}
Bundle holds every loaded catalog.
func NewBundle ¶
NewBundle loads the embedded catalogs, then catalogs in dirs (later files add keys and override earlier translations). Problems with external files are returned but don't stop loading.
func (*Bundle) Localizer ¶
Localizer builds a localizer for tag, falling back through its parents and finally the default locale.
func (*Bundle) Problems ¶
Problems reports translation issues in catalog tag relative to the default: keys the default doesn't have, and placeholders that don't match.
type Catalog ¶
type Catalog struct {
Meta Meta `json:"meta"`
Messages map[string]string `json:"messages"`
// contains filtered or unexported fields
}
Catalog is one language's messages.
type Localizer ¶
type Localizer struct {
// contains filtered or unexported fields
}
Localizer renders messages for one locale.
func Current ¶
func Current() *Localizer
Current returns the active localizer (English until SetCurrent).
func (*Localizer) HasCatalog ¶
HasCatalog reports whether any non-default catalog serves this locale.
func (*Localizer) LanguageName ¶
LanguageName is the locale's English display name ("Spanish").
func (*Localizer) N ¶
N renders the plural form of key for count; count is also available as {count}.
func (*Localizer) NativeName ¶
NativeName is the locale's name in its own language ("español").