如何在Zend Framework 2中转换表单标签? [英] How to translate form labels in Zend Framework 2?

查看:76
本文介绍了如何在Zend Framework 2中转换表单标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到!..请有人解释,如何翻译表单标签?一个简单的例子就很好.

I'm not getting it!.. Can please someone explain, how to translate form labels? A simple example would be great.

提前谢谢!

Search \ Form \ CourseSearchForm类

...

class CourseSearchForm extends Form {

    ...

    public function __construct(array $cities) {
        parent::__construct('courseSearch');
        ...
        $this->add(array(
            'name' => 'city',
            'type'  => 'Zend\Form\Element\Select',
            'options' => array(
                'label' => 'Stadt',
                'value_options' => $this->cities,
                'id'  => 'searchFormCity',
            ),
        ));
        ...
    }
}

查看脚本/module/Search/view/sea​​rch/search/search/search-form.phtml

<?php echo $this->form()->openTag($form); ?>
<dl>
    ...
    <dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt>
    <dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd>
    ...
</dl>
<?php echo $this->form()->closeTag(); ?>
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. -->


module/Application/config/module.config.php 已配置:


The module/Application/config/module.config.php is configured:

return array(
    'router' => ...
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'de_DE',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => ...
    'view_manager' => ...
);

我还编辑了视图,并使用了FormLabel视图助手:

I also edited my view and use the FormLabel view helper:

<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>

此外,我在使用翻译器的地方调试了FormLabel(行

Furthermore I debugged the FormLabel at the place, where the tranlator is used (lines 116-120) -- seems to be OK.

但是它仍然无法正常工作.

But it's still not working.

编辑

我手动添加到de_DE.po文件的标签(测试)项目已翻译. ZF2的实际问题是,我在视图脚本中使用的是$form->get('city')->getLabel()而不是$this->formlabel($form->get('city')).

The (test) items for labels, I added to the de_DE.po file manually, are tranlated. The ZF2 side problem was actually, that I was using $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) in th view script.

现在的问题是,标签未添加到de_DE.po文件中.但这不再是ZF2问题,因此我接受了鲁宾的回答并提出了一个新的Poedit问题.

The problem is now, that the labels are not added to the de_DE.po file. But it's not a ZF2 issue anymore, so I've accept Ruben's answer and open a new Poedit question.

推荐答案

而不是使用:

<?php echo $form->get('city')->getLabel(); ?>

您应该使用formlabel视图助手.如果您在转换器中插入了翻译程序,则该助手会在渲染过程中自动使用它.您很有可能会在应用程序的模块module.config.php中找到它:

You should use the formlabel view helper. This helper automatically uses your translator during rendering if you have inserted it in your ServiceManager. Most likely you will have it in your Application's module module.config.php:

'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),

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

一旦您使用了formlabel视图助手:

Once you do use the formlabel view helper:

echo $this->formLabel($form->get('city'));

当然,请确保您的翻译文件位于您的.po文件中.

And of course make sure your translations are in your .po file.

这篇关于如何在Zend Framework 2中转换表单标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆