doulandai0641 2013-10-31 07:03
浏览 20
已采纳

如何为symfony 2表单类型设置多个翻译域

I have the fallowing form structure:

abstract class BaseContentType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        //common fields
    }
    public function getDefaultOptions() {
        return array(
            'translation_domain' => 'MyBaseBundle',
            'isNew' => false,
        );
    }
 }

class SpecializedContentType extends BaseContentType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        parent::buildForm($builder, $options);
        //more fields....
    }

    public function getName() {
        return 'specialized_content';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $options = $this->getDefaultOptions() + array(
            'data_class' => 'whatever\Entity\Specialized',
        );
        $resolver->setDefaults($options);
    }
}

I need to have different translation domain for the BaseContentType and for every subclasses.

How can I add separate translation domain for a class and its parent?

  • 写回答

1条回答 默认 最新

  • doumu6941 2013-10-31 10:24
    关注

    Inject the translator

    __construct($translator) {
      $this->translator = $translator
    }
    

    Now for each field you can use it to translate from what you need

     $formbuilder
        -> add('field1','text',array(
                   'label'=> $this->translator->trans('mylabel1',array(),'domain_A')))
        -> add('field2','text',array(
                   'label'=> $this->translator->trans('mylabel2',array(),'domain_B')))
        -> add('field3','text',array(
                   'label'=> $this->translator->trans('mylabel3',array(),'domain_C')));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?