Documentation ¶
Overview ¶
Package i18n provides internalization and localization features.
Index ¶
- Variables
- func GetMessage(r *http.Request, format string, args ...interface{}) string
- func Router(next http.Handler) http.Handler
- func SetDefaultLanguage(langCode string) bool
- func Tr(lang, format string, args ...interface{}) string
- type I18n
- func (i *I18n) GetLocale(r *http.Request) *Locale
- func (i *I18n) GetMessage(r *http.Request, format string, args ...interface{}) (msg string)
- func (i *I18n) Router(next http.Handler) http.Handler
- func (i *I18n) SetDefault(langCode string) bool
- func (i *I18n) Tr(lang, format string, args ...interface{}) (msg string)
- func (i *I18n) TryMatchString(s string) (language.Tag, int, bool)
- type LangMap
- type Loader
- type LoaderConfig
- type Locale
- type Localizer
- type Map
- type Matcher
- type MessageFunc
Constants ¶
This section is empty.
Variables ¶
var DefaultLoaderConfig = LoaderConfig{ Left: "{{", Right: "}}", Strict: false, DefaultMessageFunc: nil, PluralFormDecoder: internal.DefaultPluralFormDecoder, Funcs: nil, }
DefaultLoaderConfig represents the default loader configuration.
Functions ¶
func GetMessage ¶
GetMessage is package-level function which calls the `Default.GetMessage` method.
See `I18n#GetMessage` method for more.
func Router ¶
Router is package-level function which calls the `Default.Router` method.
See `I18n#Router` method for more.
func SetDefaultLanguage ¶
SetDefaultLanguage changes the default language of the `Default` `I18n` instance.
Types ¶
type I18n ¶
type I18n struct { // If not nil, this request's context key can be used to identify the current language. // The found language(in this case, by path or subdomain) will be also filled with the current language on `Router` method. ContextKey interface{} // DefaultMessageFunc is the field which can be used // to modify the behavior when a key or language was not found. // All language inputs fallback to the default locale if not matched. // This is why this one accepts both input and matched languages, // so the caller can be more expressful knowing those. // // Defaults to nil. DefaultMessageFunc MessageFunc // ExtractFunc is the type signature for declaring custom logic // to extract the language tag name. ExtractFunc func(*http.Request) string // If not empty, it is language identifier by url query. URLParameter string // If not empty, it is language identifier by cookie of this name. Cookie string // If true then a subdomain can be a language identifier too. Subdomain bool // If true then it will return empty string when translation for a a specific language's key was not found. // Defaults to false, fallback defaultLang:key will be used. Strict bool // contains filtered or unexported fields }
I18n is the structure which keeps the i18n configuration and implements Localization and internationalization features.
var Default *I18n
Default keeps a package-level pre-loaded `I18n` instance. The default glob pattern is "./locales/*/*" which accepts folder structure as: - ./locales
- el-GR
- filename.yaml
- filename.toml
- filename.json
- en-US
- ...
- zh-CN
- ...
- ...
The default language depends on the first lookup, please use the package-level `SetDefaultLanguage` to set a default language as you are not able to customize the language lists from here.
See `New` package-level function to declare a fresh new, customized, `I18n` instance.
func New ¶
New returns a new `I18n` instance. It contains a `Router` wrapper to (local) redirect subdomains and path prefixes too.
The "languages" input parameter is optional and if not empty then only these languages will be used for translations and the rest (if any) will be skipped. the first parameter of "loader" which lookups for translations inside files.
func (*I18n) GetLocale ¶
GetLocale returns the found locale of a request. It will return the first registered language if nothing else matched.
func (*I18n) GetMessage ¶
GetMessage returns the localized text message for this "r" request based on the key "format". It returns an empty string if locale or format not found.
func (*I18n) Router ¶
Router returns a new router wrapper. It compares the path prefix for translated language and local redirects the requested path with the selected (from the path) language to the router.
func (*I18n) SetDefault ¶
SetDefault changes the default language. Please avoid using this method; the default behavior will accept the first language of the registered tags as the default one.
func (*I18n) Tr ¶
Tr returns a translated message based on the "lang" language code and its key(format) with any optional arguments attached to it.
It returns an empty string if "lang" not matched, unless DefaultMessageFunc. It returns the default language's translation if "key" not matched, unless DefaultMessageFunc.
type LangMap ¶ added in v0.0.8
LangMap key as language (e.g. "el-GR") and value as a map of key-value pairs (e.g. "hello": "Γειά").
type Loader ¶
Loader accepts a `Matcher` and should return a `Localizer`. Functions that implement this type should load locale files.
func Assets ¶
func Assets(assetNames func() []string, asset func(string) ([]byte, error), options ...LoaderConfig) Loader
Assets accepts a function that returns a list of filenames (physical or virtual), another a function that should return the contents of a specific file and any Loader options. Go-bindata usage. It returns a valid `Loader` which loads and maps the locale files.
See `Glob`, `Assets`, `New` and `LoaderConfig` too.
func FS ¶ added in v0.0.8
FS is a virtual or local locale file system Loader. It accepts embed.FS or fs.FS or http.FileSystem. The "pattern" is a classic glob pattern.
See `Glob`, `Assets`, `New` and `LoaderConfig` too.
func Glob ¶
func Glob(globPattern string, options ...LoaderConfig) Loader
Glob accepts a glob pattern (see: https://golang.org/pkg/path/filepath/#Glob) and loads the locale files based on any "options".
The "globPattern" input parameter is a glob pattern which the default loader should search and load for locale files.
See `New` and `LoaderConfig` too.
func KV ¶ added in v0.0.8
func KV(langMap LangMap, opts ...LoaderConfig) Loader
KV is a loader which accepts a map of language(key) and the available key-value pairs. Example Code:
m := LangMap{ "en-US": Map{ "hello": "Hello", }, "el-GR": Map{ "hello": "Γειά", }, }
loader := KV(m, i18n.DefaultLoaderConfig) I18n, err := New(loader) I18N.SetDefault("en-US")
type LoaderConfig ¶
LoaderConfig the configuration structure which contains some options about how the template loader should act.
See `Glob` and `Assets` package-level functions.
type Locale ¶
Locale is the type which the `Localizer.GetLocale` method returns. It serves the translations based on "key" or format. See its `GetMessage`.
type Localizer ¶
type Localizer interface { // GetLocale should return a valid `Locale` based on the language index. // It will always match the Loader.Matcher.Languages[index]. // It may return the default language if nothing else matches based on custom localizer's criteria. GetLocale(index int) *Locale }
Localizer is the interface which returned from a `Loader`. Types that implement this interface should be able to retrieve a `Locale` based on the language index.
type Map ¶ added in v0.0.5
type Map = map[string]interface{}
Map is just an alias of the map[string]interface{} type.
type Matcher ¶
Matcher implements the languae.Matcher. It contains the original language Matcher and keeps an ordered list of the registered languages for further use (see `Loader` implementation).
func (*Matcher) Match ¶
Match returns the best match for any of the given tags, along with a unique index associated with the returned tag and a confidence score.
func (*Matcher) MatchOrAdd ¶
func (m *Matcher) MatchOrAdd(t language.Tag) (tag language.Tag, index int, conf language.Confidence)
MatchOrAdd acts like Match but it checks and adds a language tag, if not found, when the `Matcher.strict` field is true (when no tags are provided by the caller) and they should be dynamically added to the list.
type MessageFunc ¶ added in v0.0.4
type MessageFunc = internal.MessageFunc
MessageFunc is the function type to modify the behavior when a key or language was not found. All language inputs fallback to the default locale if not matched. This is why this signature accepts both input and matched languages, so caller can provide better messages.
The first parameter is set to the client real input of the language, the second one is set to the matched language (default one if input wasn't matched) and the third and forth are the translation format/key and its optional arguments.