Skip to content

Choice label containing a TranslatableMessage will strip its parameters #40622

Closed
@fogs

Description

@fogs

Symfony version(s) affected: 5.2.*

Description
Using TranslatableMessage as choice label will result in its __toString() being used to display it, not its trans() representation

How to reproduce
Create a form with a ChoiceType using

                'choice_label' => function ($choice, $key, $value) {
                    return new TranslatableMessage('my_choice', ['%foo%' => $value, '%bar%' => $key], 'form');
                },

The rendered page will contain the translation of my_choice, yet without considering the parameters. Reason is that the TranslatableMessage will be casted to string before it can be translated.

Possible Solution
Remove the string typecast in vendor/symfony/form/ChoiceList/Factory/DefaultChoiceListFactory.php:180

            $label = false === $dynamicLabel ? false : (string) $dynamicLabel;

..or at least if the $dynamicLabel does not implement Symfony\Contracts\Translation\TranslatableInterface

Additional context
none

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions