File tree Expand file tree Collapse file tree 3 files changed +46
-1
lines changed
src/Symfony/Component/Form Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 1414use Symfony \Component \Form \AbstractType ;
1515use Symfony \Component \OptionsResolver \Options ;
1616use Symfony \Component \OptionsResolver \OptionsResolver ;
17+ use Symfony \Contracts \Translation \TranslatableInterface ;
1718
1819/**
1920 * A choice type for native PHP enums.
@@ -29,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void
2930 ->setAllowedTypes ('class ' , 'string ' )
3031 ->setAllowedValues ('class ' , enum_exists (...))
3132 ->setDefault ('choices ' , static fn (Options $ options ): array => $ options ['class ' ]::cases ())
32- ->setDefault ('choice_label ' , static fn (\UnitEnum $ choice ): string => $ choice ->name )
33+ ->setDefault ('choice_label ' , static fn (\UnitEnum $ choice ) => $ choice instanceof TranslatableInterface ? $ choice : $ choice ->name )
3334 ->setDefault ('choice_value ' , static function (Options $ options ): ?\Closure {
3435 if (!is_a ($ options ['class ' ], \BackedEnum::class, true )) {
3536 return null ;
Original file line number Diff line number Diff line change 1616use Symfony \Component \Form \Tests \Fixtures \Answer ;
1717use Symfony \Component \Form \Tests \Fixtures \Number ;
1818use Symfony \Component \Form \Tests \Fixtures \Suit ;
19+ use Symfony \Component \Form \Tests \Fixtures \TranslatableTextAlign ;
1920use Symfony \Component \OptionsResolver \Exception \InvalidOptionsException ;
2021use Symfony \Component \OptionsResolver \Exception \MissingOptionsException ;
22+ use Symfony \Component \Translation \IdentityTranslator ;
23+ use Symfony \Contracts \Translation \TranslatableInterface ;
2124
2225class EnumTypeTest extends BaseTypeTestCase
2326{
@@ -257,6 +260,20 @@ public function testChoiceLabel()
257260 $ this ->assertSame ('Yes ' , $ view ->children [0 ]->vars ['label ' ]);
258261 }
259262
263+ public function testChoiceLabelTranslatable ()
264+ {
265+ $ form = $ this ->factory ->create ($ this ->getTestedType (), null , [
266+ 'multiple ' => false ,
267+ 'expanded ' => true ,
268+ 'class ' => TranslatableTextAlign::class,
269+ ]);
270+
271+ $ view = $ form ->createView ();
272+
273+ $ this ->assertInstanceOf (TranslatableInterface::class, $ view ->children [0 ]->vars ['label ' ]);
274+ $ this ->assertEquals ('Left ' , $ view ->children [0 ]->vars ['label ' ]->trans (new IdentityTranslator ()));
275+ }
276+
260277 protected function getTestOptions (): array
261278 {
262279 return ['class ' => Suit::class];
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Symfony package.
5+ *
6+ * (c) Fabien Potencier <fabien@symfony.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Symfony \Component \Form \Tests \Fixtures ;
13+
14+ use Symfony \Contracts \Translation \TranslatableInterface ;
15+ use Symfony \Contracts \Translation \TranslatorInterface ;
16+
17+ enum TranslatableTextAlign implements TranslatableInterface
18+ {
19+ case Left;
20+ case Center;
21+ case Right;
22+
23+ public function trans (TranslatorInterface $ translator , string $ locale = null ): string
24+ {
25+ return $ translator ->trans ($ this ->name , locale: $ locale );
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments