【问题标题】:Zend Framework 2 Translating the text of the radio buttonsZend Framework 2 翻译单选按钮的文本
【发布时间】:2012-12-18 15:55:55
【问题描述】:

我正在使用 Zend Framework 2 开发一个应用程序,我需要翻译我在表单中创建的单选按钮(“显示”、“隐藏”)的文本:

    //within the Form

    public function addRadioButtons ()
        {
            $isPublicRadioButtons = new Element\Radio('isPublic');
            $isPublicRadioButtons->setAttribute('id', 'isPublic')
                    ->setAttribute('value', '0')
                    ->setValueOptions(array(
                        '0' => 'Show',
                        '1' => 'Hide',
                    ));

            $this->add($isPublicRadioButtons);
        }

我必须在视图端做什么才能翻译它们?

我知道要翻译视图,我需要使用 $this→translate() 视图助手。所以在视图中我必须以某种方式调用单选按钮的文本..

//Whithin the view

echo $this->translate($someHowCallTheTextOfRadioButton('isPublic') , $textDomain, $locale);

【问题讨论】:

    标签: forms radio-button zend-framework2 zend-translate


    【解决方案1】:

    查看FormLabel 部分以阅读有关在zend 框架2 中翻译标签的信息。我认为要记住的最重要的事情是:

    如果您在服务管理器中的键下有一个翻译器, ‘translator’,视图助手插件管理器将自动附加 FormLabel 视图助手的翻译器。看 Zend\View\HelperPluginManager::injectTranslator() 了解更多 信息。

    如何正确设置ZendSkeletonApplication中的翻译器

    【讨论】:

      【解决方案2】:

      在您看来,您可以这样做:

      $this->formRadio()->setTranslatorTextDomain('textdomainhere');

      【讨论】:

        【解决方案3】:

        您可以让表单实现 TranslatorAwareInterface,如果您使用的是 PHP 5.4+,则让它使用 TranslatorAwareTrait(否则您只需自己实现接口)。您现在可以将翻译器实例注入您的表单,例如在表单的工厂中。然后您可以按如下方式翻译标签:

        //within the Form
        
        public function addRadioButtons ()
            {
                $isPublicRadioButtons = new Element\Radio('isPublic');
                $isPublicRadioButtons->setAttribute('id', 'isPublic')
                        ->setAttribute('value', '0')
                        ->setValueOptions(array(
                            '0' => $this->getTranslator()->translate('Show'),
                            '1' => $this->getTranslator()->translate('Hide'),
                        ));
        
                $this->add($isPublicRadioButtons);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-16
          相关资源
          最近更新 更多