Translate select options in Symfony2 class forms

前端 未结 4 1605
孤城傲影
孤城傲影 2020-12-08 10:00

I\'m using a class form in Symfony2 Beta3 as follows:

namespace Partners\\FrontendBundle\\Form;

use Symfony\\Component\\Form\\AbstractType;
use Symfony\\Com         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 10:37

    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.

提交回复
热议问题