-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Closed
Description
Description
I found this StackOverflow post showing how to make an Enum translatable using TranslatableInterface
:
enum NotifyType: int implements TranslatableInterface
{
case EMAIL = 1;
case WEBHOOK = 2;
case PUNCH_OUT = 4;
public function trans(TranslatorInterface $translator, string $locale = null): string
{
return match ($this) {
self::EMAIL => $translator->trans('supplier.notify.email', locale: $locale),
self::WEBHOOK => $translator->trans('supplier.notify.webhook', locale: $locale),
self::PUNCH_OUT => $translator->trans('supplier.notify.punch_out', locale: $locale),
};
}
}
PHP usage
NotifyType::EMAIL->trans($this->translator)
Twig usage
{{ supplier.notifyType | trans }}
I would love to see this also supported by the Form component to automatically show the translated label when using an EnumType
with an Enum implementing the TranslatableInterface
.
This can be a good solution for #44596
Example
No response
sstok