i18n

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2018 License: MIT Imports: 7 Imported by: 0

README

Translation and Internationalization in acs-engine

The translation process in acs-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 acs-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 acs-engine API called from multiple requests at the same time.

The translation files containing resource strings are packaged into acs-engine binary using go-bindata. At runtime, the translation files are recreated on disk in the same directory as acs-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 acs-engine API from acs-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

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