Documentation ¶
Overview ¶
Package i18n provides functionality to retrieve strings from a messages catalog based on a key (string ID).
The messages catalog is loaded from a file containing all of the strings for a given language. Here is an example:
English
File: messages/en greeting=hello thanks=thank you
Spanish
File: messages/es greeting=hola thanks=gracias
To use the translation functionality, call LoadLanguage with the desired messages file for the local language. Replace uses of string literals with a call to T and a given string ID. If the string ID is not present in the loaded messages file, the string ID will be returned as the default.
Once initialized, Printer can also be used directly with fmt-like print functions. This can be used to include format strings for localization as below:
val := Printer.Sprintf("You know nothing %s", "Jon Snow")
The corresponding messages catalogs would be:
File: messages/en You know nothing %s=You know nothing %s File: messages/es You know nothing %s=No sabes nada %s
Packaging message catalogs ¶
Instead of loading a messages file from an on disk file, it is possible to load it from a byte array that has been included with the source.
For an executable's messages files in the messages directory, use https://github.com/jteeuwen/go-bindata to convert these files to Go source code. This MUST be done whenever any file in messages is added or edited.
go-bindata -o messages.go messages
Use the messages by recovering the byte[] corresponding to the file in the messages directory and loading it.
data, err := Asset("messages/en") i18n.LoadLanguageBytes(language.English, data)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultLang = language.English
DefaultLang is the default langugage, set to English
var Printer *message.Printer
Printer is the message printer used by T to obtain a translated value.
Functions ¶
func LoadLanguage ¶
LoadLanguage sets the package level Printer after loading the desired language file from the path messagesFile.
func LoadLanguageBytes ¶
LoadLanguageBytes sets the package level Printer after loading the desired language data from a byte array
Types ¶
This section is empty.