简体   繁体   English

ZF2无法使用翻译

[英]Translation is not working with ZF2

Thanks for your time trying to help me. 感谢您抽出宝贵的时间来帮助我。

I have been following this tutorial: 我一直在关注本教程:

http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/ http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/

http://samminds.com/2012/09/create-po-language-files-using-poedit/ http://samminds.com/2012/09/create-po-language-files-using-poedit/

and I think I have followed all steps but the translation mechanism is not working. 而且我认为我已经按照所有步骤操作,但是翻译机制无法正常工作。

INLT extension is installed and active on the system. INLT扩展已安装并在系统上处于活动状态。

At the module config I've added: 在模块配置中,我添加了:

'translator' => array(
    'locale' => 'es_ES',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
            'text_domain' => __NAMESPACE__,
        ),
    ),
),

and inside the Module.php added the line to define the translation method. 在Module.php内添加了一行以定义翻译方法。

public function onBootstrap(MvcEvent $e)
{
    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator
        ->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        ->setFallbackLocale('fr_FR');

    ...
    ...

I have created the po and mo file succefully and upload to the server at the right place. 我已经成功创建了po和mo文件,并将其上传到正确位置的服务器。

vmamp@AMP30:/users/p0100/web/module/Application/language> ls -l
total 20
-rw-r--r-- 1 vmamp users 2652 Jan 16 23:46 es_ES.mo
-rw-r--r-- 1 vmamp users 4582 Jan 16 23:46 es_ES.po

for example this is a snipset of one the view where translation might occur: 例如,这是可能发生翻译的视图的一个片段:

            <li class="moteur"><?php echo $this->translate('Moteur')?></li>
            <li class="couleur"><?php echo $this->translate('Couleur')?></li>
            <?php if (count($this->universeData['garnissage']) > 1):?>
            <li class="selle"><?php echo $this->translate('Selle')?></li>
            <?php endif;?>
            <?php if (count($this->universeData['jonc']) > 1):?>
            <li class="jonc"><?php echo $this->translate('Jonc')?></li>
            <?php endif;?>
            <?php if (count($this->universeData['retros']) > 1):?>
            <li class="retros"><?php echo $this->translate('Retros')?></li>
            <?php endif;?>
            <?php if (count($this->universeData['signature']) > 1):?>
            <li class="signature"><?php echo $this->translate('Signature')?></li>
            <?php endif;?>
            <li class="rangement"><?php echo $this->translate('Rangement')?></li>
            <li class="confort"><?php echo $this->translate('Confort')?></li>
            <li class="perso"><?php echo $this->translate('Perso')?></li>

and this is part of the content of the .po file (es_ES.po) 这是.po文件(es_ES.po)内容的一部分

msgid "Couleur"
msgstr "Color"

#: view/application/application/configure.phtml:56
msgid "Selle"
msgstr "Asiento"

#: view/application/application/configure.phtml:59
msgid "Jonc"
msgstr "Embellecedores"

#: view/application/application/configure.phtml:62
msgid "Retros"
msgstr "Retrovisores"

#: view/application/application/configure.phtml:65
msgid "Signature"
msgstr "Luminosidad"

#: view/application/application/configure.phtml:67
msgid "Rangement"
msgstr "Orden"

#: view/application/application/configure.phtml:68
msgid "Confort"
msgstr "Confort"

I'checked what $_SERVER['HTTP_ACCEPT_LANGUAGE'] returns and here is what it is: 我检查了$ _SERVER ['HTTP_ACCEPT_LANGUAGE']返回的内容,这是什么:

es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3

so I assume strings might be translated into Spanish but they don't. 所以我认为字符串可能会翻译成西班牙语,但事实并非如此。 Since translation files were named as es_ES, and I see HTTP_ACCEPT_LANGUAGE returns a key as es-ES, I tried to renamed them to es-ES, but this has not solved the problem. 由于翻译文件被命名为es_ES,并且我看到HTTP_ACCEPT_LANGUAGE返回的密钥为es-ES,所以我尝试将它们重命名为es-ES,但这并没有解决问题。

Thinking on an encoding issue I've checked the charset of files and they are on utf8 as I set them on the correspondant metatag and what it is also the encoding I selected for the charset at poedit for charset and source charset. 考虑到编码问题,我检查了文件的字符集,并将它们放在utf8上,因为我将它们设置在对应的metatag上,这也是我在poedit上为字符集和源字符集在poedit上为字符集选择的编码。

vmamp@AMP30:/users/p0100/web/module/Application/language> file -i es_ES.po
es_ES.po: text/x-po charset=utf-8

Btw, when I display the file from the server side I appreciate charset mistakes (if I edit it from my place, for example with notepad+, special chars are right encoded). 顺便说一句,当我从服务器端显示文件时,我会欣赏到字符集错误(如果我从自己的位置进行编辑(例如,使用记事本+进行编辑,则会对特殊字符进行正确编码))。 Why I am having this issue and how could I fix it? 为什么会有这个问题,我该如何解决?

Anyway I have shown here strings those have not special chars and I suppose they might be well translated if I would implement the process right, but it seems I am missing something ... 无论如何,我在这里显示了那些没有特殊字符的字符串,我想如果我正确地执行该过程,它们可能会被很好地翻译,但是似乎我缺少了一些东西...

Anyone experienced with this field ? 有这个领域的经验吗?

The application behavieur is like if the traslation mechanism was not added, there aren't any error. 应用程序行为就像没有添加翻译机制一样,没有任何错误。

Thanks in advance for your time and effort. 在此先感谢您的时间和精力。

Best Regards. 最好的祝福。

EDIT: 编辑:

I needed to add the Text Domain to the translations lines: 我需要将文本域添加到翻译行:

<?php echo $this->translate('Moteur', 'Application')?>

after adding it, the translation works fine. 添加后,翻译效果很好。

I had the same problem. 我有同样的问题。 Maybe my solution helps you too. 也许我的解决方案也可以为您提供帮助。

In module.config.php I have: 在module.config.php中,我有:

...
'translator' => array(
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
            'text_domain' => __NAMESPACE__,
        ),
    ),
),
...

Make sure that you have to *.mo files created (you have as you already showed. I write it nevertheless to help others.). 确保您必须创建* .mo文件(您已经显示了这些文件。不过我还是写了这个文件来帮助他人。)。 I read in one tutorial you should disable the automatic creation of mo files when saving in poedit. 我读过一篇教程,在保存poedit时应该禁用自动创建mo文件的功能。 It turned out that I do not had any mo-files at all. 原来,我根本没有Mo文件。 The second error in my code was the following. 我的代码中的第二个错误如下。 I put up a testing page to display debug messages. 我建立了一个测试页面以显示调试消息。 Here you can output the used locale. 在这里您可以输出使用的语言环境。 So you can see if you fetched the correct one. 这样就可以查看您是否获取了正确的。 At the bottom I added a small translation-test to see if it works as expected: 在底部,我添加了一个小的翻译测试,看它是否按预期工作:

<?php
    echo "<br /><h1>DEBUG</h1><br />";
    echo "Translator-Textdomain: " . $this->formLabel()->getTranslatorTextDomain() . "<br />";
    echo "Translator-Locale: " . $this->formLabel()->getTranslator()->getLocale() . "<br />";
    echo "Translator->FallbackLocale: " . $this->formLabel()->getTranslator()->getFallbackLocale() . "<br />";
    echo "Translate-Test: Password -> ". $this->translate('Password');
?>

Needless to say it didn't work. 不用说它没有用。 I searched two days for the error. 我搜索了两天的错误。 Do you see the error? 看到错误了吗? I forgot to put the __NAMESPACE__ after the text to be translated. 我忘记在要翻译的文字后面加上__NAMESPACE__ Correct it should read: 更正它应为:

<?php
    echo "<br /><h1>DEBUG</h1><br />";
    echo "Translator-Textdomain: " . $this->formLabel()->getTranslatorTextDomain() . "<br />";
    echo "Translator-Locale: " . $this->formLabel()->getTranslator()->getLocale() . "<br />";
    echo "Translator->FallbackLocale: " . $this->formLabel()->getTranslator()->getFallbackLocale() . "<br />";
    echo "Translate-Test: Password -> ". $this->translate('Password', __NAMESPACE__);
?>

After these changes it worked for me. 这些更改后,它对我有用。

Hope that helps, Alex 希望能有所帮助,Alex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM