php-gettext无效

时间:2011-12-04 17:17:10

标签: php translation gettext php-gettext

我正在尝试使用php-gettext设置本地化,但无论如何它似乎都不起作用。

我有一个index.php:

<?php require_once "localization.php";?>

<a href="?locale=en_US">English</a> |
<a href="?locale=de_DE">German</a> 

<br>
<?php echo _("Hello World!"); ?><br>
<?php echo _("My name is"); ?> Bob.

和localization.php

<?php $locale = false;
if (isset($_GET["locale"])) { $locale = $_GET["locale"];}
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");

我还在./locale/de_DE/LC_MESSAGES/messages.po / .mo

下创建了翻译文件。

我在Ubuntu 11.04(natty),PHP Version 5.3.5-1ubuntu7.3,apache2

下尝试这个

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

尝试:

<?php
$directory = dirname(__FILE__).'/locale';
$domain = 'messages';
$locale ="your_locale"; //like pt_BR.utf8";

putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
?>

答案 1 :(得分:0)

也许为时已晚,但我遇到了同样的问题:

1st - 您需要将.utf8添加到$locale字符串中 第二步 - 如果没有安装语言,请检查您的系统是否支持locale -a语言,例如de_DE:sudo apt-get install language-pack-de-base

$locale = "de_DE.utf8";
if (isset($_GET["locale"])) $locale = $_GET["locale"];

$domain = 'messages';
$directory = dirname(__FILE__);//your path to locale folder may differ

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');