dongmei8460 2014-01-17 12:54
浏览 35
已采纳

翻译不适用于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/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.

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.

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.

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)

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:

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.

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.

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.

  • 写回答

1条回答 默认 最新

  • doufud21086 2014-02-10 11:51
    关注

    I had the same problem. Maybe my solution helps you too.

    In module.config.php I have:

    ...
    '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.). I read in one tutorial you should disable the automatic creation of mo files when saving in poedit. It turned out that I do not had any mo-files at all. 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. 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 超声波 合成孔径 扩散角
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥15 Delphi image图片缩放问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥15 初学者用plt报错,求解答
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退