Internationalisation with gettext

One of my projects for this week is interationalizing the tool spot. spot is coded in perl, but it's useful to prototype with a C version. You can find resources on the gnuversion of gettext and the Solaris version. There's also some helpful tutorials. There's also some examples, including a perl example.

Anyway, assume that the application initially looks like:

#include <stdio.h>
int main (int argc, char \*argv[])
{
  printf("test\\n");
}

The call to gettext will return a translated version of the string "test", but there's a couple of other calls that need to be performed to set things up.

The call setlocale will set the locale (or language, currency etc.) that the application will use. The call takes two parameters, the first is the item that should be translated - we want to translate only the messages, and the second is the language to translate into (if this is blank then the locale is obtained from the system setting).

The call to bindtextdomain takes a name of a domain, and then binds this to a directory where the translations will reside.

The call to textdomain will use the binding for an existing domain to look up the translations.

Probably the best way of seeing this is to look at the modified source:

#include <stdio.h>
#include <locale.h>
#include <libintl.h>


int main (int argc, char \*argv[])
{
  setlocale(LC_MESSAGES, "fr");
  bindtextdomain("EX", "/export/home/test/locale");
  textdomain("EX");
 
  printf(gettext("test\\n"));
  return 0;
}

The call to setlocale requests that the code use the "fr" locale. The call to bindtextdomain tells the application to look in the root directory /export/home/test/locale/ for the translations.

The program can now be compiled:

$ cc test.c

However, there's no message catalogue to provide the translations. So the next step is to identify the text which requires translation. The command to do this is xgettext:

$ xgettext test.c

This command generates a file messages.po which contains all the messages that need translation:

$ more messages.po
domain "messages"
# File:m.c, line:10, textdomain("EX");
msgid  "test\\n"
msgstr

The messages in the file can then be translated:

$ more messages.po
domain "messages"
# File:m.c, line:10, textdomain("EX");
msgid  "test\\n"
msgstr "notest\\n"

Once the message file exists it needs to be converted into a message file that gettext can read, this is performed by the command msgfmt:

$ msgfmt -o /export/home/test/locale/fr/LC_MESSAGES/EX.mo messages.po

The msgfmt command has an optional -o parameter to specify the location of the output file. The place where the file needs to reside and the name of the file are determined by the directory passed to bindtextdomain, the locale, and the name given to the domain.

When the program is run the output is:

$ a.out
notest

Which shows that although the text was "test" the output from the program is the localised "notest".

Comments:

Another useful gettext routine is bind_textdomain_codeset(3).

E.g.,
bind_textdomain_codeset("EX", "UTF-8"), so that you could just deliver one message file in UTF-8 encoding to fr.UTF-8 (while you need create several symbol links in other legacy fr locales).

For the case in your above example, gettext(3C) would convert the localized message from utf-8 to iso8859-1.

Posted by Yong Sun on May 27, 2008 at 06:04 PM PDT #

Forgot to mention, the gettext(3) routine on linux (more specifically glibc) does not require you to create symbol links in different locale variants, and the message files are just delivered to /usr/share/locale/fr_FR/LC_MESSAGES.

Posted by Yong Sun on May 27, 2008 at 06:24 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

Darryl Gove is a senior engineer in the Solaris Studio team, working on optimising applications and benchmarks for current and future processors. He is also the author of the books:
Multicore Application Programming
Solaris Application Programming
The Developer's Edge

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
5
6
8
9
10
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
The Developer's Edge
Solaris Application Programming
Publications
Webcasts
Presentations
OpenSPARC Book
Multicore Application Programming
Docs