Lightweight yet powerful continuous localization solution for Go, based on Serge and Plurr.
-
Provides solution for both web-based and desktop applications.
-
Can be used with different resource file formats, thanks to Serge. Currently offers the support for JSON resource files and string maps in the native .go format. Both formats support developer comments for translators to get extra context.
-
Supports named placeholders, plurals and conditionals via Plurr-formatted strings (see the interactive demo).
-
Support for translation and formatting functions in
html/template
andtext/template
. -
For web applications, an ability to auto-detect the best language from the browser, and to force the language using a cookie or a query string parameter.
-
Comes with ready-to-use Serge configuration files to allow for seamless continuous localization process (see examples).
-
Supports pseudotranslations out of the box. Pseudotranslation is a way to "translate" your application by applying some algorithm to each source string; it is useful to test if your application looks good with other locales without waiting for actual translations.
-
The code is split into small packages to minimize external dependencies.
There are two example projects that showcase approaches to localizing web applications and desktop applications. Below is a condensed example to illustrate the typical usage.
strings.go
(master resource file):
package main
func init() {
locpool.Resources["en"] = map[string]string{
// Page title
"Hello": "Hello!",
// {N} is the number of messages
"YouHaveNMessages": "You have {N} {N_PLURAL:message|messages}",
}
}
strings-ru.go
(localized resource file):
package main
func init() {
locpool.Resources["ru"] = map[string]string{
// Page title
"Hello": "Здравствуйте!",
// {N} is the number of messages
"YouHaveNMessages": "У вас {N} {N_PLURAL:сообщение|сообщения|сообщений}",
}
}
Code:
package main
import (
"github.com/iafan/Plurr/go/plurr"
"github.com/iafan/go-l10n/loc"
)
// Create a global localization pool which will be populated
// by resource files; use English as a default (fallback) language
var locpool = loc.NewPool("en")
func main() {
// Get Russian localization context
lc := locpool.GetContext("ru")
// Get translation by key name:
name := lc.Tr("Hello")
// get translation by key name, then format it using Plurr:
hello := lc.Format("YouHaveNMessages", plurr.Params{"N": 5})
...
}