Gettext无法翻译 [英] Gettext doesn't translate

查看:194
本文介绍了Gettext无法翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在努力地使用php,gettext和将我的新网站从丹麦语翻译为英语的努力.

For the past few days i've been struggling with php, gettext and getting my, soon to be, new website translated from danish to english.

我的目录包含.po和.mo文件,设置如下:

I have my directories with my .po and .mo files set up as following:

/locale/en_US/LC_MESSAGES/

根据本教程:我使用gettext本地化PHP网站我添加了这一点:

As per this tutorial: Localizing PHP web sites using gettext i added this:

$locale = "en_US";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");

我在页面上添加了五个整体:

I added a five entires in on my page:

echo _('Overskrift 1')

但是,当我在服务器上查看该页面时,一个启用了PHP 5.4和gettext支持的Debian框,则没有任何翻译.

But when i view the page on my server, a Debian box with PHP 5.4 and gettext support enabled, nothing is translated.

我不知道我在做什么错.我确定它很小,但是我看不到它.也许你们有一些指示?

I have no idea what i'm doing wrong. I'm sure it's something small, but i just can't see it. Maybe you guys have some pointers?

我的.po文件:

msgid ""
msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: POEditor.com\n"
"Project-Id-Version: Portfolio\n"
"Language: en\n"

#: 
msgid "Overskrift 1"
msgstr "Headline 1"

#: 
msgid "Overskrift 2"
msgstr "Headline 2"

#: 
msgid "Overskrift 3"
msgstr "Headline 3"

#: 
msgid "Overskrift 4"
msgstr "Headline 4"

#: 
msgid "Overskrift 5"
msgstr "Headline 5"

推荐答案

最典型的问题是您的系统未安装您要设置的语言环境. gettext已深度集成到系统的语言环境系统中.实际上,您需要安装使用setlocale设置的语言环境和/或将其别名化为您提供的名称.例如,在大多数系统上,没有语言环境ja_JP,但是没有语言环境ja_JP.UTF8和/或ja_JP.eucJP.在这种情况下,您需要setlocale(LC_ALL, 'ja_JP.UTF8'),只是jaja_JP不会.

The most typical problem is that your system does not have the locale installed you are setting. gettext is deeply integrated into the system's locale system. The locale you set with setlocale actually needs to be installed and/or aliased to the name you're giving it. On most systems there's, for instance, no locale ja_JP, but the locales ja_JP.UTF8 and/or ja_JP.eucJP. You need to setlocale(LC_ALL, 'ja_JP.UTF8') in this case, just ja or ja_JP won't do.

要检查安装了哪些语言环境,请在命令行上运行locale -a.如果您要查找的语言环境不存在,则需要安装它.在Ubuntu和类似系统上,这就像运行sudo locale-gen ja_JP.UTF8一样容易.

To check which locales are installed, run locale -a on the command line. If the locale you're looking for is not there, you need to install it. On Ubuntu and similar systems this is as easy as running sudo locale-gen ja_JP.UTF8.

替代解决方案

某些罕见的语言环境可能不容易找到,因此您可能无法安装所需的内容.在这种情况下,您可以找到最接近的语言环境.例如,如果您托管应用程序的服务器上没有ku_IQ但存在ku_TR,则可以改用后者.确保将目录重命名为您实际设置的语言环境.您的文本仍会正常翻译,但是日期,时间和数字等区域格式将无法正确显示.您将不得不忍受.

Some rare locales may not be easy to find, and hence you may not be able to install what you want. In that case, you can find a locale that is closest. For example, if there is no ku_IQ but ku_TR is present on the server you're hosting your app, you could use the latter instead. Make sure you rename your directory to the locale you're actually setting it to. Your texts will still be translated normally, but your regional formats like date, time, and numbers will not be displayed properly. You will have to live with that.

这篇关于Gettext无法翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆