Skip to content
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

Implement TranslatableInterface for the ReadableEnumInterface #221

Closed
laurentmuller opened this issue Apr 4, 2023 · 0 comments
Closed
Labels

Comments

@laurentmuller
Copy link
Contributor

laurentmuller commented Apr 4, 2023

Because the ReadableEnumInterface can be translated within the TranslatorInterface, it will be nice to implements also the TranslatableInterface to enums.

To translate readable enums is just matter to have a call as follow:

public function trans(TranslatorInterface $translator, string $locale = null): string
{
    return $translator->trans($this->getReadable(), [], $locale);
}

We then use in PHP:

NotifyType::EMAIL->trans($this->translator)

Or in Twig:

{{ supplier.notifyType|trans }}

We can maybe create a trait for that:

use Elao\Enum\ReadableEnumTrait;
use Symfony\Contracts\Translation\TranslatorInterface;

trait TranslatableEnumTrait {

    use ReadableEnumTrait;

    public function trans(TranslatorInterface $translator, string $locale = null): string
    {
        return $translator->trans($this->getReadable(), [], $locale);
    }
}

And in a enums:

use Elao\Enum\ReadableEnumInterface;
use Symfony\Contracts\Translation\TranslatableInterface;

enum NotifyType: int implements ReadableEnumInterface, TranslatableInterface
{
    use TranslatableEnumTrait ;

    #[EnumCase('notify.email')]
    case EMAIL = 0;
    #[EnumCase('notify.webhook')]
    case WEBHOOK = 1;
}
ogizanagi added a commit that referenced this issue Jul 5, 2023
…ller)

This PR was squashed before being merged into the 2.x-dev branch.

Discussion
----------

Implementation of the translatable interface.

This is the PR for the feature #221.

Commits
-------

53ef960 Implementation of the translatable interface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants