You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 | Fixsymfony/symfony#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
-------
65f26dad9c [Form] Support Translatable Enum
0 commit comments