10000 [Form] Support Translatable Enum by Seb33300 · Pull Request #50931 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Support Translatable Enum #50931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2023
Merged

Conversation

Seb33300
Copy link
Contributor
@Seb33300 Seb33300 commented Jul 11, 2023
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:

$builder->add('textAlign', EnumType::class, [
    'class' => TextAlign::class,
])
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),
        };
    }
}

@Seb33300
Copy link
Contributor Author

Just created the documentation PR: symfony/symfony-docs#18599

@Seb33300 Seb33300 force-pushed the enum-translatable branch from cfde7a1 to 420b861 Compare July 24, 2023 14:31
@derrabus derrabus force-pushed the enum-translatable branch from 420b861 to 65f26da Compare July 24, 2023 14:58
@derrabus
Copy link
Member

Thank you @Seb33300.

@derrabus derrabus merged commit 47b95f2 into symfony:6.4 Jul 24, 2023
@Seb33300 Seb33300 deleted the enum-translatable branch July 24, 2023 14:59
OskarStark added a commit to symfony/symfony-docs that referenced this pull request Jul 27, 2023
This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Form] Support Translatable Enum

Documentation for symfony/symfony#50931

Commits
-------

5f59527 [Form] Support Translatable Enum
This was referenced Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support translatable Enum in Forms
5 participants
0