Documentation
¶
Index ¶
- func DetectLocaleAndSet(r *http.Request) string
- func DetectLocaleFromRequest(r *http.Request) string
- func GetLocale() string
- func LoadLocale(locale string) error
- func SetDefaultTranslator(t *Translator)
- func SetFallbackLocale(locale string)
- func SetLocale(locale string)
- func Trans(key string, replaces ...map[string]string) string
- func TransChoice(key string, count int, replaces ...map[string]string) string
- func Translate(key string, replaces ...map[string]string) string
- type ServiceProvider
- type Translator
- func (t *Translator) GetLocale() string
- func (t *Translator) Load(locale string) error
- func (t *Translator) SetFallbackLocale(locale string)
- func (t *Translator) SetLocale(locale string)
- func (t *Translator) TransChoice(key string, count int, replace map[string]string) string
- func (t *Translator) Translate(key string, replace map[string]string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DetectLocaleAndSet ¶
DetectLocaleAndSet detects locale from request and sets it globally.
func DetectLocaleFromRequest ¶
DetectLocaleFromRequest detects the locale from the HTTP request's Accept-Language header.
func SetDefaultTranslator ¶
func SetDefaultTranslator(t *Translator)
SetDefaultTranslator sets the app-wide translator instance (usually done in bootstrap).
func SetFallbackLocale ¶
func SetFallbackLocale(locale string)
SetFallbackLocale sets the fallback locale globally.
func TransChoice ¶
TransChoice is the global pluralization helper. Usage: TransChoice("apples", 5, map[string]string{})
Types ¶
type ServiceProvider ¶
type ServiceProvider struct {
// contains filtered or unexported fields
}
ServiceProvider registers the localization system.
func (*ServiceProvider) Boot ¶
func (p *ServiceProvider) Boot(app *foundation.Application)
func (*ServiceProvider) Register ¶
func (p *ServiceProvider) Register(app *foundation.Application)
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator handles translation of language lines.
func NewTranslator ¶
func NewTranslator(path, defaultLocale string) *Translator
NewTranslator initializes a new translator.
func (*Translator) GetLocale ¶
func (t *Translator) GetLocale() string
GetLocale returns the current locale.
func (*Translator) Load ¶
func (t *Translator) Load(locale string) error
Load loads translation strings for a locale from a JSON file.
func (*Translator) SetFallbackLocale ¶
func (t *Translator) SetFallbackLocale(locale string)
SetFallbackLocale sets the fallback locale.
func (*Translator) SetLocale ¶
func (t *Translator) SetLocale(locale string)
SetLocale sets the active locale.
func (*Translator) TransChoice ¶
TransChoice returns the translated string for a key based on count (pluralization). Supports simple CLDR-style rules:
- "apples" => "{0} No apples|{1} One apple|[2,*] :count apples"
- Rules are pipe-separated, each prefixed with a count condition
- {0} = zero, {1} = one, [2,*] = two or more, [2,10] = range