Documentation ¶
Index ¶
Constants ¶
const ( // Prefix marks the begin of a placeholder being used for i18n interpolation Prefix = "{{" // Suffix marks the end of a placeholder being used for i18n interpolation Suffix = "}}" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Intermediate ¶
type Intermediate string
Intermediate is a named placeholder within a translation which may be replaced by a context-dependent value
func (Intermediate) Format ¶
func (i Intermediate) Format() string
Format returns the intermediate in i18next notation e.g {{hello}}
type Key ¶
type Key string
Key is a unique identifier for a translation. A Key comprises multiple fragments seperated by a "."
type Translation ¶
type Translation struct { Message string Intermediates []Intermediate }
Translation defines the translated message for a given key which contains context-dependent intermediates as placeholder
type Translations ¶
type Translations struct {
// contains filtered or unexported fields
}
Translations are a collection of language translations represented by key value structure Upon translating it will attempt to retrieve the target language from a given source function, rolling back to the default language on failure. The translations are loaded during intialization from a defined directory
func NewTranslations ¶
func NewTranslations(directory string, defaultLanguage string) Translations
NewTranslations initializes a new translations object
func (Translations) GenerateDefaultTranslate ¶
func (trl Translations) GenerateDefaultTranslate() func(k string, params ...interface{}) (template.HTML, error)
GenerateDefaultTranslate returns a translate function for the default language.
func (Translations) GenerateTranslate ¶
func (trl Translations) GenerateTranslate(targetLang string) func(k string, params ...interface{}) (template.HTML, error)
GenerateTranslate returns a translate function for a specific language that translates a given key, interpolating the passed parameter values assuming the intermediates match the parameter keys injectively.
func (Translations) Load ¶
func (trl Translations) Load() (Translations, error)
Load processes all language files of the defined directory and parses it into a kv structure keyed by the language code. It fetches all files in the directory using their base name as language identifier. The files are expected to be of JSON format. Load allows nested translations in the file meaning the key must not be denoted in a single form but can be splitted along the nesting levels (it follows the i18next standard). It will recursively summarize these keys into a full one, saving each value under the appropriate full key and return a flattened structure.