PHP Gettext-无需翻译 [英] PHP Gettext - No translation

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

问题描述

我正在尝试使用PHP gettext扩展名来翻译一些字符串.所有函数似乎都返回正确的值,但调用gettext()/_()仅返回原始字符串. PO/MO文件似乎正确,我相信我已经正确设置了目录.我正在Windows上使用PHP 5.3.10运行WAMP Server(也尝试运行5.3.4和5.3.8,因为我已经安装了).

I am trying to use the PHP gettext extension in order to translate some strings. All functions appear to return the correct values but calling gettext()/_() returns the original string only. The PO/MO files seem correct and I believe I have set the directories up correctly. I am running WAMP Server with PHP 5.3.10 on Windows (also tried running 5.3.4 and 5.3.8 because I have the installations).

首先,请参见/new2/www/index.php:

$locale = 'esn'; # returns Spanish_Spain.1252 in var dump

putenv("LC_ALL={$locale}"); // Returns TRUE
setlocale(LC_ALL, $locale); // Returns 'Spanish_Spain.1252'

$domain = 'messages';
bindtextdomain($domain, './locale'); // Returns C:\wamp\www\new2\www\locale
bind_textdomain_codeset($domain, 'UTF-8'); // Returns UTF-8
textdomain($domain); // Returns'messages'

print gettext("In the dashboard"); // Prints the original text, not the translation.
exit;

我创建了以下文件结构:

I have created the following file structure:

www/new2/www/locale/Spanish_Spain.1252/LC_MESSAGES/messages.mo

我还尝试用es_ESesnespSpanishSpanish_Spain替换Spanish_Spain.1252.

I have also tried replacing Spanish_Spain.1252 with: es_ES, esn, esp, Spanish, and Spanish_Spain.

用于生成MO的PO文件是这样的(仅给出了相关条目):

The PO file used to generate the MO is like so (only the relevant entry given):

#: C:\wamp\www\new2/www/index.php:76
msgid "In the dashboard"
msgstr "TRANSLATED es_ES DASHBOARD"

这是使用PoEdit生成的.添加任何新的.MO文件后,我都重新启动了Apache.还要注意,我以前使用的是Zend_Translate和Gettext,并且它翻译正确.不过,我希望依赖于本机的gettext扩展,部分原因是我试图创建自己的轻量级框架.

This was generated using PoEdit. I have restarted Apache after adding any new .MO file. Also note that I was previously using Zend_Translate with Gettext and it was translating correctly. I wish to rely on the native gettext extension, though, in part because I am attempting to create a lightweight framework of my own.

任何帮助将不胜感激.

编辑:修改后的目录结构.注意-可以在24小时内尝试最近的答案.

Edit: Amended directory structure. Note - will be able to try recent answers within 24hrs.

推荐答案

您的代码将其作为bindtextdomain的返回值:

Your code mentions this as the return value from bindtextdomain:

C:\wamp\www\new2\www\locale

使用Spanish_Spain.1252setlocalemessagestextdomain,对gettext的调用将在以下路径中查找:

With the setlocale of Spanish_Spain.1252 and textdomain of messages, calls to gettext will look in this path:

C:\wamp\www\new2\www\locale\Spanish_Spain.1252\LC_MESSAGES\messages.mo

但是您创建了以下文件的结构:

But you created the file structure of:

www/new2/locale/Spanish_Spain.1252/LC_MESSAGES/messages.mo
         ^^
         www/ missing here

修改

好的,所以没有帮助.我已经在Windows上创建了一个测试脚本,并像您一样使用POEdit:

Okay, so that didn't help. I've created a test script on Windows and using POEdit like you:

$locale = "Dutch_Netherlands.1252";
putenv("LC_ALL=$locale"); // 'true'
setlocale(LC_ALL, $locale); // 'Dutch_Netherlands.1252'
bindtextdomain("messages", "./locale"); // 'D:\work\so\l10n\locale'
textdomain("messages"); // 'messages'

echo _("Hello world"); // 'Hallo wereld'

我的文件夹结构是这样的:

My folder structure is like this:

D:\work\so\l10n\
    \locale\Dutch_Netherlands.1252\LC_MESSAGES\messages.mo
    \locale\Dutch_Netherlands.1252\LC_MESSAGES\messages.po
    \test.php

希望它会有所帮助,尽管它看起来几乎与您的相同.我在网上发现的一些信息:

Hope it helps, although it looks almost identical to yours. A few things I found online:

  • 在.po文件中设置字符集很重要
  • 本地化文件中的空格可能具有UTF8替代格式,因此请警惕键查找失败.可能首先要测试的最好的东西是根本没有空格的键.

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

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