i18n

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 9, 2019 License: MIT Imports: 10 Imported by: 0

README

Go-I18N

MIT License Go Doc

Overview

Go-i18n is a internationalization library for golang using the i18next json format.

Features

  • named intermediates

Installation

To install this package, run:

go get github.com/nimbusec-oss/go-i18n

Documentation

Load translations

t, err := i18n.NewTranslations("<dir>", "en").Load()

Add to FuncMap

template.FuncMap{"T":t.GenerateDefaultTranslate(),}

Use in template

{{ T "<translationKey>" }}

Documentation

Index

Constants

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

func (Key) Append

func (k Key) Append(s string) Key

Append takes a fragment s and appends it to the current key. If the current key is empty, s is the root fragment

func (Key) String

func (k Key) String() string

type Language

type Language string

Language is the code abbreviation of language

func (Language) Valid

func (lang Language) Valid() bool

Valid verifies the validity of a language allowing only two letter codes

type Store

type Store map[Key]Translation

Store is a map where a key maps to a translation

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.

Jump to

Keyboard shortcuts

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