Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Set translation domain for a whole form

Tags:

I want to translate a form created with symfony's formbuilder. As i don't want one big translation file it is splitted up into "domains".

Now i have to specify the translation_domain for each form-field, otherwise symfony will look in the wrong file. This option has to be added to every field and i'm wondering if there is a way to set this option to a whole form?

Sample code i'm not happy with:

$builder->add(     'author_name',     'text',     array('label' => 'Comment.author_name', 'translation_domain' => 'comment') )->add(     'email',     'email',     array('label' => 'Comment.email', 'translation_domain' => 'comment') )->add(     'content',     'textarea',     array('label' => 'Comment.content', 'translation_domain' => 'comment') ); 
like image 766
Alex B. Avatar asked Aug 07 '13 05:08

Alex B.


1 Answers

You've then to set it as a default option of your form, add this:

public function setDefaultOptions(OptionsResolverInterface $resolver) {         $resolver->setDefaults(array(         'translation_domain' => 'comment'     ));  } 

to your setDefaultOptions method, in your form.

Update: It is deprecated. Use configureOptions method instead (thanks @Sudhakar Krishnan)

like image 112
Ahmed Siouani Avatar answered Sep 19 '22 23:09

Ahmed Siouani