Documentation
¶
Overview ¶
Package i18n provides locale-aware message lookup with ICU MessageFormat support.
Index ¶
- Variables
- func ContextWithLocalizer(ctx context.Context, l *Localizer) context.Context
- type Detector
- type DetectorOption
- type DetectorSource
- type I18n
- func (i *I18n) Has(locale, key string) bool
- func (i *I18n) IsLanguageSupported(lang language.Tag) bool
- func (i *I18n) Keys(locale string) []string
- func (i *I18n) LoadFS(fsys fs.FS, patterns ...string) error
- func (i *I18n) LoadFiles(files ...string) error
- func (i *I18n) LoadGlob(patterns ...string) error
- func (i *I18n) LoadMessages(msgs map[string]map[string]string) error
- func (i *I18n) MatchAvailableLocale(accepts ...string) string
- func (i *I18n) NewLocalizer(locales ...string) *Localizer
- func (i *I18n) SupportedLocales() []language.Tag
- type Localizer
- func (l *Localizer) Format(message string, data ...Vars) (string, error)
- func (l *Localizer) Get(name string, data ...Vars) string
- func (l *Localizer) GetX(name, context string, data ...Vars) string
- func (l *Localizer) Locale() string
- func (l *Localizer) Lookup(name string, data ...Vars) TranslationResult
- type Option
- func WithCustomFormatters(formatters map[string]any) Option
- func WithDefaultLocale(locale string) Option
- func WithFallback(f map[string][]string) Option
- func WithLocales(locales ...string) Option
- func WithMessageFormatOptions(opts *mf.MessageFormatOptions) Option
- func WithStrictMode(strict bool) Option
- func WithUnmarshaler(u Unmarshaler) Option
- type TranslationResult
- type TranslationSource
- type Unmarshaler
- type Vars
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMessageFormatCompilation = errors.New("messageformat compilation failed")
ErrMessageFormatCompilation indicates that MessageFormat template compilation failed. The translation text is returned as-is without formatting capabilities.
Functions ¶
Types ¶
type Detector ¶ added in v0.3.1
type Detector struct {
// contains filtered or unexported fields
}
Detector resolves the best locale for an HTTP request.
func NewDetector ¶ added in v0.3.1
func NewDetector(bundle *I18n, opts ...DetectorOption) *Detector
NewDetector creates a request locale detector for the given bundle.
type DetectorOption ¶ added in v0.3.1
type DetectorOption func(*Detector)
DetectorOption configures a Detector.
func WithDetectorCookieName ¶ added in v0.3.1
func WithDetectorCookieName(name string) DetectorOption
WithDetectorCookieName sets the cookie name for locale detection.
func WithDetectorHeaderName ¶ added in v0.3.1
func WithDetectorHeaderName(name string) DetectorOption
WithDetectorHeaderName sets the header name for locale detection.
func WithDetectorPriority ¶ added in v0.3.1
func WithDetectorPriority(priority ...DetectorSource) DetectorOption
WithDetectorPriority sets the detector source priority.
func WithDetectorQueryParam ¶ added in v0.3.1
func WithDetectorQueryParam(name string) DetectorOption
WithDetectorQueryParam sets the query parameter name for locale detection.
type DetectorSource ¶ added in v0.4.0
type DetectorSource string
DetectorSource identifies a request input that can supply a locale.
const ( // DetectorSourceQuery reads the locale from the request query string. DetectorSourceQuery DetectorSource = "query" // DetectorSourceCookie reads the locale from a request cookie. DetectorSourceCookie DetectorSource = "cookie" // DetectorSourceHeader reads the locale from a request header. DetectorSourceHeader DetectorSource = "header" // DetectorSourceAccept reads the locale from the Accept-Language header. DetectorSourceAccept DetectorSource = "accept-language" )
type I18n ¶
type I18n struct {
// contains filtered or unexported fields
}
I18n is the main internationalization bundle that manages translations, locales, and fallback chains.
func NewBundle ¶
NewBundle creates a new internationalization bundle with the given options. If no default locale is set, the first locale from WithLocales is used; if no locales are configured, English is used as the default.
func (*I18n) Has ¶ added in v0.4.0
Has reports whether key is defined directly for locale in the loaded bundle state. Fallback-populated keys are not included.
func (*I18n) IsLanguageSupported ¶
IsLanguageSupported reports whether lang can be matched to a configured locale. Languages not in SupportedLocales may still match through the language matcher.
func (*I18n) Keys ¶ added in v0.3.1
Keys returns the sorted keys defined directly for locale in the loaded bundle state. Fallback-populated keys are not included.
func (*I18n) LoadMessages ¶
LoadMessages loads translations from a locale-keyed map. Locales not matching any configured locale are silently skipped.
func (*I18n) MatchAvailableLocale ¶
MatchAvailableLocale returns the best matching locale for the given Accept-Language header strings. Returns the default locale if no match is found.
func (*I18n) NewLocalizer ¶
NewLocalizer creates a Localizer for the first matching locale from locales. If none match, the default locale is used.
func (*I18n) SupportedLocales ¶ added in v0.4.0
SupportedLocales returns the configured locale tags for this bundle.
type Localizer ¶
type Localizer struct {
// contains filtered or unexported fields
}
Localizer provides translation methods for a specific locale. Create one via I18n.NewLocalizer.
func LocalizerFromContext ¶ added in v0.3.1
LocalizerFromContext returns the localizer stored in ctx.
func (*Localizer) Format ¶ added in v0.1.6
Format compiles and formats a MessageFormat message directly. This bypasses translation lookup and recompiles the message on each call, so it is intended for dynamic, non-hot-path messages that are not stored in translation files. Prefer Localizer.Get for normal translated content.
func (*Localizer) Get ¶
Get returns the translation for name with optional MessageFormat variables. Returns name as fallback if no translation is found.
func (*Localizer) GetX ¶
GetX returns the translation for name disambiguated by context. The context is appended as " <context>" to form the lookup key. For example, GetX("Post", "verb") looks up "Post <verb>".
func (*Localizer) Lookup ¶ added in v0.3.0
func (l *Localizer) Lookup(name string, data ...Vars) TranslationResult
Lookup returns the translation for name with full lookup details. Use Localizer.Get for the common case where only the text is needed.
type Option ¶ added in v0.2.4
type Option func(*I18n)
Option configures an I18n bundle. See WithDefaultLocale, WithLocales, WithFallback, and WithUnmarshaler for available options.
func WithCustomFormatters ¶ added in v0.1.6
WithCustomFormatters adds custom formatters for MessageFormat.
func WithDefaultLocale ¶
WithDefaultLocale sets the default locale. This locale is used when no translation is found in the requested locale or its fallback chain.
func WithFallback ¶
WithFallback configures locale fallback chains. Each key is a locale, and its value is an ordered list of fallback locales to try when a translation is missing. The default locale is used as the final fallback.
func WithLocales ¶
WithLocales sets the supported locales for the bundle. Invalid locale strings are silently ignored.
func WithMessageFormatOptions ¶ added in v0.1.6
func WithMessageFormatOptions(opts *mf.MessageFormatOptions) Option
WithMessageFormatOptions sets MessageFormat options for the bundle.
func WithStrictMode ¶ added in v0.1.6
WithStrictMode enables strict parsing mode for MessageFormat.
func WithUnmarshaler ¶
func WithUnmarshaler(u Unmarshaler) Option
WithUnmarshaler sets a custom unmarshaler for translation files. The default is JSON. Common alternatives include YAML, TOML, and INI.
type TranslationResult ¶ added in v0.3.0
type TranslationResult struct {
// Text is the translated message, or the key itself if not found.
Text string
// Locale is the BCP 47 locale tag that produced Text.
Locale string
// Source reports whether the result came from the requested locale,
// the fallback chain, or runtime key fallback.
Source TranslationSource
}
TranslationResult holds detailed translation lookup information.
type TranslationSource ¶ added in v0.4.0
type TranslationSource string
TranslationSource describes how a lookup result was produced.
const ( // TranslationSourceDirect indicates that the requested locale supplied the translation. TranslationSourceDirect TranslationSource = "direct" // TranslationSourceFallback indicates that a fallback locale supplied the translation. TranslationSourceFallback TranslationSource = "fallback" // TranslationSourceMissing indicates that no loaded translation was found and the key was returned. TranslationSourceMissing TranslationSource = "missing" )
type Unmarshaler ¶
Unmarshaler unmarshals translation files. Common implementations include json.Unmarshal, yaml.Unmarshal, and toml.Unmarshal.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
embed
command
|
|
|
files
command
|
|
|
glob
command
|
|
|
icu
command
|
|
|
ini
command
|
|
|
nested_files
command
|
|
|
source_locale
command
|
|
|
text
command
|
|
|
toml
command
|
|
|
yml
command
|
|
|
Package middleware provides optional HTTP integration for go-i18n.
|
Package middleware provides optional HTTP integration for go-i18n. |