Erlang Central

Gettext - An internationalization package.

Revision as of 12:54, 17 June 2007 by Joonho choi (Talk | contribs)






The gettext application makes it possible to internationalize your application. For example, if you have a Web application where you want to present information in different languages, you can accomplish that with the gettext application.

The name gettext comes from the GNU package with the same name. Note however that the only thing they have in common is the format of the PO-files, i.e the files containing the text that can be translated. A PO file contains the Original String and the Translated String.

Example of a PO entry generated by gettext:

 #: esmb_gettext.erl:13 
 msgid ""
 "Hello World"
 msgstr ""
 "Hej Värld"

The string(s) following the msgid tag is the Key, i.e the Original String. The string(s) following the msgstr tag is the Value, i.e the Translated String that will be presented in place of the Original String.

Using gettext you can create an initial PO-file containing all the strings of your application that should be possible to translate. By translating the strings into some other language and loading the new PO-file into the gettext DB you can adapt your application for different languages.

Note that the very first entry of the PO file is a bit special since it contains meta-information. Especially important is the charset information. It is important that you set this right when doing a translation. If you store the strings in utf-8 format then put that info in the PO-file.

Example of the charset info in the PO-file:

 ...removed some lines here...
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=utf-8\n"
 "Content-Transfer-Encoding: 8bit\n"

Installing gettext

The easiest is to install it using erlmerge. Just run the command: erlmerge -i gettext (as root if your Erlang installation is done as root).

If you are not using erlmerge (why not?). The you will find gettext in the jungerl repository of sourceforge.

Importing from jungerl

An alternative to use erlmerge is to import gettext into your own source code repository. Here follows a suggestion for how to do this.

First, if you have write access to jungerls CVS repository, then set a tag in jungerl to make it easier for you to diff any future changes:

 cd .../lib/gettext
 cvs tag gettext-2006-July-21

Next, create another directory where you export gettext from your tag:

 mkdir aaa
 cd aaa
 cvs -z3 -d ${CVSROOT} export -r gettext-2006-July-21 jungerl
 cd jungerl/lib/gettext

Now, import the code into your own source code repository:

 cvs -d ${MYCVSROOT} import ${MYPROJ}/lib/gettext jungerl gettext-2006-July-21

You're done !

If you don't have write access to jungerl then you have to import without setting a tag. Just checkout jungerl anonymously, remove the CVS dirs, and import.

 cvs -z3 -d \
    co -d ${HOME}/jungerl jungerl
 cd ${HOME}/jungerl/lib/gettext
 rm -rf CVS */CVS
 cvs -d ${MYCVSROOT} import ${MYPROJ}/lib/gettext jungerl ${TODAYS-DATE}

If you add the ${TODAYS-DATE} in the import comment you should be able to diff against future jungerl changes using the date information.

Using gettext

In your Erlang or Yaws file, whenever you have a string that should be possible to present in a different language, you wrap it with one of the gettext macros ?TXT/1 or ?TXT2/1.

Example of how to use the TXT2 macro:

 hello(LangCode) -