Symfony2 Set domain translation for whole form without class
I want to translate a form created with symfony formbuilder. Now I have to specify translation_domain for each form field. This parameter needs to be added to every field and I am wondering if there is a way to set this option for the whole form?
I need a solution for a form without a class. I know a solution for the Type Type class.
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'translation_domain' => 'main'
));
This is my code:
$form = $this->get('form.factory')->createNamedBuilder('shopChoiceForm')
->add('shops', 'entity', array('class' => 'AcmeBundle:Shop', 'choices' => $choices, 'translation_domain' => 'main'))
->add('submit', 'submit', array('label' => 'Choose', 'translation_domain' => 'main'))
->getForm();
source to share
You can specify the domain of the translations in the thread when you create your form. To do this, just use the following code:
{% trans_default_domain "main" %}
It will change the default translation domain to main
. And this will only affect the current template. If you want to display many forms in one template, you can use a tag form_theme
and import a theme with only one line.
You can specify the translation domain in the parameter array passed in createNamedBuilder()
like:
$form = $this->get('form.factory')->createNamedBuilder('shopChoiceForm', 'form', null, array('translation_domain' => 'main'))
->add('shops', 'entity', array('class' => 'AcmeBundle:Shop', 'choices' => $choices))
->add('submit', 'submit', array('label' => 'Choose'))
->getForm();
source to share