I have a small learning app writen using ZF2 (based on ZF2 user guide: http://framework.zend.com/manual/2.2/en/user-guide/skeleton-application.html) and right now I want to add internationalization to my app and it's almost done but I'm stuck on translating forms. When i wanted to use $this->translate
in form class i got error saying that method translate does not exists. Is there any good example or tutorial about internationalization in ZF2 ?
Zend Framework 2形式的国际化(i18n)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dsg56465 2013-05-28 14:52关注
I will quote the answer i have provided right here. I won't bother to edit the texts too much, so do not bother about some of the sentences, but the essence is important and will help you out a lot.
The first thing to get Form-Translation working is to add files to the
translator
When adding a new translator to your configuration, do NOT use the default translator-text-domain. The Syntax is as follows:
$translator->addTranslationFile( $type, $resource, $textDomain, //<-- this is the important one $lang );
In your example you've added a file to the default-textdomain. This, sadly, brings lots of troubles with it, at it will not always work as expected. Whenever you're adding translation files, add them to your own text-domain!
After that, all you need to do is to assign the
Zend\Form\View\Helper
your text-domain. This is done by the following:// For Labels $this->formLabel()->setTranslatorTextDomain('your-textdomain'); // For Buttons (input type submit) $this->formButton()->setTranslatorTextDomain('your-textdomain'); // For Error-Messages $this->formElementErrors()->setTranslatorTextDomain('your-textdomain');
About the Translator itself
Another thing to note is: as long as there is a Translator-Service attached to your configuration with the name
translator
, it will automatically be attached to the form as default translator. This is a sample configuration i use within my modules regularly:'translator' => array( 'locale' => 'de_DE', 'translation_file_patterns' => array( array( 'type' => 'phparray', 'base_dir' => __DIR__ . '/lang', 'pattern' => '%s.php', 'text_domain' => __NAMESPACE__, ), ), ),
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测