Updated by ledzep2
2013.9.18 support go 1.1.2
go-i18n is a set of tools that can be used to translate Go programs into multiple lanaguages.
The project is being developed against the current release of Go: https://golang.org/doc/devel/release.html#r60
Run the following commands to install the go-i18n tools.
goinstall -u github.com/ledzep2/go-i18n/src/pkg/i18n
goinstall -u github.com/ledzep2/go-i18n/src/cmd/goi18n
The i18n package provides runtime APIs for looking up translated strings. An example is provided in example/
The goi18n command provides functionality for managing the translation process.
A typical workflow looks like this:
-
Write Go code using the i18n package.
package main import ( "flag" "fmt" "github.com/ledzep2/go-i18n/src/pkg/i18n" ) var ( HelloWorld = i18n.NewMessage("Hello world!", "This message is displayed when the program begins") GoodbyeWorld = i18n.NewMessage("Goodbye world.", "This message is displayed when the program ends") ) var locale string func main() { flag.StringVar(&locale, "locale", "", "The locale to use for translated messages.") flag.Parse() i18n.SetLocale(locale) fmt.Println(HelloWorld.String()) fmt.Println(GoodbyeWorld.String()) }
-
Extract the message from the Go source files.
goi18n extract -format=json main.go > messages.json
or
goi18n extract -format=json -output=messages.json main.go
-
Get the messages translated.
-
Merge the new translations with any existing translations.
goi18n merge existing/es_ES.json new/es_ES.json
-
Format the translations into Go source files.
goi18n format es_ES.json fr_FR.json ... # generates es_ES.go fr_FR.go ...
-
Compile your program with the generated source files.