Reputation: 322
I have form.
$builder
->add('name', TextType::class, array(
'label' => 'ad_menu.fields.name.label',
'required' => true,
'translation_domain' => 'forms'
))
->add('title', TextType::class, array(
'label' => 'ad_menu.fields.title.label',
'required' => true,
'translation_domain' => 'forms'
))
->add('description', TextareaType::class, array(
'label' => 'ad_menu.fields.description.label',
'translation_domain' => 'forms'
))
->add('visible', CheckboxType::class, array(
'label' => 'ad_menu.buttons.edit.label',
'translation_domain' => 'forms'
))
;
And I have forms.en.yml
file in translations
directory of the bundle.
ad_menu:
fields:
name:
label: Name
title:
label: Title
description:
label: Description
visible:
label: Visible
buttons:
add:
label: Add
edit:
label: Edit
But my form is untranslated.
If I use in twig template for example: {{ 'ad_menu.fields.name.label'|trans({}, 'forms') }}
- it works.
What the problem? Thanks.
UPDATE:
In controller:
public function addAction(Request $request)
{
$adMenu = new AdMenu();
$form = $this->createForm(new AdMenuType(), $adMenu);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($adMenu);
$em->flush();
}
return $this->render('AdBackendBundle:Menu:add.html.twig', array(
'form' => $form->createView()
));
}
Form:
class AdMenuType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'label' => 'ad_menu.fields.name.label',
'required' => true,
// 'translation_domain' => 'forms'
))
->add('title', TextType::class, array(
'label' => 'ad_menu.fields.title.label',
'required' => true,
// 'translation_domain' => 'forms'
))
->add('description', TextareaType::class, array(
'label' => 'ad_menu.fields.description.label',
// 'translation_domain' => 'forms'
))
->add('visible', CheckboxType::class, array(
'label' => 'ad_menu.buttons.edit.label',
// 'translation_domain' => 'forms'
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AdBackendBundle\Entity\AdMenu',
'translation_domain' => 'forms'
));
}
}
Twig:
{{ form_start(form) }}
<div class="form-group">
<label for="ad_menu_title">{{ form.children.title.vars.label }}</label>
{{ form_widget(form.title, {'attr': {'class': 'form-control', 'id': 'ad_menu_title'}}) }}
</div>
<div class="form-group">
<label for="ad_menu_name">{{ form.children.name.vars.label }}</label>
{{ form_widget(form.name, {'attr': {'class': 'form-control', 'id': 'ad_menu_name'}}) }}
</div>
<div class="form-group">
<label for="ad_menu_description">{{ form.children.description.vars.label }}</label>
{{ form_widget(form.description, {'attr': {'class': 'form-control', 'id': 'ad_menu_description'}}) }}
</div>
<div class="checkbox">
<label>
{{ form_widget(form.visible) }}
{{ form.children.visible.vars.label }}
</label>
</div>
<input type="submit" class="btn btn-success" value="{{ 'ad_menu.buttons.add.label'|trans({}, 'forms') }}">
{{ form_end(form) }}
Upvotes: 1
Views: 171
Reputation: 3500
You have to set the translation_domain
in the configureOptions()
method of your "formType" like in the example below:
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'translation_domain' => 'forms',
]);
}
Read the related docs about translation domain on form type.
UPDATE BASED ON COMMENTS:
The error is related on how you are rendering the labels. You need to render each label like: {{ form_label(form.name) }}
instead of {{ form.children.name.vars.label }}
.
Read how to customize forms.
Upvotes: 2
Reputation: 2389
To get autoloading catalogues in Symfony2 full stack framework you need to use either
project_root/app/Resources/translations/domain.lang.format
or project_root/src/(*/)*Bundle/Resources/translations
as path.
You seem to use translation
instead of translations
.
see http://symfony.com/doc/current/best_practices/i18n.html#translation-source-file-location
Also after modifying catalogues you may need to clear your cache
Upvotes: 0