【问题标题】:PHP gettext does not translatePHP gettext 不翻译
【发布时间】:2016-06-13 12:23:32
【问题描述】:

我正在尝试使用 gettext 翻译我的 PHP 项目,但它不起作用。 PHP 输出 gettext 字符串名称而不是实际的翻译。 在 POeditor 中编译的 .po 文件。

设置语言环境:

// $language = 'en';
// $domain is the same as .mo file name
putenv("LANG=" . $language);
putenv("LANGUAGE=" . $language);
setlocale(LC_ALL, $language); // Also tried with .utf8 at the end
bindtextdomain($domain, 'locale'); // Also tried full path
textdomain($domain);

PO 文件:(在 locale/en/LC_MESSAGES/filename.po 中)

msgid "SETUP_UI"
msgstr "User Interface"

msgid "SETUP_ACCOUNT"
msgstr "Account and Login"

... and so on

在 PHP 中回显:

<?= gettext('SETUP_UI') ?>

是的,我尝试了 stackoverflow 中的所有内容 - 谷歌搜索超过 3 小时。我在 PHP 中安装了 gettext,还尝试重新启动 Apache,重新编译 .po 文件...

浏览器中的结果是:

SETUP_UI

感谢您的回答。

【问题讨论】:

    标签: php apache translation gettext


    【解决方案1】:

    您需要将.po 文件编译为.mo(这是gettext 消耗的)

    【讨论】:

    • 是的,我知道,但是 POeditor 正在编译同时保存文件。
    • @SamuelIllo .mo 文件是否在同一个 locale/en/LC_MESSAGES/ 文件夹中?
    • 是的,它们在同一个文件夹中......但我实际上通过使用 json 文件而不是 gettext 解决了这个问题。也许 POedit 编译器也被损坏或什么...
    • @SamuelIllo Nah,这实际上永远不会发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多