It looks complicated on first sight, because GNU gettext is very powerful. But it can be done in 10 minutes to setup a simple example which scales to an arbitrary complex application.
index.php
<?php
session_start();
// I18N support information here
$language = 'en';
if ($_REQUEST['lang']) {
$language = $_REQUEST['lang'];
}
putenv("LANG=$language");
setlocale(LC_ALL, $language);
// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, "locale");
textdomain($domain);
echo(gettext("This is text number one."));
echo(gettext("This is a second text."));
echo('<br/>');
echo('<a href="' . $_SERVER['PHP_SELF'] . '?lang=en' . '">English</a>');
echo('<br>');
echo('<a href="' . $_SERVER['PHP_SELF'] . '?lang=de' . '">German</a>');
?>
Simple, eh?
xgettext -n *.php. I use cygin on Win32 for this purpose. You will get a file named messages.po.
project is assumed to be the base directory):
project/
locale/
de/
LC_MESSAGES/
en/
LC_MESSAGES/
index.php
messages.po to the LC_MESSAGES directories.
messages.po files. Under every entry msgid there is an entry msgstr. This is where you put the translated version.
en as it is (in index.php, English strings are used), you will get the following locale/de/LC_MESSAGES/messages.po:
# 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: 2004-02-01 10:34+0100\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" #: index.php:20 msgid "This is text number one." msgstr "Dies ist der Text Nummer eins." #: index.php:21 msgid "This is a second text." msgstr "Das ist ein zweiter Text."
messages.po, invoke msgfmt.exe messages.po
See:
http://www.onlamp.com/pub/a/php/2002/06/13/php.html
http://www.php.net/manual/en/ref.gettext.php