Documentation
¶
Overview ¶
Package i18n provides a unified internationalization module supporting locale detection, translation lookup with fallback, locale-specific formatting, and pluggable machine-translation backends.
The core package has zero external dependencies. Optional integrations live in sub-modules:
i18n/gin — Gin middleware and helpers i18n/mymemory — MyMemory API translator
Index ¶
- func SetLocale(ctx context.Context, locale Locale) context.Context
- func WithLocale(ctx context.Context, locale Locale) context.Context
- type Config
- type CurrencyFormat
- type Formatter
- type Locale
- type Manager
- func (m *Manager) DetectLocale(lang string) Locale
- func (m *Manager) GetDefaultLocale() Locale
- func (m *Manager) GetSupportedLocales() []Locale
- func (m *Manager) GetTranslation(locale Locale, key string) string
- func (m *Manager) GetTranslations(locale Locale) map[string]string
- func (m *Manager) IsSupportedLocale(locale Locale) bool
- func (m *Manager) LoadTranslationFile(locale Locale, filePath string) error
- func (m *Manager) LoadTranslations(path string) error
- func (m *Manager) ParseAcceptLanguage(header string) Locale
- func (m *Manager) ResolveLocale(raw string) Locale
- func (m *Manager) SetTranslation(locale Locale, key, value string)
- func (m *Manager) T(locale Locale, key string, args ...interface{}) string
- func (m *Manager) Translate(text, from, to string) (string, error)
- type NumberFormat
- type TimeUnits
- type Translator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
DefaultLocale Locale // fallback when no locale is detected; defaults to "en"
SupportedLocales []Locale // locales this manager recognises
FallbackLocale Locale // secondary fallback for missing keys; defaults to DefaultLocale
// TranslationsPath is a directory of JSON translation files.
// Filenames must follow the pattern: <prefix>.<locale>.json
// e.g. messages.en.json, messages.zh-CN.json
TranslationsPath string
// Translator is an optional external MT backend.
Translator Translator
}
Config holds i18n Manager configuration.
type CurrencyFormat ¶
CurrencyFormat holds locale-specific currency formatting rules.
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter handles locale-specific formatting of numbers, currency, dates, and relative time.
func NewFormatter ¶
NewFormatter creates a formatter for the given locale.
func (*Formatter) FormatCurrency ¶
FormatCurrency formats a monetary amount with the locale's currency symbol.
func (*Formatter) FormatDate ¶
FormatDate formats a date using a token-based format string. Supported tokens: YYYY, MM, DD, HH, mm, ss. If format is empty, the locale's default date format is used.
func (*Formatter) FormatNumber ¶
FormatNumber formats a number with the given decimal places and locale-specific separators.
type Locale ¶
type Locale string
Locale represents a BCP 47 locale identifier (e.g. "en", "zh-CN", "ja-JP").
const ( LocaleEn Locale = "en" LocaleEnUS Locale = "en-US" LocaleEnGB Locale = "en-GB" LocaleZhCN Locale = "zh-CN" LocaleZhTW Locale = "zh-TW" LocaleJaJP Locale = "ja-JP" LocaleKoKR Locale = "ko-KR" LocaleFrFR Locale = "fr-FR" LocaleDeDE Locale = "de-DE" LocaleEsES Locale = "es-ES" )
Common locale constants.
func GetLocaleFromContext ¶
GetLocaleFromContext extracts the locale from a context, returning the default locale if none is set.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles internationalization: translation storage, lookup, locale detection, and formatting.
func NewManager ¶
NewManager creates a new i18n manager from the given config. If config is nil, sensible defaults are applied.
func (*Manager) DetectLocale ¶
DetectLocale detects a locale from a language string (e.g. "en-US", "zh-CN").
func (*Manager) GetDefaultLocale ¶
GetDefaultLocale returns the manager's default locale.
func (*Manager) GetSupportedLocales ¶
GetSupportedLocales returns the list of supported locales.
func (*Manager) GetTranslation ¶
GetTranslation looks up a key in the requested locale, falling back to the fallback locale, then returning the key itself if not found.
func (*Manager) GetTranslations ¶
GetTranslations returns a copy of all translations for a locale.
func (*Manager) IsSupportedLocale ¶
IsSupportedLocale reports whether the locale is in the supported list.
func (*Manager) LoadTranslationFile ¶
LoadTranslationFile loads a single JSON translation file for a locale.
func (*Manager) LoadTranslations ¶
LoadTranslations walks path and loads all *.json translation files. The locale is extracted from the filename suffix (e.g. "messages.zh-CN.json" → "zh-CN").
func (*Manager) ParseAcceptLanguage ¶
ParseAcceptLanguage parses an Accept-Language header value and returns the first supported locale found, or the default locale if none match.
Example header: "en-US,en;q=0.9,zh-CN;q=0.8"
func (*Manager) ResolveLocale ¶
ResolveLocale maps a raw locale string to one of the supported locales. If the raw string doesn't match, the manager's default locale is returned.
func (*Manager) SetTranslation ¶
SetTranslation sets a single key-value pair for a locale.
type NumberFormat ¶
NumberFormat holds locale-specific number formatting rules.
type TimeUnits ¶
type TimeUnits struct {
JustNow string
Seconds string
Minutes string
Hours string
Days string
Months string
Years string
}
TimeUnits holds translated relative-time unit labels.
type Translator ¶
Translator is the interface for external machine-translation services. Implementations live in sub-modules (e.g. i18n/mymemory).