i18n

package
v0.0.0-...-4e18fe8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package i18n provides abstractions for field-based localization libraries.

Index

Constants

This section is empty.

Variables

View Source
var EmptyConfig = new(Config)

EmptyConfig is the Config used as a replacement for an empty string.

View Source
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.

View Source
var ErrNilConfig = errors.New("i18n: cannot translate nil Config")

ErrNilConfig is the error returned if a nil config is given to Localizer.Localize.

View Source
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

func NewFallbackConfig(term Term, fallback string) *Config

NewFallbackConfig is a utility function that can be used to inline term-only Configs with a fallback.

func NewStaticConfig

func NewStaticConfig(s string) *Config

NewStaticConfig returns a static *Config that always produces s, even if '{{' and '}}' are used.

func NewTermConfig

func NewTermConfig(term Term) *Config

NewTermConfig is a utility function that can be used to inline term-only Configs.

func (Config) WithPlaceholders

func (c Config) WithPlaceholders(placeholders interface{}) *Config

WithPlaceholders returns a copy of the Config with the passed placeholders set.

func (Config) WithPlural

func (c Config) WithPlural(plural interface{}) *Config

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

type Func func(term Term, placeholders map[string]interface{}, plural interface{}) (string, error)

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

func NewLocalizer(lang string, f Func) *Localizer

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) Localize

func (l *Localizer) Localize(c *Config) (string, error)

Localize generates a localized message using the passed config.

func (*Localizer) LocalizeTerm

func (l *Localizer) LocalizeTerm(term Term) (string, error)

LocalizeTerm is short for

l.Localize(&i18n.Config{Term: term})

func (*Localizer) MustLocalize

func (l *Localizer) MustLocalize(c *Config) string

MustLocalize is the same as Localize, but it panics if there is an error.

func (*Localizer) MustLocalizeTerm

func (l *Localizer) MustLocalizeTerm(term Term) string

MustLocalizeTerm is the same as LocalizeTerm, but it panics if there is an error.

func (*Localizer) WithPlaceholder

func (l *Localizer) WithPlaceholder(key string, val interface{})

WithPlaceholder adds the passed default placeholder to the Localizer.

func (*Localizer) WithPlaceholders

func (l *Localizer) WithPlaceholders(p map[string]interface{})

WithPlaceholders adds the passed default placeholders to the Localizer.

type Term

type Term string

Term is a unique identifier of a localized message.

func (Term) AsConfig

func (t Term) AsConfig() *Config

AsConfig wraps the term in a Config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL