

Internationalization (i18n) of Web Application by GNU gettext Tools
source link: https://siongui.github.io/2016/01/07/i18n-web-application-by-gnu-gettext-tools/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK