i18n: github.com/kataras/i18n Index | Files

package i18n

import "github.com/kataras/i18n"

Package i18n provides internalization and localization features.

Index

Package Files

doc.go i18n.go loader.go

func GetMessage Uses

func GetMessage(r *http.Request, format string, args ...interface{}) string

GetMessage is package-level function which calls the `Default.GetMessage` method.

See `I18n#GetMessage` method for more.

func Router Uses

func Router(next http.Handler) http.Handler

Router is package-level function which calls the `Default.Router` method.

See `I18n#Router` method for more.

func SetDefaultLanguage Uses

func SetDefaultLanguage(langCode string) bool

SetDefaultLanguage changes the default language of the `Default` `I18n` instance.

func Tr Uses

func Tr(lang, format string, args ...interface{}) string

Tr is package-level function which calls the `Default.Tr` method.

See `I18n#Tr` method for more.

type I18n Uses

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{}
    // 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 Uses

func New(loader Loader, languages ...string) (*I18n, error)

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 Uses

func (i *I18n) GetLocale(r *http.Request) Locale

GetLocale returns the found locale of a request. It will return the first registered language if nothing else matched.

func (*I18n) GetMessage Uses

func (i *I18n) GetMessage(r *http.Request, format string, args ...interface{}) string

GetMessage returns the localized text message for this "r" request based on the key "format".

func (*I18n) Router Uses

func (i *I18n) Router(next http.Handler) http.Handler

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 Uses

func (i *I18n) SetDefault(langCode string) bool

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 Uses

func (i *I18n) Tr(lang, format string, args ...interface{}) string

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 "format" not matched.

func (*I18n) TryMatchString Uses

func (i *I18n) TryMatchString(s string) (language.Tag, int, bool)

TryMatchString will try to match the "s" with a registered language tag. It returns -1 as the language index and false if not found.

type Loader Uses

type Loader func(m *Matcher) (Localizer, error)

Loader accepts a `Matcher` and should return a `Localizer`. Functions that implement this type should load locale files.

func Assets Uses

func Assets(assetNames func() []string, asset func(string) ([]byte, error), options ...LoaderOption) 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 Glob Uses

func Glob(globPattern string, options ...LoaderOption) 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.

type LoaderConfig Uses

type LoaderConfig struct {
    // Template delimeters, defaults to {{ }}.
    Left, Right string
    // Template functions map, defaults to nil.
    FuncMap template.FuncMap
    // If true then it will return error on invalid templates instead of moving them to simple string-line keys.
    // Also it will report whether the registered languages matched the loaded ones.
    // Defaults to false.
    Strict bool
}

LoaderConfig is an optional configuration structure which contains some options about how the template loader should act.

See `Glob` and `Assets` package-level functions.

type LoaderOption Uses

type LoaderOption func(*LoaderConfig)

LoaderOption is a type which accepts a pointer to `LoaderConfig` and can be optionally passed to the second variadic input argument of the `Glob` and `Assets` functions.

type Locale Uses

type Locale interface {
    // Index returns the current locale index from the languages list.
    Index() int
    // Tag returns the full language Tag attached to this Locale,
    // it should be unique across different Locales.
    Tag() *language.Tag
    // Language should return the exact language code of this `Locale`
    // that the user provided on `New` function.
    //
    // Same as `Tag().String()` but it's static.
    Language() string
    // GetMessage should return translated text based on the given "key".
    GetMessage(key string, args ...interface{}) string
}

Locale is the interface which returns from a `Localizer.GetLocale` method. It serves the translations based on "key" or format. See `GetMessage`.

func GetLocale Uses

func GetLocale(r *http.Request) Locale

GetLocale is package-level function which calls the `Default.GetLocale` method.

See `I18n#GetLocale` method for more.

type Localizer Uses

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 Matcher Uses

type Matcher struct {
    Languages []language.Tag
    // contains filtered or unexported fields
}

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 Uses

func (m *Matcher) Match(t ...language.Tag) (language.Tag, int, language.Confidence)

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 Uses

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.

func (*Matcher) ParseLanguageFiles Uses

func (m *Matcher) ParseLanguageFiles(fileNames []string) (map[int][]string, error)

ParseLanguageFiles returns a map of language indexes and their associated files based on the "fileNames".

type MemoryLocalizer Uses

type MemoryLocalizer map[int]Locale

MemoryLocalizer is a map which implements the `Localizer`.

func (MemoryLocalizer) GetLocale Uses

func (l MemoryLocalizer) GetLocale(index int) Locale

GetLocale returns a valid `Locale` based on the "index".

func (MemoryLocalizer) SetDefault Uses

func (l MemoryLocalizer) SetDefault(index int) bool

SetDefault changes the default language based on the "index". See `I18n#SetDefault` method for more.

Package i18n imports 15 packages (graph). Updated 2019-12-14. Refresh now. Tools for package owners.