To install the package, run:
go get github.com/go-zoox/i18n
package main
import (
"context"
"github.com/go-zoox/i18n"
)
func main() {
i := i18n.New()
err := i.Load(func (lang string) (map[string][string]string, error) {
// loads locales
return map[string][string]string{
"en-US": {
"product": "product",
"design": "design",
"frontend": "frontend",
"backend": "backend",
"test": "test",
"who am i": "who am i",
"i am {name}": "i am {name}",
"where is the {place.name} ?": "where is the {place.name} ?",
},
"zh-CN": {
"product": "产品",
"design": "设计",
"frontend": "前端",
"backend": "后端",
"test": "测试",
"who am i": "我是谁",
"i am {name}": "我是{name}",
"where is the {place.name} ?": "{place.name}在哪里 ?",
},
}, nil
})
if err != nil {
panic(err)
}
translation := i.T("en-US", "product")
fmt.Println(translation) // product
}
package main
import (
"context"
"github.com/go-zoox/i18n"
)
// The directory structure should be like this:
// lang/
//
// en-US.json
// zh-CN.json
// en-US.yaml
// en-US.toml
// en-US.ini
// ...
func main() {
i := i18n.New()
err := i.LoadFromDir("./lang")
if err != nil {
panic(err)
}
translation := i.T("en-US", "product")
fmt.Println(translation) // product
}
package main
import (
"context"
"github.com/go-zoox/i18n"
)
func main() {
i := i18n.New()
err := i.LoadFromURL("https://raw.githubusercontent.com/go-zoox/i18n/master/tests/locales.json")
if err != nil {
panic(err)
}
translation := i.T("en-US", "product")
fmt.Println(translation) // product
}
GoZoox is released under the MIT License.