i18n

package
v0.35.6 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: MIT Imports: 7 Imported by: 112

README

Translation and Internationalization in aks-engine

The translation process in aks-engine borrows some approach from Kubernetes translation. The strings to be localized are mainly the error message strings under pkg directory. Those error message strings are also consumed by other components such as ACS RP.

The localization in aks-engine depends on github.com/leonelquinteros/gotext, a GNU gettext utility for Go. The package supports concurrency in translating strings in multiple goroutines, e.g., the same aks-engine API called from multiple requests at the same time.

The translation files containing resource strings are packaged into aks-engine binary using go-bindata. At runtime, the translation files are recreated on disk in the same directory as aks-engine binary, for gotext to load.

How to add new string to be localized

When a new error string needs to be localized, it needs to use translation function Errorf in pkg/i18n/i18n.go. The locale is passed to aks-engine API from aks-engine command or any other component calls it. If the locale is nil, then it falls back to en-us as in the Go source file.

Once the Go source file is modified, scripts/update-translation.sh needs to be run to extract the resource strings. The script generates PO file according to the locale specified. An example of running the script is:

scripts/update-translation.sh -l en_US -p

poedit is a common tool for translation. After the PO files are translated, MO files can be generated by either poedit or tool such as msgfmt. Both PO file and MO file need to be placed under translations/<locale>/LC_MESSAGES/.

How to add a new language

When the resource strings need to be translated into a new language, the following steps are needed:

1. Use scripts/update-translation.sh with the language to genreate PO file
2. Translate the PO file and generate MO file.
3. Place PO file and MO file under `translations/<language>/LC_MESSAGES/` and commit

Documentation

Overview

Package i18n contains localized strings for user facing messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLanguage

func GetLanguage() string

GetLanguage queries the program's current locale.

func Initialize

func Initialize(locale *gotext.Locale) error

Initialize is the translation initialization function shared by the main program and package.

func LoadTranslations

func LoadTranslations() (*gotext.Locale, error)

LoadTranslations loads translation files and sets the locale to the system locale. It should be called by the main program.

func SetLanguage

func SetLanguage(language string)

SetLanguage sets the program's current locale. If the language is not supported, then the default locale is used.

Types

type Translator

type Translator struct {
	Locale *gotext.Locale
}

Translator is a wrapper over gotext's Locale and provides interface to translate text string and produce translated error

func (*Translator) Errorf

func (t *Translator) Errorf(msgid string, vars ...interface{}) error

Errorf produces an error with a translated error string.

func (*Translator) NErrorf

func (t *Translator) NErrorf(msgid, msgidPlural string, n int, vars ...interface{}) error

NErrorf produces an error with a translated error string in the appropriate plural form.

func (*Translator) NT

func (t *Translator) NT(msgid, msgidPlural string, n int, vars ...interface{}) string

NT translates a text string into the appropriate plural form, based on GNU's gettext library.

func (*Translator) T

func (t *Translator) T(msgid string, vars ...interface{}) string

T translates a text string, based on GNU's gettext library.

Jump to

Keyboard shortcuts

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