At first we write the source code for a sample program, that looks like this:
#include <iostream>
int main(int argc, char **argv)
{
std::cout << "Hello!" << std::endl;
std::cout << "Good bye!" << std::endl;
return 0;
}
Save this file as hello.cpp.
To add internationalization, we copy the C++ runtime files to the directory where hello.cpp is.
We modify the source like this:
#include "i5ecg.hpp"
#include <iostream>
#include <cerrno>
#ifndef _
#define _(A, B) cat.getmsg((A), (B))
#endif
int main(int argc, char **argv)
{
i5ecg cat;
int err = cat.open("hello", 0
if (err) std::cerr << strerror(errno) << std::endl;
std::cout << _("Hello!", 0) << std::endl;
std::cout << _("Good bye!", 0) << std::endl;
return 0;
}
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 -C -k_ -o hello.pot ../hello.cpp
. 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!"
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:
...
std::cout << _("Hello!", 1) << std::endl;
std::cout << _("Good bye!", 2) << std::endl;
...
The program can now be compiled.
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.