mirror of https://github.com/knadh/go-i18n.git
87a5109554 | ||
---|---|---|
LICENSE | ||
README.md | ||
go.mod | ||
i18n.go | ||
i18n_test.go |
README.md
go-i18n
go-i18n is a tiny i18n library that enables {langKey: langValue}
style JSON language maps to be loaded and used in Go programs. It is modeled after vue-i18n, optionally enabling interoperability of the same language files in Go backends a Vue frontends. It is used by listmonk and dictpress.
Usage
Sample JSON language file
A JSON language file looks is a simple map of key: value
pairs. Singular/plural terms are represented as Singular|Plural
. _.code
and _.name
are mandatory special keys. Check listmonk translations for complex examples.
{
"_.code": "en",
"_.name": "English",
"pageTitle": "Welcome to the page",
"page": "Single page|Many pages",
"pageVars": "The page is named {name} and has {count} items"
}
i := i18n.NewFromFile("en.json")
i.T("pageTitle") // Welcome to the page
i.T("page") // Single Page
i.S("page") // Single Page
i.P("page") // Many pages
i.Tc("page", 1) // Single Page (second param is a number. 1 is singular)
i.Tc("page", 2) // Many pages (>= 1 is plural)
i.Ts("pageVars", "name", "Foo", "count", "123") // The page is named Foo and has 123 items
Licensed under the MIT license.