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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。