File tree Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 14
14
use Symfony \Component \Form \AbstractType ;
15
15
use Symfony \Component \OptionsResolver \Options ;
16
16
use Symfony \Component \OptionsResolver \OptionsResolver ;
17
+ use Symfony \Contracts \Translation \TranslatableInterface ;
17
18
18
19
/**
19
20
* A choice type for native PHP enums.
@@ -29,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void
29
30
->setAllowedTypes ('class ' , 'string ' )
30
31
->setAllowedValues ('class ' , enum_exists (...))
31
32
->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 )
33
34
->setDefault ('choice_value ' , static function (Options $ options ): ?\Closure {
34
35
if (!is_a ($ options ['class ' ], \BackedEnum::class, true )) {
35
36
return null ;
Original file line number Diff line number Diff line change 16
16
use Symfony \Component \Form \Tests \Fixtures \Answer ;
17
17
use Symfony \Component \Form \Tests \Fixtures \Number ;
18
18
use Symfony \Component \Form \Tests \Fixtures \Suit ;
19
+ use Symfony \Component \Form \Tests \Fixtures \TranslatableTextAlign ;
19
20
use Symfony \Component \OptionsResolver \Exception \InvalidOptionsException ;
20
21
use Symfony \Component \OptionsResolver \Exception \MissingOptionsException ;
22
+ use Symfony \Component \Translation \IdentityTranslator ;
23
+ use Symfony \Contracts \Translation \TranslatableInterface ;
21
24
22
25
class EnumTypeTest extends BaseTypeTestCase
23
26
{
@@ -257,6 +260,20 @@ public function testChoiceLabel()
257
260
$ this ->assertSame ('Yes ' , $ view ->children [0 ]->vars ['label ' ]);
258
261
}
259
262
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
+
260
277
protected function getTestOptions (): array
261
278
{
262
279
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 <[email protected] >
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