Documentation
¶
Index ¶
- func AddTranslations(loc string, msgs map[string]string)
- func BootFromEnv()
- func FromContext(ctx context.Context) string
- func LoadDirectory(dir string) error
- func LoadJSONBytes(locale string, data []byte) error
- func LoadJSONFile(locale, path string) error
- func Middleware() router.Middleware
- func SetDefault(loc string)
- func T(key string, args ...any) string
- func TCtx(ctx context.Context, key string, args ...any) string
- func TLocale(loc, key string, args ...any) string
- func WithLocale(ctx context.Context, loc string) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddTranslations ¶
AddTranslations registers translation strings for a locale.
func BootFromEnv ¶
func BootFromEnv()
BootFromEnv loads translations from disk and applies APP_LOCALE default. Directories: LANG_PATH if set (single dir), else "lang" and "resources/lang"; then RESOURCES_LANG_PATH if set. Missing directories are ignored.
func FromContext ¶
FromContext returns the locale stored in context, or empty string.
func LoadDirectory ¶
LoadDirectory walks dir for locale files. Supported layout:
lang/en.json → locale "en" lang/en/messages.json → locale "en" (merged) resources/lang/en.json (same)
Only files named *.json are loaded; subdirectories named with a locale tag (two+ letters) load all JSON inside them into that locale.
func LoadJSONBytes ¶
LoadJSONBytes merges JSON translation data into locale.
func LoadJSONFile ¶
LoadJSONFile loads a single JSON file of translations into the given locale. The file must be a JSON object with string values (nested objects are flattened with dot keys, e.g. {"auth": {"failed": "x"}} → "auth.failed").
func Middleware ¶
func Middleware() router.Middleware
Middleware inspects Accept-Language and sets the locale on the request context. It stores the value in both request.Context() and reqctx.Context for downstream use.
func SetDefault ¶
func SetDefault(loc string)
SetDefault sets the default locale (used by T when no context is available).
func T ¶
T returns the translated string for key using the default locale. If not found, the key itself is returned.
func TCtx ¶
TCtx uses the locale from ctx (set by locale.Middleware or WithLocale). Falls back to the default locale when ctx has no locale.
Types ¶
This section is empty.