翻译Symfony2类表单中的选择选项

Ser*_*rgi 29 forms translation internationalization choicefield symfony

我在Symfony2 Beta3中使用类表单如下:

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')));
        ...
Run Code Online (Sandbox Code Playgroud)

我想翻译"是"和"否"选项,但我不知道如何在这里使用翻译器.

bin*_*gen 86

您可以照常使用翻译资源.这对我有用:

    $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'
    ));
Run Code Online (Sandbox Code Playgroud)

然后将您的翻译添加到Bundle的Resources-> translations目录中.

从@CptSadface更新:

symfony 2.7中,使用choice_label参数,您可以像这样指定转换域:

'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',
Run Code Online (Sandbox Code Playgroud)

如果不指定域,则不会翻译选项.

  • 这有点元,但不应该选择项目之一是"女性"?我知道我们在计算机科学相关的网站上,但仍然...... (7认同)