Documentation
¶
Overview ¶
Package i18n provides abstractions for field-based localization libraries.
Index ¶
- Variables
- type Config
- type Fallback
- type Func
- type LocalizationError
- type Localizer
- func (l *Localizer) Localize(c *Config) (string, error)
- func (l *Localizer) LocalizeTerm(term Term) (string, error)
- func (l *Localizer) MustLocalize(c *Config) string
- func (l *Localizer) MustLocalizeTerm(term Term) string
- func (l *Localizer) WithPlaceholder(key string, val interface{})
- func (l *Localizer) WithPlaceholders(p map[string]interface{})
- type Term
Constants ¶
This section is empty.
Variables ¶
var EmptyConfig = new(Config)
EmptyConfig is the Config used as a replacement for an empty string.
var ErrNaN = errors.New("i18n: the used plural value is not a number")
ErrNaN gets returned, if a plural value is neither a number type nor a string containing a number.
var ErrNilConfig = errors.New("i18n: cannot translate nil Config")
ErrNilConfig is the error returned if a nil config is given to Localizer.Localize.
var ErrPlaceholders = errors.New("i18n: placeholders must be of type map[string]string or struct")
ErrPlaceholders gets returned, if the type of Placeholders is invalid.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Term is the key of the translation. // // Term may be empty in which case Fallback will always be used. Term Term // Placeholders contains the placeholder data. // This can either be a map[string]string or a struct. // // Structs // // If you use a struct the Localizer will to convert the // name of the fields to snake_case. // However, if you want to use a custom name for the keys, you // can use the `i18n:"myname"` struct tag. Placeholders interface{} // Plural is a number or a string containing such, that is used to // identify if the message should be pluralized or not. // // If nil, the other message should be used. Plural interface{} // Fallback is the fallback used if the Func is nil or the // Func returned an error. Fallback Fallback }
Config is a data struct that contains all information needed to create a localized message. However, Configs don't necessarily produce localized text: If the Term is empty, or the Func returns with an error, the Config will resort to its Fallback.
func NewFallbackConfig ¶
NewFallbackConfig is a utility function that can be used to inline term-only Configs with a fallback.
func NewStaticConfig ¶
NewStaticConfig returns a static *Config that always produces s, even if '{{' and '}}' are used.
func NewTermConfig ¶
NewTermConfig is a utility function that can be used to inline term-only Configs.
func (Config) WithPlaceholders ¶
WithPlaceholders returns a copy of the Config with the passed placeholders set.
func (Config) WithPlural ¶
WithPlural returns a copy of the Config with the passed plural set.
type Fallback ¶
type Fallback struct { // One is the singular form of the fallback message, if there is any. One string // Other is the plural form of the fallback message. // This is also the default form, meaning if no pluralization is needed // this field should be used. Other string // contains filtered or unexported fields }
Fallback is the English fallback used if a translation is not available. The message is created using go's text/template system. `{{` and `}}` are used as delimiters, respectively.
type Func ¶
Func is the function used to translate to a specific language.
term is the unique id of the translation.
placeholders is a map with the filled placeholders, or, if there are no placeholders, a nil map.
plural is the interface{} defining the plural. Valid plural types are number types or strings containing a number. If plural is nil, Other should be used.
If the Func returns an error, the fallback translation will be used.
type LocalizationError ¶
type LocalizationError struct { Term Term // contains filtered or unexported fields }
LocalizationError gets returned if the Localizer is unable to produce a translation with the data available, i.e. if neither the underlying Func, nor the Fallback return a non-error value.
func (*LocalizationError) Error ¶
func (e *LocalizationError) Error() string
Error generates a error message.
func (*LocalizationError) Is ¶
func (e *LocalizationError) Is(target error) bool
Is checks if the error matches the passed error.
func (*LocalizationError) StackTrace ¶
func (e *LocalizationError) StackTrace() errorutil.StackTrace
type Localizer ¶
type Localizer struct { // Lang is the language the Localizer is translating to. // This does not account for possible fallbacks used by either the Func // itself or the Fallback field of a Config. // // It is unique to every language and dialect. // // If Lang is empty, the localizer is a fallback localizer. Lang string // contains filtered or unexported fields }
Localizer is a translator for a specific language. It provides multiple utility functions and wraps a Func.
The zero value of a Localizer is a fallback localizer.
func NewFallbackLocalizer ¶
func NewFallbackLocalizer() *Localizer
NewFallbackLocalizer creates a new *Localizer that always uses the fallback messages.
func NewLocalizer ¶
NewLocalizer creates a new Localizer for the passed language that generates text using the passed Func.
lang must be unique for every language and dialect used.
func (*Localizer) MustLocalize ¶
MustLocalize is the same as Localize, but it panics if there is an error.
func (*Localizer) MustLocalizeTerm ¶
MustLocalizeTerm is the same as LocalizeTerm, but it panics if there is an error.
func (*Localizer) WithPlaceholder ¶
WithPlaceholder adds the passed default placeholder to the Localizer.
func (*Localizer) WithPlaceholders ¶
WithPlaceholders adds the passed default placeholders to the Localizer.