翻译不适用于ZF2(Translation is not working with ZF2)

编程入门 行业动态 更新时间:2024-10-27 16:27:29
翻译不适用于ZF2(Translation is not working with ZF2)

感谢您抽出时间帮助我。

我一直在关注本教程:

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

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

我认为我已经遵循了所有步骤,但翻译机制不起作用。

INLT扩展已在系统上安装并处于活动状态。

在模块配置中,我添加了:

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

在Module.php里面添加了一行来定义翻译方法。

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

我已成功创建了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

例如,这是可能发生翻译的视图的一个片段:

<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>

这是.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"

我检查了$ _SERVER ['HTTP_ACCEPT_LANGUAGE']返回的内容,以下是它的内容:

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

所以我假设字符串可能被翻译成西班牙语,但他们没有。 由于转换文件被命名为es_ES,并且我看到HTTP_ACCEPT_LANGUAGE返回一个密钥作为es-ES,我试图将它们重命名为es-ES,但这并没有解决问题。

考虑到编码问题,我已经检查了文件的字符集,并且它们在utf8上,因为我将它们设置在相应的元标记上,它也是我为charset为charset和源字符集选择的字符集的编码。

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

顺便说一句,当我从服务器端显示文件时,我很欣赏字符集错误(如果我从我的地方编辑它,例如用记事本+,特殊字符是正确的编码)。 为什么我遇到这个问题,我该如何解决?

无论如何,我已经在这里展示了那些没有特殊字符的字符串,如果我能正确实现这个过程,我想它们可能会很好地翻译,但似乎我错过了一些东西......

有这个领域经验的人吗?

应用程序行为就像没有添加traslation机制一样,没有任何错误。

提前感谢您的时间和精力。

最好的祝福。

编辑:

我需要将文本域添加到翻译行:

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

添加后,翻译工作正常。

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.

最满意答案

我有同样的问题。 也许我的解决方案对你也有帮助

在module.config.php我有:

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

确保你必须创建* .mo文件(你已经展示了。我写这篇文章以帮助其他人。)。 我在一个教程中读到,你应该在保存poedit时禁用自动创建mo文件。 事实证明我根本没有任何mo文件。 我的代码中的第二个错误如下。 我建立了一个测试页面来显示调试消息。 在这里,您可以输出使用的区域设置。 所以你可以看看你是否拿到了正确的。 在底部,我添加了一个小的翻译测试,看它是否按预期工作:

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

不用说它不起作用。 我搜索了两天的错误。 你看到错误吗? 我忘了把__NAMESPACE__放在要翻译的文本之后。 正确的应该是:

<?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__); ?>

经过这些改变后,它对我有用。

希望有所帮助,Alex

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

更多推荐

本文发布于:2023-07-14 17:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   working   Translation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!