Simple and easy to use i18n (Internationalization and localization) engine written in Go, used for translating locale strings. Use with go generate or on the CLI. Currently supports JSON, YAML, TOML and CSV translation files
go get -u github.com/touchvas/localizationsThis package aims to be as simple and easy to use as possible. It also takes inspiration from popular localization libraries/packages in other languages - so makes it easier to reason about coming from other languages and frameworks.
The suggested way to use go-localize is to use go generate. For example, take the following directory structure:
goapp
└── localizations_src
├── en
│ └── messages.yaml
└── es
├── customer
│ └── messages.json
└── messages.json
Example of JSON translation file:
{
"hello": "Hola",
"how_are_you": "¿Cómo estás?",
"whats_your_name": "¿Cuál es tu nombre?",
"hello_my_name_is": "Hola, mi nombre es {{.name}}"
}Example of YAML translation file:
hello: hello
how_are_you: How are you?
whats_your_name: "What's your name?"
hello_my_name_is: Hello my name is {{.name}}
hello_firstname_lastname: Hello {{.firstname}} {{.lastname}}Example of CSV translation file:
hello, hello
how_are_you, How are you?
Example of TOML translation file:
hello = "hello"
how_are_you = "How are you?"Now you'll be able to use the localization like so:
localizer := Localizer{}
localizer.SetSource("localizations_src")
localizer.SetDefaultLocale("en", "en")
localizer.SetupLocalization()
println(localizer.Translate("response.messages.verification_code_limit_reached")) // Wait for a few minutes to resend verification code againWith en being the locale and es being the fallback. The localization keys are worked out using folder structure, eg:
en/customer/messages.json with the contents being:
{
"hello": "hello customer!"
}You'll be able to access this using the key: customer.messages.hello.
It is suggested to instead of using hardcoded locale keys i.e. en to use the language keys included in key, i.e: language.BritishEnglish.String()
which is en-GB
Take this replacement string for example:
password_reset_code: "Your Account verification code is {{.code}}"To then replace code variable, you can use
something like this:
localizer := Localizer{}
localizer.SetSource("localizations_src")
localizer.SetDefaultLocale("en", "en")
localizer.SetupLocalization()
replacements := map[string]string{
"code": "1234",
}
println(localizer.Translate("password_reset_code", replacements))You can define the locale and fallbacks using:
localizer.SetDefaultLocale("en", "en")Where en is the locale and es is the fallback. If no translation key-value is
found then the key will be returned. For example
println(l.Get("key_doesnt_exist")) //"key_doesnt_exist" will be printedWe currently support JSON and YAML translation files. Please suggest missing file type using issues or pull requests.