diff options
Diffstat (limited to 'src/Renderer/Twig/Extensions/Translation.php')
-rw-r--r-- | src/Renderer/Twig/Extensions/Translation.php | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Renderer/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php new file mode 100644 index 00000000..63f9800e --- /dev/null +++ b/src/Renderer/Twig/Extensions/Translation.php @@ -0,0 +1,57 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Helpers\Translator; +use Twig_Extension as TwigExtension; +use Twig_Extensions_TokenParser_Trans as TranslationTokenParser; +use Twig_Filter as TwigFilter; +use Twig_Function as TwigFunction; +use Twig_TokenParserInterface as TwigTokenParser; + +class Translation extends TwigExtension +{ + /** @var Translator */ + protected $translator; + + /** @var TranslationTokenParser */ + protected $tokenParser; + + /** + * @param Translator $translator + * @param TranslationTokenParser $tokenParser + */ + public function __construct(Translator $translator, TranslationTokenParser $tokenParser) + { + $this->translator = $translator; + $this->tokenParser = $tokenParser; + } + + /** + * @return array + */ + public function getFilters() + { + return [ + new TwigFilter('trans', [$this->translator, 'translate']), + ]; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('__', [$this->translator, 'translate']), + ]; + } + + /** + * @return TwigTokenParser[] + */ + public function getTokenParsers() + { + return [$this->tokenParser]; + } +} |