Skip to content

Commit 47b95f2

Browse files
committed
feature #50931 [Form] Support Translatable Enum (Seb33300)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Form] Support Translatable Enum | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #50919 | License | MIT | Doc PR | symfony/symfony-docs#18599 This PR introduce support for Enum implementing `TranslatableInterface` in `EnumType`. Example of use: ```php $builder->add('textAlign', EnumType::class, [ 'class' => TextAlign::class, ]) ``` ```php use Symfony\Contracts\Translation\TranslatableInterface; use Symfony\Contracts\Translation\TranslatorInterface; enum TextAlign: int implements TranslatableInterface { case Left = 1; case Center = 2; case Right = 3; public function trans(TranslatorInterface $translator, string $locale = null): string { // Translate enum from name (Left, Center or Right) return $translator->trans($this->name, locale: $locale); // Translate enum from custom labels return match ($this) { self::Left => $translator->trans('Left aligned', locale: $locale), self::Center => $translator->trans('Centered', locale: $locale), self::Right => $translator->trans('Right aligned', locale: $locale), }; } } ``` Commits ------- 65f26da [Form] Support Translatable Enum
2 parents f7b0a93 + 65f26da commit 47b95f2

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
@@ -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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy