Intlize

Tutorial

C source

At first we write the source code for a sample program, that looks like this:

#include <stdio.h>

int main(int argc, char **argv)
{
puts("Hello!");
puts("Good bye!");
return 0;
}

Save this file as hello.c.

To add internationalization, we copy the C runtime files to the directory where hello.c is.

We modify the source like this:

#include <intlize/i5e.h>
#include <stdio.h>
#include <locale.h>
#include <langinfo.h>

#ifndef _
#define _(A, B) (i5e_getmsg(&cat, (B)))
#endif

int main(int argc, char **argv)
{
i5e_catalog_t cat = i5e_create_catalog();
char *lang = setlocale(LC_ALL, "");
if (lang == NULL) lang = "C";
int err = i5e_open(cat, lang,
"/usr/local/share/locale/%L/LC_MESSAGES/hello.msg", nl_langinfo(CODESET));
if (err) {
fputs(i5e_error_text(err), stderr);
i5e_free_catalog(cat);
return 1;
}
puts(_("Hello!", 0));
puts(_("Good bye!", 0));
i5e_free_catalog(cat);
return 0;
}

Extracting the translatable messages

At the current stage, intlize relies on GNU xgettext to extract the messages. I suggest that you create a subdirectory po where translation files go. cd to this directory and invoke gettext with xgettext -k_ -o hello.pot ../hello.c. Please refer to the xgettext manual for additional information.

You now have a file hello.pot in the po subdirectory. The file looks like:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2007-09-23 22:01+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: ../hello.cpp:18
msgid "Hello!"
msgstr ""

#: ../hello.cpp:19
msgid "Good bye!"
msgstr ""

In order to create a translation for the program, we copy this file to hello.LANG where LANG is the language code of the translation. To create a german translation, we copy to hello.de.

There are many tools available to process these files. But even a simple text editor will do in this case. Of the header, only the placeholders PACKAGE VERSION and CHARSET are evaluated by intlize.

We edit hello.de like this:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: hello 0.1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2007-09-23 22:01+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"

#: ../hello.cpp:18
msgid "Hello!"
msgstr "Hallo!"

#: ../hello.cpp:19
msgid "Good bye!"
msgstr "Tschüss!"

Intlize

Now we can invoke intlize. intlize hello.de will create a subdirectory de with hello.msg in it. Since we do need a default translation, we use intlize -C hello.de; this creates the subdirectories C and de with each holding a hello.msg.

Intlize will modify the source as follows:

...
puts(_("Hello!", 1));
puts(_("Good bye!", 2));
...

The program can now be compiled. C/hello.msg needs to be copied to /usr/local/share/locale/C/LC_MESSAGES/ and de/hello.msg to /usr/local/share/locale/de/LC_MESSAGES/,since we gave that path to i5e_open.

Get Intlize at SourceForge.net. Fast, secure and Free Open Source software downloads

home

project summary

news

cvs

description

tutorial

manual

download