i18n

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

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

View Source
var DefaultLang = language.English

DefaultLang is the default langugage, set to English

View Source
var Printer *message.Printer

Printer is the message printer used by T to obtain a translated value.

Functions

func LoadLanguage

func LoadLanguage(lang language.Tag, messagesFile string) error

LoadLanguage sets the package level Printer after loading the desired language file from the path messagesFile.

func LoadLanguageBytes

func LoadLanguageBytes(lang language.Tag, messagesData []byte) error

LoadLanguageBytes sets the package level Printer after loading the desired language data from a byte array

func T

func T(stringID string) string

T (Translate) takes the message key stringID and returns the string value that the key maps to in the loaded messages file. If the key is not found, stringID is returned as the default value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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