在Symfony2类表单中转换选择选项 [英] Translate select options in Symfony2 class forms
本文介绍了在Symfony2类表单中转换选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
namespace Partners\FrontendBundle\Form;
使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilder;
class ConfigForm extends AbstractType
{
public function buildForm(FormBuilder $ builder,array $ options)
{
$ builder-> add(' no_containers','choice',array('choices'=> array(1 =>'yes',0 =>'no')));
...
我想翻译'yes'和'no'选项,但我不知道如何在这里使用翻译器。
解决方案
您可以照常使用翻译资源。这对我有效:
$ builder-> add('sex','choice',array(
'choices'=>数组(
1 =>'profile.show.sex.male',
2 =>'profile.show.sex.female',
),
'required'=> false,
'label'=>'profile.show.sex.label',
'translation_domain'=>'AcmeUserBundle'
) );
然后将您的翻译添加到您的Bundle的Resources-> translations目录。
从@CptSadface更新:
$ b 在 symfony 2.7 中,使用choice_label参数,可以指定翻译域名如下:
'choice_label'=> 'typeName',
'choice_translation_domain'=> 'messages',
不指定域名,选项不会被翻译。
I'm using a class form in Symfony2 Beta3 as follows:
namespace Partners\FrontendBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ConfigForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no')));
...
I want to translate the 'yes' and 'no' options, but I don't know how to use the translator here.
解决方案
You can use the translation resources as usual. This worked for me:
$builder->add('sex', 'choice', array(
'choices' => array(
1 => 'profile.show.sex.male',
2 => 'profile.show.sex.female',
),
'required' => false,
'label' => 'profile.show.sex.label',
'translation_domain' => 'AcmeUserBundle'
));
And then add your translations to the Resources->translations directory of your Bundle.
Update from @CptSadface:
In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:
'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',
Without specifying the domain, options are not translated.
这篇关于在Symfony2类表单中转换选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文