I18n with gettext
1. Install gettext2. Make sure php has gettext support
3. Create a directory structure like the following:
test.php locale en_US LC_MESSAGES zh_TW LC_MESSAGES
Test.php should look like this
<?php
header('Content-Type: text/html; charset=utf-8');
if (isset($_GET['lang'])) {
$language = $_GET['lang'];
} else {
$language = "en_EN";
}
$locale = setlocale(LC_ALL, $language);
$gettext_domain = 'messages';
bindtextdomain($gettext_domain, "./locale");
textdomain($gettext_domain);
?>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8">
</head>
<body><form id="form1" name="form1" method="get">
<input type=radio name="lang" id="lang" value="en_EN" onchange="document.form1.submit();"><? echo gettext("English"); ?>
<br>
<input type=radio name="lang" id="lang" value="zh_TW" onchange="document.form1.submit();"><? echo gettext("Chinese"); ?>
<br>
</form>
<h1><? echo _("English"); ?></h1>
</body>
</html>
header('Content-Type: text/html; charset=utf-8');
if (isset($_GET['lang'])) {
$language = $_GET['lang'];
} else {
$language = "en_EN";
}
$locale = setlocale(LC_ALL, $language);
$gettext_domain = 'messages';
bindtextdomain($gettext_domain, "./locale");
textdomain($gettext_domain);
?>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8">
</head>
<body><form id="form1" name="form1" method="get">
<input type=radio name="lang" id="lang" value="en_EN" onchange="document.form1.submit();"><? echo gettext("English"); ?>
<br>
<input type=radio name="lang" id="lang" value="zh_TW" onchange="document.form1.submit();"><? echo gettext("Chinese"); ?>
<br>
</form>
<h1><? echo _("English"); ?></h1>
</body>
</html>
Now run this to generate a messages.po and a messages.mo file:
xgettext -n *.php
vi messages.po #enter the translation text, assuming chinese traditional
msgfmt messages.po
cp messages.* zh_TW/LC_MESSAGES/
vi messages.po #enter the translation text, assuming chinese traditional
msgfmt messages.po
cp messages.* zh_TW/LC_MESSAGES/
My messages.po file for traditional chinese
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: 2007-05-14 19:04+0800\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"
#: test.php:23 test.php:35
msgid "English"
msgstr "英文"
#: test.php:25
msgid "Chinese"
msgstr "中文"
#: test.php:28
msgid "Malaysia"
msgstr "馬拉文"
#: test.php:31
msgid "Thailand"
msgstr "泰文"
# 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-05-14 19:04+0800\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"
#: test.php:23 test.php:35
msgid "English"
msgstr "英文"
#: test.php:25
msgid "Chinese"
msgstr "中文"
#: test.php:28
msgid "Malaysia"
msgstr "馬拉文"
#: test.php:31
msgid "Thailand"
msgstr "泰文"
Now just point your browser to test.php, choose between languages and you should see chinese.

