How can I display months in French ? Symfony 6.0.2

Viewed 43

I built a registration form in Symfony. I am asking for date of birth to my user. I'd like to know how I can display months in French (or other languages). At the moment they are displayed in English. I posted code on UserType.php. I am thinking maybe I have to add something in it but I have no clue.

How can I display months in French ?

Thank you.

Registration form : months view

UserType.php

<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('gender', ChoiceType::class, [
                'choices' => [
                    'Je suis ...' => '',
                    'un homme' => 'male',
                    'une femme' =>'female',
                    'non-binaire' => 'non-binary'
                ]
            ])
            ->add('lastname')
            ->add('firstname')
            ->add('birthdate', BirthdayType::class, [
                'placeholder' => [
                    'year' => 'Année', 'month' => 'Mois', 'day' => 'Jour',
                ],
            ])
            ->add('occupation')
            ->add('nationality', CountryType::class, [
                'placeholder' => 'Choisis un pays',
            ])
            ->add('nativelanguage', LanguageType::class, [
                'placeholder' => 'Choisis ta langue maternelle',
            ])
            ->add('wishedlanguages', LanguageType::class, [
                'placeholder' => 'Choisis une langue étrangère',
            ])
            ->add('email')
            ->add('password', PasswordType::class, [
                'mapped' => false
            ])
            ->add('password', RepeatedType::class, [
                'type' => PasswordType::class,
                'invalid_message' => 'Les deux mots de passe doivent être identiques.',
                'options' => ['attr' => ['class' => 'password-field']],
                'required' => true,
                'first_options'  => ['label' => 'Password'],
                'second_options' => ['label' => 'Repeat Password'],
            ])
            ->add('roles', CheckboxType::class, [
                'label' => 'Je m\'inscris uniquement en tant qu\'organisateur.',
                'required' => false,
                'compound' => true,
            ])
            ->get('roles')
            ->addModelTransformer(new CallbackTransformer(
                function ($arrayAsBool) {
                return in_array("ROLE_ADMIN", $arrayAsBool);
            },
                function ($boolAsArray) {
                return $boolAsArray ? ["ROLE_ADMIN"] : ["ROLE_USER"];
            }))
            ->add('Subcription', SubmitType::class);
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}
1 Answers

The BirthdayType supports choice_translation_domain, set this to true.

->add('birthdate', BirthdayType::class, [
    'placeholder' => [
        'year' => 'Année', 'month' => 'Mois', 'day' => 'Jour',
    ],
    'choice_translation_domain' => true
])

Update your defaults to set the translation domain name.

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => User::class,
        'translation_domain' => 'forms' // Call it what you like
    ]);
}

Then create a translation file, eg: translations/forms.fr.yaml. This filename is referencing the translation_domain above.

# translations/forms.fr.yaml
Jan: Janvier
Feb: Février
Mar: Mars
...
...

The language locale fr for example, is set in the users request i believe, so you can have as many language translations as you would like. So forms.de.yaml, forms.es.yaml etc.. and wherever the user is coming from, if the translation matches it will use it.

Related