localization

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectLocaleAndSet

func DetectLocaleAndSet(r *http.Request) string

DetectLocaleAndSet detects locale from request and sets it globally.

func DetectLocaleFromRequest

func DetectLocaleFromRequest(r *http.Request) string

DetectLocaleFromRequest detects the locale from the HTTP request's Accept-Language header.

func GetLocale

func GetLocale() string

GetLocale returns current locale.

func LoadLocale

func LoadLocale(locale string) error

LoadLocale preloads a locale.

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 SetLocale

func SetLocale(locale string)

SetLocale changes the current locale globally.

func Trans

func Trans(key string, replaces ...map[string]string) string

Trans is alias for Translate.

func TransChoice

func TransChoice(key string, count int, replaces ...map[string]string) string

TransChoice is the global pluralization helper. Usage: TransChoice("apples", 5, map[string]string{})

func Translate

func Translate(key string, replaces ...map[string]string) string

Translate is the global translation helper (Laravel-like). Usage: Translate("welcome.message", map[string]string{"name": "John"})

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

func (t *Translator) TransChoice(key string, count int, replace map[string]string) string

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

func (*Translator) Translate

func (t *Translator) Translate(key string, replace map[string]string) string

Translate returns the translated string for a key. Falls back to fallbackLocale if key not found in current locale.

Jump to

Keyboard shortcuts

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