Skip to content

Support translatable Enum in Forms #50919

@Seb33300

Description

@Seb33300

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions