我没有得到它!能否请人解释一下,如何翻译表格标签?举个简单的例子就太好了。
提前谢谢你!
类搜索\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/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:
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视图助手:
<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>此外,我在使用转发器的地方调试了FormLabel (116-120行) --似乎没问题。
但还是不起作用。
编辑
我手动添加到de_DE.po文件中的标签(测试)项被传递。ZF2方面的问题实际上是,在视图脚本中,我使用的是$form->get('city')->getLabel()而不是$this->formlabel($form->get('city'))。
现在的问题是,标签没有添加到de_DE.po文件中。但是这不再是一个ZF2问题了,所以我已经接受了Ruben的答案,并打开了一个新的Po编辑问题。
发布于 2013-04-12 02:37:21
而不是使用:
<?php echo $form->get('city')->getLabel(); ?>您应该使用表单标签视图助手。如果您已将翻译插入ServiceManager中,则此助手会在呈现过程中自动使用它。您很可能会将其放在应用程序的模块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视图助手:
echo $this->formLabel($form->get('city'));当然,确保翻译在.po文件中。
发布于 2013-04-27 13:02:07
我认为你的问题是你的标签没有被poedit (或类似的工具)检测到,所以你必须手动地将它们添加到你的poedit目录(.po)中。
要使您的标签字符串被po编辑之类的工具检测到,您的字符串需要在转换()函数或_()中使用(其他函数可以在Catalog/properties/关键字中添加)
由于_()函数在ZF2 (今天)中不是用户,所以一个很小的黑客就是在您的index.php中添加这样的函数(不需要以这种方式修改任何内容):
// in index.php
function _($str)
{
return $str;
}在代码中,当字符串在转换函数之外时,只需使用它即可。
//...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => _('myLabel') , // <------ will be detected by poedit
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
//...或者像这样,如果你愿意
$myLabel = _('any label string'); // <--- added to poedit catalog
//...
'options' => array(
'label' => $myLabel ,
'value_options' => $this->cities,
'id' => 'searchFormCity',
),发布于 2013-04-12 07:05:34
@鲁本说对了!
我使用PoEdit生成*.mo文件,并确保文件中的所有翻译都在某个地方创建(例如,视图),创建一个名为_lan.phtml的文件,所有文本都要翻译:
<?php echo $this->translate("My label");
... ?>当然,Poedit必须配置为查找我的关键字。检查这个如何配置它
https://stackoverflow.com/questions/15961628
复制相似问题