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

      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