Intlize

Tutorial

C source with catgets support

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 file to the directory where hello.c is.

We modify the source like this:

#include "i5ecg.h"
#include <stdio.h>
#include <errno.h>

#ifndef _
#define _(A, B) (i5ecg_getmsg(cat, (A), (B)))
#endif


int main(int argc, char **argv)
{
nl_catd cat = catopen("hello", 0);
if (cat == (nl_catd) -1) fputs(strerror(errno), stderr);
puts(_("Hello!", 0));
puts(_("Good bye!", 0));
catclose(cat);
return 0;
}

Please refer to the catgets / catopen / catclose manual for details. On my system the code above doesn't work unless NLSPATH is set.

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 -fc hello.de will create a subdirectory de with hello.msg in it.

Intlize will modify the source as follows:

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

The program can now be compiled.

Gencat

The message catalog is not ready for use right now. It needs to be converted with gencat: cd to de and run gencat hello.cat hello.msg.

This creates a file hello.cat, that is usable by catgets.

Please note that the format of the human readable hello.msg is well defined and portable, while the binary format of hello.cat is not. If you want to write portable code, it is thus necessary that you invoke gencat at compile time.

Now copy hello.cat to a location where catopen can find it. Please refer to the gencat and catopen manuals for details.

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

home

project summary

news

cvs

description

tutorial

manual

download