Documentation
¶
Overview ¶
Package i18n provides internationalisation support: JSON translation files, CLDR plural rules, text/template interpolation, and a Bundle that holds per-locale Translators.
Index ¶
- func DirectionFor(lang string) string
- func DirectionFromContext(c echo.Context) string
- func InterpolationVars(s string) []string
- func IsPluralObject(obj map[string]any) bool
- func LocaleFromContext(c echo.Context) string
- func RegisterRule(lang string, rule PluralRule)
- type Bundle
- type BundleConfig
- type PluralCategory
- type PluralRule
- type Translator
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DirectionFor ¶
DirectionFor returns "rtl" for right-to-left languages and "ltr" otherwise. Supports region tags like "ar-SA" by checking the base language.
func DirectionFromContext ¶
DirectionFromContext returns the text direction from the context's translator.
func InterpolationVars ¶
func IsPluralObject ¶
IsPluralObject returns true when all keys in obj are valid CLDR plural category names and all values are strings.
func LocaleFromContext ¶
LocaleFromContext returns the locale string from the Echo context.
func RegisterRule ¶
func RegisterRule(lang string, rule PluralRule)
RegisterRule adds or overrides a plural rule for the given language tag.
Types ¶
type Bundle ¶
type Bundle struct {
// contains filtered or unexported fields
}
Bundle holds translators for all loaded locales.
func NewBundle ¶
func NewBundle(cfg BundleConfig) (*Bundle, error)
NewBundle reads all *.json files from cfg.LocaleDir, validates them against the default locale, and returns a ready-to-use Bundle.
func (*Bundle) DefaultLocale ¶
DefaultLocale returns the default locale code.
func (*Bundle) ResolveLocale ¶
ResolveLocale returns the best matching loaded locale for a code that may include a region tag (e.g. "fr-FR"). It tries an exact match first, then the base language. Returns ("", false) if no match is found.
func (*Bundle) SupportedLocales ¶
SupportedLocales returns the sorted list of loaded locale codes.
func (*Bundle) Translator ¶
func (b *Bundle) Translator(locale string) *Translator
Translator returns the Translator for a locale, falling back to the default.
type BundleConfig ¶
type BundleConfig struct {
LocaleDir string // directory containing *.json files
DefaultLocale string // e.g. "en"
FallbackToDefault *bool // fall back to default locale for missing keys (default true; nil = true)
Logger *slog.Logger // receives missing/extra-key warnings; nil = slog.Default()
}
BundleConfig configures how a Bundle loads locale files.
type PluralCategory ¶
type PluralCategory string
PluralCategory represents a CLDR plural category.
const ( Zero PluralCategory = "zero" One PluralCategory = "one" Two PluralCategory = "two" Few PluralCategory = "few" Many PluralCategory = "many" Other PluralCategory = "other" )
type PluralRule ¶
type PluralRule func(n int) PluralCategory
PluralRule maps a count to its CLDR plural category for a language.
func RuleFor ¶
func RuleFor(lang string) PluralRule
RuleFor returns the plural rule for a language. It tries the full tag first (e.g. "fr-CA"), then the base language ("fr"), then custom rules, then builtins, falling back to English.
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator holds the messages for a single locale and resolves translations.
func FromContext ¶
func FromContext(c echo.Context) *Translator
FromContext retrieves the Translator stored in the Echo context. Panics if no translator has been set (middleware not applied).
func (*Translator) Direction ¶
func (t *Translator) Direction() string
Direction returns the text direction ("ltr" or "rtl").
func (*Translator) Has ¶
func (t *Translator) Has(key string) bool
Has reports whether a key exists in this translator (not checking fallback).
func (*Translator) Locale ¶
func (t *Translator) Locale() string
Locale returns the locale code (e.g. "en").
func (*Translator) T ¶
func (t *Translator) T(key string, args ...any) string
T translates a key with optional arguments.
Argument patterns:
- No args: return plain string.
- int arg: use as plural count; remaining args for interpolation data.
- map[string]any arg: use as interpolation data.
Falls back to the fallback translator, then returns the key as-is.
type ValidationError ¶
type ValidationError struct {
Locale string
Key string
Kind string // one of the validation* constants
Message string
}
ValidationError describes a mismatch between locale files.
func (ValidationError) Error ¶
func (e ValidationError) Error() string