Documentation
¶
Overview ¶
Package i18n provides a framework-agnostic translation engine.
Translations are stored as nested JSON objects keyed by namespace. The "::" delimiter separates namespaces so that key names may contain dots, underscores, or hyphens (e.g. "shared::enum::role::org.admin").
Lookups follow a fallback chain: exact locale (e.g. "en_US") -> base language ("en") -> configured fallback language -> the key itself.
The package does not embed any locale files of its own; consumers bring their own translations via NewI18n, LoadFromFS, or LoadFromDir. A separate generator (cmd/i18n-gen) produces compile-time-safe key constants from a primary locale file.
Index ¶
- Constants
- type I18n
- func (i *I18n) GetAvailableLanguages() []string
- func (i *I18n) GetDefaultLanguage() string
- func (i *I18n) GetFallbackLanguage() string
- func (i *I18n) GetKeyValues(locale, parentKey string, params ...map[string]string) map[string]string
- func (i *I18n) GetKeys(locale, parentKey string) []string
- func (i *I18n) GetValues(locale, parentKey string, params ...map[string]string) []KeyValue
- func (i *I18n) HasTranslation(locale, key string) bool
- func (i *I18n) Translate(locale, key string, params ...map[string]string) string
- type KeyValue
- type Translation
Constants ¶
const NamespaceDelimiter = "::"
NamespaceDelimiter separates namespaces in a translation key. Dots, underscores, and hyphens are preserved as part of key names.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type I18n ¶
type I18n struct {
// contains filtered or unexported fields
}
I18n is a stateless translation engine. Safe for concurrent use; the internal state is built once in NewI18n and never mutated after that.
Internally each locale is held in two shapes:
- the original nested tree, used by GetKeys / GetKeyValues which need subtree access;
- a flattened map[fullKey]value built at construction time, used by the hot Translate / HasTranslation paths so each lookup is a single map probe with no string splitting or tree-walking.
This costs roughly one extra string pointer per (locale, leaf-key) pair — kilobytes for a typical app — and turns Translate into an allocation-free O(1) call once the value is found.
func LoadFromDir ¶
LoadFromDir is the os.DirFS equivalent of LoadFromFS — useful for loading translations at runtime from disk during development.
func LoadFromFS ¶
LoadFromFS reads every *.json file directly under dir in fsys and returns an I18n instance. File names (minus .json) become locale codes. Consumers typically pass an embed.FS so locale files ship inside the binary:
//go:embed locales/*.json var localesFS embed.FS i18nInstance, err := i18n.LoadFromFS(localesFS, "locales", "en", "en")
func NewI18n ¶
func NewI18n(defaultLanguage, fallbackLanguage string, translations map[string]Translation) *I18n
NewI18n creates a new I18n instance from an in-memory map of translations keyed by locale code. The nested trees are walked once to build per-locale flat lookup tables; after this call the result is safe for concurrent reads.
func (*I18n) GetAvailableLanguages ¶
GetAvailableLanguages returns the sorted list of loaded locale codes.
func (*I18n) GetDefaultLanguage ¶
GetDefaultLanguage returns the default language code.
func (*I18n) GetFallbackLanguage ¶
GetFallbackLanguage returns the fallback language code.
func (*I18n) GetKeyValues ¶
func (i *I18n) GetKeyValues(locale, parentKey string, params ...map[string]string) map[string]string
GetKeyValues returns direct string children of parentKey as a {localKey: translation} map, suitable for populating dropdowns and enum-like UIs. Nested objects are ignored. Translation values are resolved using the same fallback chain as Translate, with optional placeholder interpolation.
func (*I18n) GetKeys ¶
GetKeys returns every leaf key under parentKey, with the full namespace prefix preserved. Results are merged across exact locale, base language, and the fallback language so the caller sees the complete set of available keys regardless of which file defines them. Pass an empty parentKey to walk the full tree.
func (*I18n) GetValues ¶
GetValues is the slice-of-pairs variant of GetKeyValues, sorted by id. Convenient for API responses where map ordering is unwanted.
func (*I18n) HasTranslation ¶
HasTranslation reports whether a translation exists for key in the requested locale (including base-language fallback).
func (*I18n) Translate ¶
Translate resolves key for the requested locale, falling back to the base language (e.g. "en" for "en_US") and then the configured fallback language. If no translation is found the key is returned unchanged. When params is supplied, {{name}} placeholders are interpolated; passing no params (or a nil map) skips interpolation entirely.
type Translation ¶
Translation is a nested map of translation values. Leaf values are strings; intermediate nodes are nested Translation maps.