go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.
- Supports pluralized strings for all 200+ languages in the Unicode Common Locale Data Repository (CLDR).
- Code and tests are automatically generated from CLDR data
- Supports strings with named variables using text/template syntax.
- Translation files are simple JSON or YAML.
- Documented and tested!
The i18n package provides runtime APIs for fetching translated strings.
The goi18n command provides functionality for managing the translation process.
Make sure you have setup GOPATH.
go get -u github.com/Compufreak345/go-i18n/goi18n
goi18n -help
A typical workflow looks like this:
-
Add a new string to your source code.
T("settings_title")
-
Add the string to en-US.all.json
[ { "id": "settings_title", "translation": "Settings" } ]
-
Run goi18n
goi18n path/to/*.all.json
-
Send
path/to/*.untranslated.json
to get translated. -
Run goi18n again to merge the translations
goi18n path/to/*.all.json path/to/*.untranslated.json
A translation file stores translated and untranslated strings.
Example:
[
{
"id": "d_days",
"translation": {
"one": "{{.Count}} day",
"other": "{{.Count}} days"
}
},
{
"id": "my_height_in_meters",
"translation": {
"one": "I am {{.Count}} meter tall.",
"other": "I am {{.Count}} meters tall."
}
},
{
"id": "person_greeting",
"translation": "Hello {{.Person}}"
},
{
"id": "person_unread_email_count",
"translation": {
"one": "{{.Person}} has {{.Count}} unread email.",
"other": "{{.Person}} has {{.Count}} unread emails."
}
},
{
"id": "person_unread_email_count_timeframe",
"translation": {
"one": "{{.Person}} has {{.Count}} unread email in the past {{.Timeframe}}.",
"other": "{{.Person}} has {{.Count}} unread emails in the past {{.Timeframe}}."
}
},
{
"id": "program_greeting",
"translation": "Hello world"
},
{
"id": "your_unread_email_count",
"translation": {
"one": "You have {{.Count}} unread email.",
"other": "You have {{.Count}} unread emails."
}
}
]
If you would like to submit a pull request, please
- Write tests
- Format code with goimports.
- Read the common code review comments.
go-i18n is available under the MIT license. See the LICENSE file for more info.