代码之家  ›  专栏  ›  技术社区  ›  timing stringCode

PHP Gettext无法通过cli或apache工作

  •  0
  • timing stringCode  · 技术社区  · 12 年前

    运行php-5.3.10

    这是我第一次通过gettext使用i18n建立网站。然而,英文文本仍在印刷,而不是我期待的荷兰文本。我已经用谷歌搜索了两天了,但我仍然没有找到解决方案。

    php代码即使在从cli运行时也不会提供翻译,因此Apache缓存不是问题所在。代码:

    <?php
    // no errors are thrown, sadly
    error_reporting(E_ALL);
    
    $domain = 'test';
    
    setlocale(LC_ALL, 'nl_NL.utf8'); // returns 'nl_NL.utf8', I've also tried without '.utf8' etc.
    bindtextdomain($domain, dirname(__FILE__) '/locale'); // returns correct path
    bind_textdomain_codeset($domain, 'UTF-8');
    textdomain($domain); // returns 'test'
    
    // this prints the date in Dutch so setlocale works
    echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
    
    // this should print the translated version but just prints 'Home'
    echo ' ' . gettext('Home');
    

    荷兰语言环境根据“locales-a”安装,如下所示:

    nl_AW
    nl_AW.utf8
    nl_BE.utf8
    nl_NL.utf8
    

    这是.po文件:

    msgid ""
    msgstr ""
    "Language: nl\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    
    msgid "Home"
    msgstr "Thuis"
    

    并且它被正确地编译成.mo文件,msgunfmt至少没有给出错误。我已尝试将此文件放入区域设置/nl/LC_MESSAGES/、区域设置/nl_nl/LC_MESSAGES/和区域设置/nl_nl.utf8/LC_MESSAGES/中。但无效。

    我主要在寻找一种调试方法。整个试错方法有点耗时。

    1 回复  |  直到 12 年前
        1
  •  2
  •   timing stringCode    12 年前

    我找到了解决方案,但不知道我为什么要这样做。对于cli和apache,我都设法使翻译工作正常。这就是我所做的。

    putenv('LANGUAGE=nl_NL');
    

    在putenv中与LC_ALL一起使用LANGUAGE也会破坏Apache的翻译 使用:

    putenv('LC_ALL=nl_NL');