无法在 Ubuntu 上获取 gettext (php) 工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5257519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get gettext (php) on Ubuntu working
提问by pthulin
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.
下面的示例在 Mac OS X 和 Apache 上工作,即我得到了回显的翻译字符串。但是在带有 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");
?>
Anyone have any ideas?
谁有想法?
回答by Serrano
I was facing the same problem. I'll describe the things I did to fix it on Ubuntu 10.10.
我面临着同样的问题。我将描述我在 Ubuntu 10.10 上修复它所做的事情。
1) make sure you have 'gettext' installed,
1) 确保你已经安装了“gettext”,
sudo apt-get install 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.
或者,如果无法安装“gettext”,您可以安装“php-gettext”。如果您已经安装了“gettext”,则不需要“php-gettext”包。
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',
2) 然后为您的语言生成语言环境。在本例中,我将使用“sv_SE”。在“/usr/share/i18n/SUPPORTED”中查找支持的语言环境,
less /usr/share/i18n/SUPPORTED
You'll find multiple lines that start with 'sv_SE',
您会发现多行以“sv_SE”开头,
sv_SE.UTF-8 UTF-8
sv_SE ISO-8859-1
sv_SE.ISO-8859-15 ISO-8859-15
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,
这意味着您有多种选项可以为 sv_SE 生成语言环境。其中一个选项的名称(即sv_SE ISO-8859-1
)中没有句点 (. );这是该语言环境的默认字符集。要为默认字符集生成语言环境,请运行以下命令,
sudo locale-gen sv_SE
If you want to generate that locale for the UTF-8 character set, run this command,
如果要为 UTF-8 字符集生成该语言环境,请运行以下命令,
sudo locale-gen sv_SE.UTF-8
Restart Apache after generating locales (it won't find the newly generated locales otherwise),
生成语言环境后重启Apache(否则它不会找到新生成的语言环境),
sudo service apache2 restart
3) Finally, update your PHP script to match the locale you generated. If you generated the locale for 'sv_SE',
3) 最后,更新您的 PHP 脚本以匹配您生成的语言环境。如果您为“sv_SE”生成了语言环境,
setlocale(LC_ALL, "sv_SE");
setlocale(LC_ALL, "sv_SE");
But if you generated the UTF-8 equivalent of that locale, use,
但是,如果您生成了该语言环境的 UTF-8 等价物,请使用,
setlocale(LC_ALL, "sv_SE.UTF-8");
setlocale(LC_ALL, "sv_SE.UTF-8");
All should work now.
现在一切都应该工作了。
回答by Adam Ranganathan
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:
尽管我遵循了已接受的答案中概述的步骤,但我无法让它在 Ubuntu 14.04 LTS 上运行。我必须做两件重要的事情:
(1) Before generating the messages.mo file, I had to specify in message.po explicitly UTF-8 for charset:
(1) 在生成messages.mo 文件之前,我必须在message.po 中为字符集明确指定UTF-8:
msgid ""
msgstr "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-12-19 16:28+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" <---------change here
"Content-Transfer-Encoding: 8bit\n"
(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 generated kn_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:
(2) 我必须保持与安装的语言环境相同的文件夹名称。例如,我通过运行sudo locale-gen kn_IN
. 这产生了kn_IN.UTF-8
. 请注意 UTF-8 中的大写字母和连字符。我在我的 php 脚本中设置区域设置时使用了相同的名称,并用于放置 messages.mo 文件的文件夹名称:
PHP Script:
PHP脚本:
// I18N support information
$locale = "kn_IN.UTF-8"; <----------change here
putenv("LANG=".$locale);
putenv("LC_ALL=".$locale);
setlocale(LC_ALL, $locale);
$domain = 'messages';
bindtextdomain($domain, "./Locale");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
Folder Name:
文件夹名称:
project_folder
/Locale
/kn_IN.UTF-8 <------change here
/LC_MESSAGES
/messages.mo
Ubuntu is case sensitive to folder and file names. So make sure the capital letters are maintained as shown above.
Ubuntu 对文件夹和文件名区分大小写。因此,请确保如上所示保持大写字母。