Symfony News

New in Symfony 4.3: Improved form translation

Webnet team

Contributed by
Webnet team
in #28635.

The forms created with the Symfony Form component translate their labels and help messages automatically. However, the translations cannot contain any custom parameters because in the templates, the trans() Twig filter is called without passing any parameters.

In Symfony 4.3 we improved the translation of Symfony Forms allowing to define custom translation parameters using three new config options: label_translation_parameters, help_translation_parameters, and attr_translation_parameters (this one is useful to translate placeholder and title):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class OrderType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('comment', TextType::class, [
            'label' => 'Comment for the order of %company%',
            'label_translation_parameters' => [
                '%company%' => 'Acme Ltd.',
            ],

            'help' => 'The address of %company% is %address%',
            'help_translation_parameters' => [
                '%company%' => 'Acme Ltd.',
                '%address%' => '4 Form street, Symfonyville',
            ],
        ])
    }
}

Be trained by Symfony experts - 2019-02-18 Clichy - 2019-02-18 Clichy - 2019-02-20 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook