代码
<?php
define('PACKAGE','hello');
putenv('LANG=zh_CN');
setlocale(LC_ALL,'zh_CN');
bindtextdomain(PACKAGE,'/var/www/locale');
textdomain(PACKAGE);
echo gettext("Hello World!");
?>
生成po
xgettext -Cd locale/zh_CN/LC_MESSAGES/hello hello.php
修改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.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-07-05 23:20+0000\n"
"PO-Revision-Date: 2010-07-12 11:14+0800\n"
"Last-Translator: Legend Zhang <openplatform@sohu.com>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=GBK\n"
"Content-Transfer-Encoding: 8bit\n"
#: hello.php:12
msgid "Hello World!"
msgstr "你好世界"
生成mo
msgfmt -o locale/zh_CN/LC_MESSAGES/hello.mo locale/zh_CN/LC_MESSAGES/hello.po
修改/var/lib/locales/supported.d/locale文件
en_GB ISO-8859-1 zh_CN GBK
运行dpkg-reconfigure locales
这样就可以了。
在Ubuntu上主要是没有配置好这个locale文件,所以gettext无法正常工作。