Go bindings for GNU gettext, an internationalization and localization library for writing multilingual systems.
The GNU C library. If you're using GNU/Linux, FreeBSD or OSX you should already have it.
Use go get to download and install the binding:
go get github.com/gosexy/gettextpackage main
import (
"github.com/gosexy/gettext"
"fmt"
"os"
)
func main() {
gettext.BindTextdomain("example", ".")
gettext.Textdomain("example")
os.Setenv("LANGUAGE", "es_MX.utf8")
gettext.SetLocale(gettext.LC_ALL, "")
fmt.Println(gettext.Gettext("Hello, world!"))
}You can use os.Setenv to set the LANGUAGE environment variable or set it
on a terminal:
export LANGUAGE="es_MX.utf8"
./gettext-programNote that xgettext does not officially support Go syntax yet, however, you
can generate a valid .pot file by forcing xgettest to use the C++
syntax:
xgettext -d example -s gettext_test.go -o example.pot -L c++ -i \
--keyword=NGettext:1,2 --keyword=GettextThis will generate a example.pot file.
After translating the .pot file, you must generate .po and .mo files and
remember to set the UTF-8 charset.
msginit -l es_MX -o example.po -i example.pot
msgfmt -c -v -o example.mo example.poFinally, move the .mo file to an appropriate location.
mv example.mo examples/es_MX.utf8/LC_MESSAGES/example.moYou can read gosexy/gettext documentation from a terminal
go doc github.com/gosexy/gettextOr you can browse it online.
The original gettext documentation could be very useful as well:
man 3 gettextHere's another good tutorial on using gettext.