New in Symfony 4.3: Improved form translation
February 14, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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',
],
])
}
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Just miss a config-based option to add the "raw" filter after "trans" and it'll be perfect 👍
@Alex there's a new option in Symfony 4.3 called "help_html". Set it to "true" to output HTML contents as "raw". But I think there's no such option for "label".