Internationalization (i18n) of Web Application by GNU gettext Tools


GNU gettext tools are great for creating i18n (web) applications. In this post, we will show how to use gettext tools, which include xgettext, msginit, msgmerge, and msgfmt to create i18n applications.

My work, Pāḷi Dictionary, is a example of i18n web application. I will show how to use gettext tools by how I use gettext tools in Pāḷi Dictionary.

Mark strings for i18n

First, mark strings you want to tranlste by _("string"). For example,

<title>Hello World</title>

after mark

<title>_("Hello World")</title>

Extract translatable strings

Next, we extract the marked strings in previous step by xgettext, which extracts translatable strings from given input files. Assume the marked strings are located in html files under app/html/ directory. Run xgettext by:

$ xgettext --no-wrap --from-code=UTF-8 --keyword=_ --output=locale/messages.pot `find app/html/ -name "*.html"`

A file named messages.pot will be created under locale/ directory. We will put all i18n-related stuff in locale/ directory.

Next, we will set charset in messages.pot. You can open your editor to edit by hand. I use sed to automate the task:

$ sed -i "s/charset=CHARSET/charset=utf-8/g" locale/messages.pot

Generate translation file for each supported language

Support we want to support three languages (English, Traditional Chinese, and Vietnamese). We use en_US locale for English, zh_TW for Traditional Chinese, and vi_VN for Vietnamese. msginit is used to generate translation file for each supported language:

$ msginit --no-wrap --no-translator --input=locale/messages.pot --locale=en_US -o locale/en_US/LC_MESSAGES/messages.po
$ msginit --no-wrap --no-translator --input=locale/messages.pot --locale=zh_TW -o locale/zh_TW/LC_MESSAGES/messages.po
$ msginit --no-wrap --no-translator --input=locale/messages.pot --locale=vi_VN -o locale/vi_VN/LC_MESSAGES/messages.po

the input is the messages.pot in previous step, and three PO files are generated and put in respective directories. Then edit these PO files to translate the strings in the files.

Update translation file for each supported language

Everytime you add or delete translatable strings in original html files, msgmerge is used to help you update the PO files for each locale. Re-generate messages.pot again by xgettext and then run msgmerge:

$ msgmerge --no-wrap --backup=none --update locale/en_US/LC_MESSAGES/messages.po locale/messages.pot
$ msgmerge --no-wrap --backup=none --update locale/zh_TW/LC_MESSAGES/messages.po locale/messages.pot
$ msgmerge --no-wrap --backup=none --update locale/vi_VN/LC_MESSAGES/messages.po locale/messages.pot

After the update, you maybe need to edit the PO files to translate the newly added strings.

Generate MO file for run-time use of web application

During the run-time of i18n application, the POT or PO files are not used. Instead we will generate MO files from PO files in previous step for run-time application use. MO files are binary message catalog. We can generate MO files by msgfmt:

msgfmt locale/en_US/LC_MESSAGES/messages.po -o locale/en_US/LC_MESSAGES/messages.mo
msgfmt locale/zh_TW/LC_MESSAGES/messages.po -o locale/zh_TW/LC_MESSAGES/messages.mo
msgfmt locale/vi_VN/LC_MESSAGES/messages.po -o locale/vi_VN/LC_MESSAGES/messages.mo

These MO files are the files we really need in our applications during run-time.

Use MO file in your application

The use of MO files are supported in different programming languages, such as Python or Go. The following posts show how to use MO files during run-time.

For Go to use gettext

Please read [7] and [10] to see how to use the PO and MO file in your Go (web) application.