Skip to content

Commit

Permalink
feature #224 Implementation of the translatable interface. (laurentmu…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
ogizanagi committed Jul 5, 2023
2 parents d555de9 + 53ef960 commit 8b09a3a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
20 changes: 20 additions & 0 deletions src/Bridge/Symfony/Translation/TranslatableEnumInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/

namespace Elao\Enum\Bridge\Symfony\Translation;

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

interface TranslatableEnumInterface extends ReadableEnumInterface, TranslatableInterface
{
}
26 changes: 26 additions & 0 deletions src/Bridge/Symfony/Translation/TranslatableEnumTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/

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);
}
}

0 comments on commit 8b09a3a

Please sign in to comment.