Skip to content

[Form] Fix ChoiceType translation domain #37521

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

Merged
merged 1 commit into from
Jul 12, 2020

Conversation

VincentLanglet
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #...
License MIT
Doc PR symfony/symfony-docs#...

When using

->add('foo', ChoiceType::class, [
    'label'       => 'label',
    'translation_domain' => false,
    'choices'     => [
        'choice.no'  => false,
        'choice.yes' => true,
    ],
    'choice_translation_domain' => 'messages',
    'expanded'    => true,
    'required'    => false,
    'placeholder' => false,
]);

I discovered that the choices was not translated.

Seems like it's because the subForm is using the translation_domain instead of the choice_translation_domain.

@VincentLanglet VincentLanglet requested a review from xabbuh as a code owner July 8, 2020 10:17
@VincentLanglet VincentLanglet marked this pull request as draft July 8, 2020 10:35
@VincentLanglet VincentLanglet marked this pull request as ready for review July 8, 2020 10:59
@yceruto
Copy link
Member

yceruto commented Jul 8, 2020

I think this should be merged into 3.4 as a bugfix.

@VincentLanglet VincentLanglet changed the base branch from master to 3.4 July 8, 2020 14:20
@xabbuh
Copy link
Member

xabbuh commented Jul 8, 2020

Can we add a test to prevent future regressions?

@VincentLanglet
Copy link
Contributor Author

I think this should be merged into 3.4 as a bugfix.

I started my branch from the 3.4. I just took the wrong one for the PR. It's fixed :)

@VincentLanglet
Copy link
Contributor Author

Can we add a test to prevent future regressions?

Done @xabbuh

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jul 9, 2020
@nicolas-grekas
Copy link
Member

Thank you @VincentLanglet.

@nicolas-grekas nicolas-grekas merged commit a56f648 into symfony:3.4 Jul 12, 2020
@VincentLanglet VincentLanglet deleted the fixSubForm branch July 12, 2020 09:58
@XWB
Copy link
Contributor

XWB commented Aug 21, 2020

@nicolas-grekas @VincentLanglet @xabbuh

This change caused a regression in https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L304

Our radio button labels are not translated anymore unless I add 'choice_translation_domain' => 'messages' to the form type.

Symfony 4.4.10

image

Symfony 4.4.11

image

Prior to this change, we did not had to configure the translation domain because "messages" was the default and labels were translated automatically.

@xabbuh
Copy link
Member

xabbuh commented Aug 21, 2020

@XWB Thanks for the report. I am going to look into it.

@VincentLanglet
Copy link
Contributor Author

It seems to be expected.

In the DoctrineType, there is the following config:

choice_translation_domain => false

Prior to this fix, the choice_translation_domain was certainly overridden because the label_translation_domain was true.

Now the FormType correctly use the choice_translation_domain option. So

choice_translation_domain => true

is needed if you save translation key in your database.

@XWB
Copy link
Contributor

XWB commented Aug 21, 2020

@VincentLanglet I understand, but it was the default behavior for years, so now all our radio buttons are broken. In my opinion that's a BC break.

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Aug 21, 2020

In my opinion that's a BC break.

I understand that the situation is annoying, but it's not because a bug is old that it's a BC break.
A bugfix should always be called a bugfix.

People shouldn't expect the choice to be translated when they use choice_translation_domain => false.
When using

->add('foo', ChoiceType::class, [
    'label'       => 'foo.label',
    'choices'  => [
        'no'  => false,
        'yes' => true,
    ],
    'choice_translation_domain' => false,
]);

The choice was translated. How can you not call this a bug ? That's not what the documentation explains.

And when using

->add('foo', ChoiceType::class, [
    'label'       => 'label',
    'translation_domain' => false,
    'choices'  => [
        'choice.no'  => false,
        'choice.yes' => true,
    ],
    'choice_translation_domain' => 'messages',
]);

The choices wasn't translated, this is also an issue.

I completely understand that this bug + the fact that the EntityType had the option to false by default should have mistaken a lot of user. I don't know if a message can/should be added to the Changelog to alert users about this. WDYT ?

@XWB
Copy link
Contributor

XWB commented Aug 24, 2020

@VincentLanglet

People shouldn't expect the choice to be translated when they use choice_translation_domain => false.

I understand, though that was not entirely my use case. Unlike your example, I never specified choice_translation_domain, and the form labels of EntityType have been translated by default (just like all other form types) as long as I can remember.

I completely understand that this bug + the fact that the EntityType had the option to false by default should have mistaken a lot of user. I don't know if a message can/should be added to the Changelog to alert users about this. WDYT ?

An entry at https://symfony.com/blog/ would have been nice. It took me almost 2 hours to figure out what was going on between Symfony 4.4.10 and 4.4.11.

@VincentLanglet
Copy link
Contributor Author

I understand, though that was not entirely my use case. Unlike your example, I never specified choice_translation_domain, and the form labels of EntityType have been translated by default (just like all other form types) as long as I can remember.

Yes but the EntityType was using choice_translation_domain => false since the beginning.
The tricky part was that this was not working since the beginning.

The changelog should have been

[Form] Fix ChoiceType translation domain
[Form] EntityType does not translate choices, as configured. Please set `choice_translation_domain => true` to keep the previous behavior.

Or a PR could be make to set choice_translation_domain => true to the EntityType.

@XWB
Copy link
Contributor

XWB commented Aug 24, 2020

Yes but the EntityType was using choice_translation_domain => false since the beginning.

Unfortunately that was never really clear from the documentation, as no default value is specified in the docs: https://symfony.com/doc/current/reference/forms/types/entity.html#choice-translation-domain

The changelog should have been

I agree.

@VincentLanglet
Copy link
Contributor Author

Yes but the EntityType was using choice_translation_domain => false since the beginning.

Unfortunately that was never really clear from the documentation, as no default value is specified in the docs: https://symfony.com/doc/current/reference/forms/types/entity.html#choice-translation-domain

This could worth a PR ! Do you want to make it @XWB ?

@XWB
Copy link
Contributor

XWB commented Aug 24, 2020

@VincentLanglet

It looks like a default template is included, any idea how I could address this change without altering the other form types?

image

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Aug 24, 2020

I don't know :(

@xabbuh
Copy link
Member

xabbuh commented Sep 12, 2020

see symfony/symfony-docs#14220 for the documentation update

@XWB
Copy link
Contributor

XWB commented Sep 12, 2020

Thank you @xabbuh

fabpot added a commit that referenced this pull request Sep 13, 2020
…prevent further regressions (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[DoctrineBridge] add choice_translation_domain tests to prevent further regressions

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix ##37521 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

7775b37 add choice_translation_domain tests to prevent further regressions
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 25, 2020
…e types (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

fix default value of choice_translation_domain for choice types

fixes symfony/symfony#37521 (comment)

Commits
-------

c0a42de fix default value of choice_translation_domain for choice types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants