Skip to content

Fix for issue #7242 : broken translations for enums in forms#7243

Open
FoxCie wants to merge 5 commits intoEasyCorp:4.xfrom
FoxCie:issue_7242_broken_enum_translations_in_forms
Open

Fix for issue #7242 : broken translations for enums in forms#7243
FoxCie wants to merge 5 commits intoEasyCorp:4.xfrom
FoxCie:issue_7242_broken_enum_translations_in_forms

Conversation

@FoxCie
Copy link

@FoxCie FoxCie commented Nov 17, 2025

With symfony/form >= v7.3.5, EnumType uses the keys of the choices option as labels for the enum cases, breaking the translation that happened previously when enum implements TranslatableInterface. The translation now occurs only when the choices option has integer keys (more precisely, currently only if it satisfies array_list()). Fixing this by changing the way ChoiceFields are configured with the ChoiceConfigurator, to define the choices option with integer keys when the enum is translatable.

With symfony/form >= v7.3.5, EnumType uses the keys of the choices option as labels for the enum cases, breaking the translation that happened previously when enum implements TranslatableInterface.
The translation now occurs only when the choices option has integer keys (more precisely, currently only if it satisfies array_list()).
Fixing this by changing the way ChoiceFields are configured with the ChoiceConfigurator, to define the choices option with integer keys when the enum is translatable.
Comment on lines 69 to 70
// When dealing with enums that implement TranslatableInterface, they are now translated by Symfony only if
// the keys of the choices are integers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a bug in Symfony?

Copy link
Author

@FoxCie FoxCie Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They acknowledged that the previous behaviour was actually the bug. When dealing with choices, the keys of the array should be the labels. It is not explicit on EnumType, but on its parent ChoiceType, the doc states that clearly :
image

Source : https://symfony.com/doc/current/reference/forms/types/choice.html#choices .

The thing is that it was actually impossible to change an enum's labels with the choices array with the previous behaviour. Now it is possible, but the old behaviour is still present if the keys are integers (e.g. when giving the ::cases() as the choices).

EDIT : the associated bug in Symfony is here : symfony/symfony#61927 .

Copy link
Contributor

@Seb33300 Seb33300 Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still looks wrong to me. The EnumType should transform the data to make it compatible with ChoiceType

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is what is done. It is just the label that can be changed by passing a string value as the key of the array. EnumType does what should be done so that the values are properly handled and converted. If you think this is not appropriate, you could open an issue on the Symfony side.

@siggidiel
Copy link

Hey guys. What's the status of this? The translations of enum based ChoiceFields on form pages are broken for a while already. Any updates on this? :)

@rdevaissiere
Copy link

Thanks for the PR @FoxCie. It works well for me in forms but there's an issue with the selected value(s) as they come up empty in index/detail pages. I can't quite figure out a fix for now but choiceMessages is not populated in ChoiceConfigurator::configure(). Hope this is helpful.

@FoxCie
Copy link
Author

FoxCie commented Mar 1, 2026

Thanks for the PR @FoxCie. It works well for me in forms but there's an issue with the selected value(s) as they come up empty in index/detail pages. I can't quite figure out a fix for now but choiceMessages is not populated in ChoiceConfigurator::configure(). Hope this is helpful.

Nice catch, we should only change forms, not the other pages. I just pushed a version that should fix that (by just checking if the page is index or details, but I am not sure if this is the correct thing to do, there maybe a better way by changing more what was done before).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants