diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-29 15:29:48 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-29 23:46:32 +0200 |
commit | 8257864829ffdfb410f05e0dd0a9c781f48b741a (patch) | |
tree | a0334208645bdb04625545e6775a602939c0a859 /src/Renderer/Twig | |
parent | a2c47304d8230f1937c2a4f019aba89fa74203bc (diff) |
Added translation support for twig templates
See https://twig-extensions.readthedocs.io/en/latest/i18n.html for documentation
Diffstat (limited to 'src/Renderer/Twig')
-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]; + } +} |