Implementation by J.Jørgen von Bargen was sent to lua-l mailing list as public domain.
$ luarocks install mo
-- load this module
mo = require 'mo'
-- load .mo file with translations
-- returns translating function on success
-- returns nil, 'error message' on failure
_ = mo('russian.mo')
-- translate from English into Russian
print(_("hello")) -- prints "привет"
-- the function returns its argument if it can't translate it
print(_("unknown phrase")) -- prints "unknown phrase"
You need busted and gettext to run unit tests.
$ busted
Let us translate some Lua project into Russian.
$ xgettext -p ru -s -j --language=Lua *.lua --from-code utf-8
This command creates (or adds to) file ru/messages.po
.
It finds texts like _("text")
in source files and adds them
to .po
file.
There are a lot of programs to translate .po
file.
One of them is poedit.
To compile .po
to .mo
, run the following command:
$ msgfmt ru/messages.po -o ru/messages.mo
This module is used to parse .mo
file from Lua.