summaryrefslogtreecommitdiff
path: root/src/Renderer
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-29 15:29:48 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:32 +0200
commit8257864829ffdfb410f05e0dd0a9c781f48b741a (patch)
treea0334208645bdb04625545e6775a602939c0a859 /src/Renderer
parenta2c47304d8230f1937c2a4f019aba89fa74203bc (diff)
Added translation support for twig templates
See https://twig-extensions.readthedocs.io/en/latest/i18n.html for documentation
Diffstat (limited to 'src/Renderer')
-rw-r--r--src/Renderer/Twig/Extensions/Translation.php57
-rw-r--r--src/Renderer/TwigServiceProvider.php10
2 files changed, 63 insertions, 4 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];
+ }
+}
diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php
index 4a5b1de3..0f453989 100644
--- a/src/Renderer/TwigServiceProvider.php
+++ b/src/Renderer/TwigServiceProvider.php
@@ -6,6 +6,7 @@ use Engelsystem\Container\ServiceProvider;
use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Globals;
use Engelsystem\Renderer\Twig\Extensions\Session;
+use Engelsystem\Renderer\Twig\Extensions\Translation;
use Engelsystem\Renderer\Twig\Extensions\Url;
use Twig_Environment as Twig;
use Twig_LoaderInterface as TwigLoaderInterface;
@@ -14,10 +15,11 @@ class TwigServiceProvider extends ServiceProvider
{
/** @var array */
protected $extensions = [
- 'config' => Config::class,
- 'globals' => Globals::class,
- 'session' => Session::class,
- 'url' => Url::class,
+ 'config' => Config::class,
+ 'globals' => Globals::class,
+ 'session' => Session::class,
+ 'url' => Url::class,
+ 'translation' => Translation::class,
];
public function register()