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 <intlize/i5e.hpp>
#include <iostream>
#include <locale>
#include <langinfo.h>
#ifndef _
#define _(A, B) cat.getmsg(B)
#endif
int main(int argc, char **argv)
{
i5e cat;
std::locale loc("");
int err = cat.open(loc.name(),
"/usr/local/share/locale/%L/LC_MESSAGES/hello.msg, nl_langinfo(CODESET)");
if (err) {
std::cerr << cat.error_text(err) << std::endl;
return 1;
}
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 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 modified the source as follows:
...
std::cout << _("Hello!", 1) << std::endl;
std::cout << _("Good bye!", 2) << std::endl;
...
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
.