Go bindings for GNU gettext, an internationalization and localization library for writing multilingual systems.
Installation should be straightforward on Linux.
Installing gettext on a Mac is a bit awkward:
brew install gettext
export CGO_LDFLAGS=-L/usr/local/opt/gettext/lib
export CGO_CPPFLAGS=-I/usr/local/opt/gettext/include
go get github.com/gosexy/gettext
Use go get
to download and install the binding:
go get github.com/gosexy/gettext
This is an example program showing the BindTextdomain
, Textdomain
and
SetLocale
bindings:
package main
import (
"fmt"
"github.com/gosexy/gettext"
)
func main() {
textDomain := "default"
gettext.BindTextdomain(textDomain, "path/to/domains")
gettext.Textdomain(textDomain)
gettext.SetLocale(gettext.LcAll, "es_MX.utf8")
fmt.Println(gettext.Gettext("Hello, world!"))
}
Set the LANGUAGE
env to the name of the language you want to use in your
program:
export LANGUAGE="es_MX.utf8"
./myapp
You can use the xgettext
command to extract strings to be translated from a
Go program:
go get github.com/gosexy/gettext/go-xgettext
go-xgettext -o outfile.pot --keyword=Gettext --keyword-plural=NGettext infile.go
This will generate a example.pot
file.
After actually translating the .pot
file, you'll have to generate .po
and
.mo
files with msginit
and msgfmt
:
msginit -l es_MX -o example.po -i example.pot
msgfmt -c -v -o example.mo example.po
Finally, move the .mo
file to an appropriate location.
mv example.mo examples/es_MX.utf8/LC_MESSAGES/example.mo
Check out the API documentation godoc.org/github.com/gosexy/gettext).
The original gettext documentation:
man 3 gettext
And here's a good tutorial on using gettext.