Skip to content

[Form] Support Translatable Enum #18599

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 27, 2023
Merged

[Form] Support Translatable Enum #18599

merged 1 commit into from
Jul 27, 2023

Conversation

Seb33300
Copy link
Contributor

Documentation for symfony/symfony#50931

@Seb33300 Seb33300 requested a review from xabbuh as a code owner July 23, 2023 16:21
@Seb33300 Seb33300 changed the base branch from 6.3 to 6.4 July 23, 2023 16:23
@javiereguiluz javiereguiluz added the Waiting Code Merge Docs for features pending to be merged label Jul 24, 2023
@carsonbot carsonbot added this to the next milestone Jul 24, 2023
derrabus added a commit to symfony/symfony that referenced this pull request Jul 24, 2023
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       | Fix #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
-------

65f26da [Form] Support Translatable Enum
symfony-splitter pushed a commit to symfony/form that referenced this pull request Jul 24, 2023
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       | Fix symfony/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
@derrabus
Copy link
Member

Code PR has been merged.

@xabbuh xabbuh modified the milestones: next, 6.4 Jul 25, 2023
@xabbuh xabbuh removed the Waiting Code Merge Docs for features pending to be merged label Jul 25, 2023
@xabbuh
Copy link
Member

xabbuh commented Jul 26, 2023

We also need a versionadded directive which explains that you can use this feature since Symfony 6.4.

@Seb33300
Copy link
Contributor Author

Added

@OskarStark
Copy link
Contributor

Thanks for your work on this new feature!

@OskarStark OskarStark merged commit 8612bab into symfony:6.4 Jul 27, 2023
@Seb33300 Seb33300 deleted the patch-3 branch July 27, 2023 13:09
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.

6 participants