无法在 Ubuntu 上运行 gettext (php)
以下示例适用于带有 Apache 的 Mac OS X,即我得到了回显翻译后的字符串。但在使用 lighttpd 的 Ubuntu 上,我得到原始文本“非活动帐户”。我尝试了环境变量的各种组合,但没有任何运气。它也不是文件权限,因为我可以回显 .mo 文件的内容。
<?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");
?>
有人有什么想法吗?
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");
?>
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我面临着同样的问题。我将描述我在 Ubuntu 10.10 上修复该问题所做的事情。
1) 确保您已安装“gettext”,
或者,如果无法安装“gettext”,您可以安装“php-gettext”。如果您已经安装了“gettext”,则不需要软件包“php-gettext”。
2) 然后生成您的语言的区域设置。在此示例中,我将使用“sv_SE”。在 '/usr/share/i18n/SUPPORTED' 中查找支持的语言环境,
您会发现多行以 'sv_SE' 开头,
这意味着您有多个选项来生成 sv_SE 的语言环境。其中一个选项的名称中没有句点 (.)(即
sv_SE ISO-8859-1
);这是该区域设置的默认字符集。要生成默认字符集的语言环境,请运行以下命令,如果要生成 UTF-8 字符集的语言环境,请运行此命令,
生成语言环境后重新启动 Apache(否则它将找不到新生成的语言环境) ),
3) 最后,更新您的 PHP 脚本以匹配您生成的区域设置。如果您生成了“sv_SE”的语言环境,
但如果您生成了该区域设置的 UTF-8 等效项,请使用,
现在一切都应该可以了。
I was facing the same problem. I'll describe the things I did to fix it on Ubuntu 10.10.
1) make sure you have 'gettext' installed,
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) 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',
You'll find multiple lines that start with 'sv_SE',
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,If you want to generate that locale for the UTF-8 character set, run this command,
Restart Apache after generating locales (it won't find the newly generated locales otherwise),
3) Finally, update your PHP script to match the locale you generated. If you generated the locale for 'sv_SE',
But if you generated the UTF-8 equivalent of that locale, use,
All should work now.
尽管我遵循了已接受答案中概述的步骤,但我无法让它在 Ubuntu 14.04 LTS 上运行。我必须做两件重要的事情:
(1)在生成 messages.mo 文件之前,我必须在 message.po 中明确指定 UTF-8 作为字符集:
(2)我必须维护与安装的语言环境相同的文件夹名称。例如,我通过运行 sudo locale-gen kn_IN 安装了卡纳达语。这生成了
kn_IN.UTF-8
。请注意 UTF-8 中的大写字母和连字符。我在 php 脚本中设置区域设置以及放置 messages.mo 文件的文件夹名称时使用了相同的名称:PHP 脚本:
文件夹名称:
Ubuntu 对文件夹和文件名区分大小写。因此,请确保保留大写字母,如上所示。
Though I followed the steps outlined in the accepted answer, I couldn't get it to work on Ubuntu 14.04 LTS. I had to do two important things:
(1) Before generating the messages.mo file, I had to specify in message.po explicitly UTF-8 for charset:
(2) I had to maintain same folder name as the locale installed. For instance, I installed Kannada by running
sudo locale-gen kn_IN
. This generatedkn_IN.UTF-8
. Note the capital letters and hyphen in UTF-8. I used this same name while setting locale in my php script and for the folder name where I put the messages.mo file:PHP Script:
Folder Name:
Ubuntu is case sensitive to folder and file names. So make sure the capital letters are maintained as shown above.