i18n
Simple i18n
support that relies on standard go libraries
How to start?
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!
Lookup for a specific locale
result, err := translate.LookupWithLocale("en", "some.awesome.text")
if err != nil {
...
}
fmt.Println(result)
The program should print Hello World!
Lookup with arguments
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!
JSON Format
{
"some": {
"awesome": {
"text": "Hello World!",
"textWithArgs": "Hello %s!"
}
}
}
How to install?
go get github.com/alobaton/i18n
How to test?
$ go test ./...
Example
Here you can find an example