Simple i18n
support that relies on standard go libraries
The i18n
package mainly includes a set of methods for managing the data. Start by creating a en.json
file.
{
"some": {
"awesome": {
"text": "Hello World!"
}
}
}
Create a new Translate
instance as follows.
var err error
translate, err = i18n.NewTranslate().BindPath("./example").BindMainLocale("en").Init()
if err != nil {
...
}
Once you setup the i18n instance, you should be able to lookup for messages.
result, err := translate.Lookup("some.awesome.text")
if err != nil {
...
}
fmt.Println(result)
The program should print Hello World!
result, err := translate.LookupWithLocale("en", "some.awesome.text")
if err != nil {
...
}
fmt.Println(result)
The program should print Hello World!
i18n
relies on fmt.Sprintf(...)
to apply replacements, so you should be able to use it as follows.
Your .json
file should look like this.
{
"some": {
"awesome": {
"textWithArgs": "Hello %s!"
}
}
}
Lookup for messages like this.
result, err := translate.Lookup("some.awesome.textWithArgs", "i18n")
if err != nil {
...
}
fmt.Println(result)
The program should print Hello i18n!
{
"some": {
"awesome": {
"text": "Hello World!",
"textWithArgs": "Hello %s!"
}
}
}
go get github.com/alobaton/i18n
$ go test ./...
Here you can find an example