douci1541 2013-05-28 12:31
浏览 39
已采纳

Zend Framework 2形式的国际化(i18n)

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 ?

  • 写回答

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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题