locale

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTranslations

func AddTranslations(loc string, msgs map[string]string)

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

func FromContext(ctx context.Context) string

FromContext returns the locale stored in context, or empty string.

func LoadDirectory

func LoadDirectory(dir string) error

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

func LoadJSONBytes(locale string, data []byte) error

LoadJSONBytes merges JSON translation data into locale.

func LoadJSONFile

func LoadJSONFile(locale, path string) error

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

func T(key string, args ...any) string

T returns the translated string for key using the default locale. If not found, the key itself is returned.

func TCtx

func TCtx(ctx context.Context, key string, args ...any) string

TCtx uses the locale from ctx (set by locale.Middleware or WithLocale). Falls back to the default locale when ctx has no locale.

func TLocale

func TLocale(loc, key string, args ...any) string

TLocale returns the translated string for key in the given locale.

func WithLocale

func WithLocale(ctx context.Context, loc string) context.Context

WithLocale stores the locale in context.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL