无法在Ubuntu上获取gettext(php) [英] Can't get gettext (php) on Ubuntu working
问题描述
以下示例在具有Apache的Mac OS X上运行,即,我得到了回显的转换后的字符串.但是在带有lighttpd的Ubuntu上,我得到了原始文本'Inactive account'.我尝试了各种环境变量的组合,但没有任何运气.这也不是文件权限,因为我可以回显.mo文件的内容.
The following example works on Mac OS X with Apache, i.e. I get the translated string echoed back. But on Ubuntu with lighttpd I get the original text 'Inactive account'. I've tried all sorts of combinations of environment varialbes without any luck. It's not file permissions either because I can echo out the contents of the .mo file.
<?php
//$locale = 'sv_SE.UTF-8';
$locale = 'sv_SE';
$dir = dirname(__FILE__);
// File permission is apparantly not a problem as this works...
//echo file_get_contents($dir . '/sv_SE/LC_MESSAGES/flattr.mo');
putenv("LANG=$locale");
putenv("LANGUAGE=$locale");
putenv("LC_ALL=$locale");
putenv("LC_MESSAGES=$locale");
setlocale(LC_ALL, $locale);
setlocale(LC_MESSAGES, $locale);
//setlocale(LANG, $locale);
//setlocale(LANGUAGE, $locale);
bindtextdomain('flattr', $dir);
//bind_textdomain_codeset("flattr", 'UTF-8');
textdomain('flattr');
echo _("Inactive account");
?>
有人有什么主意吗?
推荐答案
我遇到了同样的问题.我将介绍为在Ubuntu 10.10上进行修复而做的事情.
I was facing the same problem. I'll describe the things I did to fix it on Ubuntu 10.10.
1)确保已安装"gettext",
1) make sure you have 'gettext' installed,
sudo apt-get install gettext
或者,如果无法安装'gettext',则可以安装'php-gettext'.如果您已经安装了"gettext",则不需要软件包"php-gettext".
Alternatively, you can install 'php-gettext' if 'gettext' cannot be installed. The package 'php-gettext' is not required if you already have 'gettext' installed.
2)然后为您的语言生成语言环境.在此示例中,我将使用"sv_SE".在"/usr/share/i18n/SUPPORTED"中查找受支持的语言环境,
2) Then generate the locale for your language. In this example I'll use 'sv_SE'. Look up the supported locales in '/usr/share/i18n/SUPPORTED',
less /usr/share/i18n/SUPPORTED
您会发现以'sv_SE'开头的多行
You'll find multiple lines that start with 'sv_SE',
sv_SE.UTF-8 UTF-8
sv_SE ISO-8859-1
sv_SE.ISO-8859-15 ISO-8859-15
这意味着您具有用于生成sv_SE的语言环境的多个选项.其中一个选项的名称中没有句点(.)(即sv_SE ISO-8859-1
);这是该语言环境的默认字符集.要为默认字符集生成语言环境,请运行以下命令,
This means you have multiple options for generating the locale for sv_SE. One of the options does not have a period (.) in its name (i.e. sv_SE ISO-8859-1
); this is the default character set for that locale. To generate the locale for the default character set, run the following command,
sudo locale-gen sv_SE
如果要为UTF-8字符集生成该语言环境,请运行此命令,
If you want to generate that locale for the UTF-8 character set, run this command,
sudo locale-gen sv_SE.UTF-8
在生成语言环境后重新启动Apache(否则将找不到新生成的语言环境)
Restart Apache after generating locales (it won't find the newly generated locales otherwise),
sudo service apache2 restart
3)最后,更新您的PHP脚本以匹配您生成的语言环境.如果您为"sv_SE"生成了语言环境,
3) Finally, update your PHP script to match the locale you generated. If you generated the locale for 'sv_SE',
setlocale(LC_ALL,"sv_SE");
setlocale(LC_ALL, "sv_SE");
但是,如果您生成了与该语言环境等效的UTF-8,请使用
But if you generated the UTF-8 equivalent of that locale, use,
setlocale(LC_ALL,"sv_SE.UTF-8");
setlocale(LC_ALL, "sv_SE.UTF-8");
现在一切正常.
这篇关于无法在Ubuntu上获取gettext(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!