Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| TranslationTwigExtension | |
0.00% |
0 / 10 |
|
0.00% |
0 / 5 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFilters | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getFunctions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| translate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLocale | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Web\Twig; |
| 6 | |
| 7 | use App\Domain\Translation\Service\TranslatorInterface; |
| 8 | use Twig\Extension\AbstractExtension; |
| 9 | use Twig\TwigFilter; |
| 10 | use Twig\TwigFunction; |
| 11 | |
| 12 | class TranslationTwigExtension extends AbstractExtension |
| 13 | { |
| 14 | public function __construct( |
| 15 | private TranslatorInterface $translator |
| 16 | ) {} |
| 17 | |
| 18 | public function getFilters(): array |
| 19 | { |
| 20 | return [ |
| 21 | new TwigFilter('trans', [$this, 'translate']), |
| 22 | ]; |
| 23 | } |
| 24 | |
| 25 | public function getFunctions(): array |
| 26 | { |
| 27 | return [ |
| 28 | new TwigFunction('t', [$this, 'translate']), |
| 29 | new TwigFunction('current_locale', [$this, 'getLocale']), |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | public function translate(string $key, string $category = 'global', ?string $locale = null): string |
| 34 | { |
| 35 | return $this->translator->translate($key, $category, $locale); |
| 36 | } |
| 37 | |
| 38 | public function getLocale(): string |
| 39 | { |
| 40 | return $this->translator->getLocale(); |
| 41 | } |
| 42 | } |