8000 [Form] Support Translatable Enum · symfony/symfony@65f26da · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 65f26da

Browse files
Seb33300derrabus
authored andcommitted
[Form] Support Translatable Enum
1 parent 43066ff commit 65f26da

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/Symfony/Component/Form/Extension/Core/Type/EnumType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\OptionsResolver\Options;
1616
use 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;

src/Symfony/Component/Form/Tests/Extension/Core/Type/EnumTypeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
10BC0
@@ -16,8 +16,11 @@
1616
use Symfony\Component\Form\Tests\Fixtures\Answer;
1717
use Symfony\Component\Form\Tests\Fixtures\Number;
1818
use Symfony\Component\Form\Tests\Fixtures\Suit;
19+
use Symfony\Component\Form\Tests\Fixtures\TranslatableTextAlign;
1920
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
2021
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
22+
use Symfony\Component\Translation\IdentityTranslator;
23+
use Symfony\Contracts\Translation\TranslatableInterface;
2124

2225
class 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];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)
0