From 53ef9602aa831b68bd6b91a5874b72755dea28f0 Mon Sep 17 00:00:00 2001 From: Bibi Date: Wed, 5 Jul 2023 09:58:56 +0200 Subject: [PATCH] Implementation of the translatable interface. --- README.md | 42 +++++++++++++++++++ composer.json | 1 + .../Translation/TranslatableEnumInterface.php | 20 +++++++++ .../Translation/TranslatableEnumTrait.php | 26 ++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/Bridge/Symfony/Translation/TranslatableEnumInterface.php create mode 100644 src/Bridge/Symfony/Translation/TranslatableEnumTrait.php diff --git a/README.md b/README.md index 6b8ddc3..3990646 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ enum Suit: string implements ReadableEnumInterface - [Symfony Forms](#symfony-form) - [Symfony Controller Argument Resolver](#symfony-httpkernel) - Symfony VarDumper + - [Symfony Translation](#symfony-translation) - [Doctrine ORM](#doctrine) - [Doctrine ODM](#doctrine-odm) - [Faker](#faker) @@ -426,6 +427,47 @@ class DefaultController ➜ A call to `/cards?suits[]=H&suits[]=S` will resolve the `$suits` argument as `[Suit::Hearts, Suit::Spades]`. +### Symfony Translation + +Because the `ReadableEnumInterface` can be translated within the `TranslatorInterface`, it is easy to use `TranslatableInterface` to enums. + +To translate readable enums is just matter to have a call: + +```php +public function trans(TranslatorInterface $translator, string $locale = null): string +{ + return $translator->trans($this->getReadable(), [], $locale); +} +``` + +An interface and a trait have been added for that purpose. + +```php +use Elao\Enum\Bridge\Symfony\Translation\TranslatableEnumInterface; +use Elao\Enum\Bridge\Symfony\Translation\TranslatableEnumTrait; + +class Card: string implements TranslatableEnumInterface +{ + use TranslatableEnumTrait; + + #[EnumCase('suit.hearts')] + case Hearts = '♥︎'; + // ... +} +``` + +We then use in **PHP**: + +```php +$translated = Card::Hearts->trans($this->translator) +``` + +Or in **Twig**: + +```twig +{{ game.card|trans }} +``` + ### Doctrine As of `doctrine/orm` 2.11, PHP 8.1 enum types [are supported natively](https://twitter.com/ogizanagi/status/1480456881265012736?s=20): diff --git a/composer.json b/composer.json index 8b75ba8..55cefe9 100644 --- a/composer.json +++ b/composer.json @@ -54,6 +54,7 @@ "symfony/framework-bundle": "^5.4|^6.0", "symfony/http-kernel": "^5.4.2|^6.0.1", "symfony/phpunit-bridge": "^5.4|^6.0", + "symfony/translation": "^5.4|^6.0", "symfony/var-dumper": "^5.4|^6.0", "symfony/yaml": "^5.4|^6.0" }, diff --git a/src/Bridge/Symfony/Translation/TranslatableEnumInterface.php b/src/Bridge/Symfony/Translation/TranslatableEnumInterface.php new file mode 100644 index 0000000..66c2d4a --- /dev/null +++ b/src/Bridge/Symfony/Translation/TranslatableEnumInterface.php @@ -0,0 +1,20 @@ + + */ + +namespace Elao\Enum\Bridge\Symfony\Translation; + +use Elao\Enum\ReadableEnumInterface; +use Symfony\Contracts\Translation\TranslatableInterface; + +interface TranslatableEnumInterface extends ReadableEnumInterface, TranslatableInterface +{ +} diff --git a/src/Bridge/Symfony/Translation/TranslatableEnumTrait.php b/src/Bridge/Symfony/Translation/TranslatableEnumTrait.php new file mode 100644 index 0000000..7a335ad --- /dev/null +++ b/src/Bridge/Symfony/Translation/TranslatableEnumTrait.php @@ -0,0 +1,26 @@ + + */ + +namespace Elao\Enum\Bridge\Symfony\Translation; + +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); + } +}