diff --git a/src/Symfony/Component/Form/Extension/Core/Type/EnumType.php b/src/Symfony/Component/Form/Extension/Core/Type/EnumType.php index 003819e065738..bfede9c04d14e 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/EnumType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/EnumType.php @@ -14,6 +14,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Contracts\Translation\TranslatableInterface; /** * A choice type for native PHP enums. @@ -29,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void ->setAllowedTypes('class', 'string') ->setAllowedValues('class', enum_exists(...)) ->setDefault('choices', static fn (Options $options): array => $options['class']::cases()) - ->setDefault('choice_label', static fn (\UnitEnum $choice): string => $choice->name) + ->setDefault('choice_label', static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name) ->setDefault('choice_value', static function (Options $options): ?\Closure { if (!is_a($options['class'], \BackedEnum::class, true)) { return null; diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/EnumTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/EnumTypeTest.php index 7c43e945b1e4e..0458720691031 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/EnumTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/EnumTypeTest.php @@ -16,8 +16,11 @@ use Symfony\Component\Form\Tests\Fixtures\Answer; use Symfony\Component\Form\Tests\Fixtures\Number; use Symfony\Component\Form\Tests\Fixtures\Suit; +use Symfony\Component\Form\Tests\Fixtures\TranslatableTextAlign; use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; +use Symfony\Component\Translation\IdentityTranslator; +use Symfony\Contracts\Translation\TranslatableInterface; class EnumTypeTest extends BaseTypeTestCase { @@ -257,6 +260,20 @@ public function testChoiceLabel() $this->assertSame('Yes', $view->children[0]->vars['label']); } + public function testChoiceLabelTranslatable() + { + $form = $this->factory->create($this->getTestedType(), null, [ + 'multiple' => false, + 'expanded' => true, + 'class' => TranslatableTextAlign::class, + ]); + + $view = $form->createView(); + + $this->assertInstanceOf(TranslatableInterface::class, $view->children[0]->vars['label']); + $this->assertEquals('Left', $view->children[0]->vars['label']->trans(new IdentityTranslator())); + } + protected function getTestOptions(): array { return ['class' => Suit::class]; diff --git a/src/Symfony/Component/Form/Tests/Fixtures/TranslatableTextAlign.php b/src/Symfony/Component/Form/Tests/Fixtures/TranslatableTextAlign.php new file mode 100644 index 0000000000000..7a5d5cdff68e7 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/TranslatableTextAlign.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Fixtures; + +use Symfony\Contracts\Translation\TranslatableInterface; +use Symfony\Contracts\Translation\TranslatorInterface; + +enum TranslatableTextAlign implements TranslatableInterface +{ + case Left; + case Center; + case Right; + + public function trans(TranslatorInterface $translator, string $locale = null): string + { + return $translator->trans($this->name, locale: $locale); + } +}