10000 Choice label containing a TranslatableMessage will strip its parameters · Issue #40622 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Choice label containing a TranslatableMessage will strip its parameters #40622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fogs opened this issue Mar 29, 2021 · 0 comments
Closed

Choice label containing a TranslatableMessage will strip its parameters #40622

fogs opened this issue Mar 29, 2021 · 0 comments

Comments

@fogs
Copy link
fogs commented Mar 29, 2021

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

@fogs fogs added the Bug label Mar 29, 2021
@xabbuh xabbuh added the Form label Mar 29, 2021
nicolas-grekas added a commit that referenced this issue May 7, 2021
…abel option of ChoiceType (alexandre-daubois)

This PR was merged into the 5.2 branch.

Discussion
----------

[Form] Add missing TranslatableMessage support to choice_label option of ChoiceType

It leads to loss of information because it'll use `__toString` to cast, which is incompatible with newly added `TranslatableMessage`, for example.

| Q             | A
| ------------- | ---
| Branch?       | 5.2 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #40622 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | _none_

So this one's a bit tricky in my mind. I didn't want to check if `$dynamicLabel instanceof TranslatableMessage`, because of course it doesn't belong to the same component.

Aside, it would sound so strange to me to add `|object` to `$label` here: https://github.com/symfony/symfony/blob/bb1e1e58aea5318e96d1c22cc8a91668ed7baaaa/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php#L40

But maybe that's the way to go? Requiring your help here. I'm fully open to your ideas, as we're loosing a big feature here by losing `TranslatableMessage` translation parameters.

If the passed object doesn't implement `__toString`, it'll lead to an exception during template rendering, as expected.

Commits
-------

c2873aa [Form] Add TranslatableMessage support to choice_label option of ChoiceType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0