Documentation ¶
Index ¶
Constants ¶
const ( // VarsKey is the key for the message's variables, per locale(global) or per key (local). VarsKey = "Vars" // PluralCountKey is the key for the template's message pluralization. PluralCountKey = "PluralCount" // VarCountKeySuffix is the key suffix for the template's variable's pluralization, // e.g. HousesCount for ${Houses}. VarCountKeySuffix = "Count" // VarsKeySuffix is the key which the template message's variables // are stored with, // e.g. welcome.human.other_vars VarsKeySuffix = "_vars" )
Variables ¶
var DefaultPluralFormDecoder = func(_ context.Locale, key string) (PluralForm, bool) { if isDefaultPluralForm(key) { return pluralForm(key), true } return nil, false }
DefaultPluralFormDecoder is the default `PluralFormDecoder`. Supprots "zero", "one", "two", "other", "=x", "<x", ">x".
Functions ¶
This section is empty.
Types ¶
type Catalog ¶
type Catalog struct { Locales []*Locale // contains filtered or unexported fields }
Catalog holds the locales and the variables message storage.
func NewCatalog ¶
NewCatalog returns a new Catalog based on the registered languages and the loader options.
func (*Catalog) SetDefault ¶
SetDefault changes the default language based on the "index". See `I18n#SetDefault` method for more.
type Locale ¶
type Locale struct { // ID is the tag.String(). ID string // Options given by the Catalog Options Options // Fields set by Catalog. FuncMap template.FuncMap Printer *message.Printer // Fields set by this Load method. Messages map[string]Renderer Vars []Var // shared per-locale variables. // contains filtered or unexported fields }
Locale is the default Locale. Created by Catalog. One Locale maps to one registered and loaded language. Stores the translation variables and most importantly, the Messages (keys and their renderers).
func (*Locale) GetMessage ¶
GetMessage should return translated text based on the given "key".
func (*Locale) Language ¶
Language should return the exact languagecode of this `Locale` that the user provided on `New` function.
Same as `Tag().String()` but it's static.
type Map ¶
type Map = map[string]interface{}
Map is just an alias of the map[string]interface{} type. Just like the iris.Map one.
type Message ¶
type Message struct { Locale *Locale Key string Value string Plural bool Plurals []*PluralMessage // plural forms by order. Vars []Var }
Message is the default Renderer for translation messages. Holds the variables and the plurals of this key. Each Locale has its own list of messages.
func (*Message) AddPlural ¶
func (m *Message) AddPlural(form PluralForm, r Renderer)
AddPlural adds a plural message to the Plurals list.
func (*Message) Render ¶
Render completes the Renderer interface. It accepts arguments, which can resolve the pluralization type of the message and its variables. If the Message is wrapped by a Template then the first argument should be a map. The map key resolves to the pluralization of the message is the "PluralCount". And for variables the user should set a message key which looks like: %VAR_NAME%Count, e.g. "DogsCount" to set plural count for the "Dogs" variable, case-sensitive.
type 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.
Note: we don't accept the Context here because Tr method and template func {{ tr }} have no direct access to it.
type Options ¶
type Options struct { // Left delimiter for template messages. Left string // Right delimeter for template messages. Right string // Enable strict mode. Strict bool // Optional functions for template messages per locale. Funcs func(context.Locale) template.FuncMap // Optional function to be called when no message was found. DefaultMessageFunc MessageFunc // Customize the overall behavior of the plurazation feature. PluralFormDecoder PluralFormDecoder }
The Options of the Catalog and its Locales.
type PluralCounter ¶
type PluralCounter interface { // PluralCount returns the plural count of the message. // If returns -1 then this is not a valid plural message. PluralCount() int // VarCount should return the variable count, based on the variable name. VarCount(name string) int }
PluralCounter if completes by an input argument of a message to render, then the plural renderer will resolve the plural count and any variables' counts. This is useful when the data is not a type of Map or integers.
type PluralForm ¶
type PluralForm interface { String() string // the string is a verified plural case's raw string value. // Field for priority on which order to register the plural cases. Less(next PluralForm) bool MatchPlural(pluralCount int) bool }
A PluralForm is responsible to decode locale keys to plural forms and match plural forms based on the given pluralCount.
See `pluralForm` package-level type for a default implementation.
type PluralFormDecoder ¶
type PluralFormDecoder func(loc context.Locale, key string) (PluralForm, bool)
A PluralFormDecoder should report and return whether a specific "key" is a plural one. This function can be implemented and set on the `Options` to customize the plural forms and their behavior in general.
See the `DefaultPluralFormDecoder` package-level variable for the default implementation one.
type PluralMessage ¶
type PluralMessage struct { Form PluralForm Renderer Renderer }
PluralMessage holds the registered Form and the corresponding Renderer. It is used on the `Message.AddPlural` method.
type Template ¶
type Template struct { *Message // contains filtered or unexported fields }
Template is a Renderer which renders template messages.
func NewTemplate ¶
NewTemplate returns a new Template message based on the catalog and the base translation Message. See `Locale.Load` method.
type Var ¶
type Var struct { Name string // Variable name, e.g. Name Literal string // Its literal is ${Name} Cases []interface{} // one:...,few:...,... Format string // defaults to "%d". Argth int // 1, 2, 3... }
Var represents a message variable. The variables, like the sub messages are sorted. First: plurals (which again, are sorted) and then any custom keys. In variables, the sorting depends on the exact order the associated message uses the variables. This is extremely handy. This package requires the golang.org/x/text/message capabilities only for the variables feature, the message itself's pluralization is managed by the package.