Victor Castro
Victor Castro

Reputation: 1242

access to translation in form label

Currently, when I create a form using the FormBuilder, Symfony (3.3.5) show me a warning in the profiler that tell me there are no translation message for the given locale.

I followed the instructions in the symfony's documentation, but the problem still persistant.

My translation file is located here: app/Resources/translations/properties.fr.yml and my config.yml file looks like this:

parameters: locale: fr framework: translator: fallbacks: ['%locale%'] paths: - '%kernel.project_dir%/app/Resources/translations'

the file just contains this:

properties:
    zipcode:
        label: 'Code postal du bien'

and finally the form is created like this:

$property = new Properties();
$form = $this->createFormBuilder($property)
  ->add('zipCode', Type\IntegerType::class, array(
    'attr' => array(
      'min' => '10000',
      'max' => '99999'
    ),
    'label' => 'properties.zipcode.label'
  ))
  ->getForm();

Why my translation file is not used ?

Upvotes: 2

Views: 866

Answers (2)

Frank B
Frank B

Reputation: 3697

The deault translation domain is 'messages'. So you could change properties.fr.yml to messages.fr.yml. Another possibility is to change the translation domain like so:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
       'data_class'         => 'Acme\Entity\DemoEntity',
       'translation_domain' => 'properties'
    ]);
}

Upvotes: 2

A.Creney
A.Creney

Reputation: 136

You should add an attribute to your field like this:

$property = new Properties();
$form = $this->createFormBuilder($property)
  ->add('zipCode', Type\IntegerType::class, array(
    'translation_domain' => 'yourDomain',
    'attr' => array(
      'min' => '10000',
      'max' => '99999'
    ),
    'label' => 'properties.zipcode.label'
  ))
  ->getForm();

Replace "yourDomain" by your domain (file name). Hope this will help.

Upvotes: 3

Related Questions