From 42976091563c59cd9c8f9f87e8c2a8dcdc3a4f40 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 30 Sep 2010 10:14:28 +0200 Subject: [PATCH] [TwigBundle] moved translator helpers to their own extension (removed usage of the magic _view variable context) --- .../TwigBundle/Extension/HelpersExtension.php | 11 ---- .../TwigBundle/Extension/TransExtension.php | 63 +++++++++++++++++++ .../Bundle/TwigBundle/Node/TransNode.php | 2 +- .../TwigBundle/Resources/config/twig.xml | 5 ++ 4 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Extension/TransExtension.php diff --git a/src/Symfony/Bundle/TwigBundle/Extension/HelpersExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/HelpersExtension.php index e99b6bdace..c70d8f73a7 100644 --- a/src/Symfony/Bundle/TwigBundle/Extension/HelpersExtension.php +++ b/src/Symfony/Bundle/TwigBundle/Extension/HelpersExtension.php @@ -2,10 +2,7 @@ namespace Symfony\Bundle\TwigBundle\Extension; -use Symfony\Component\Templating\Engine; use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser; -use Symfony\Bundle\TwigBundle\TokenParser\TransTokenParser; -use Symfony\Bundle\TwigBundle\TokenParser\TransChoiceTokenParser; /* * This file is part of the Symfony package. @@ -53,14 +50,6 @@ class HelpersExtension extends \Twig_Extension // {% flash 'notice' %} new HelperTokenParser('flash', '', 'session', 'flash'), - - // {% trans "Symfony is great!" %} - new TransTokenParser(), - - // {% transchoice count %} - // {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples - // {% endtranschoice %} - new TransChoiceTokenParser(), ); } diff --git a/src/Symfony/Bundle/TwigBundle/Extension/TransExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/TransExtension.php new file mode 100644 index 0000000000..fdf85b3bbe --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Extension/TransExtension.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * @author Fabien Potencier + */ +class TransExtension extends \Twig_Extension +{ + protected $translator; + + public function __construct(TranslatorInterface $translator) + { + $this->translator = $translator; + } + + public function getTranslator() + { + return $this->translator; + } + + /** + * Returns the token parser instance to add to the existing list. + * + * @return array An array of Twig_TokenParser instances + */ + public function getTokenParsers() + { + return array( + // {% trans "Symfony is great!" %} + new TransTokenParser(), + + // {% transchoice count %} + // {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples + // {% endtranschoice %} + new TransChoiceTokenParser(), + ); + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'translator'; + } +} diff --git a/src/Symfony/Bundle/TwigBundle/Node/TransNode.php b/src/Symfony/Bundle/TwigBundle/Node/TransNode.php index daf5f3e992..a9a8d2bddf 100644 --- a/src/Symfony/Bundle/TwigBundle/Node/TransNode.php +++ b/src/Symfony/Bundle/TwigBundle/Node/TransNode.php @@ -37,7 +37,7 @@ class TransNode extends \Twig_Node $method = null === $this->count ? 'trans' : 'transChoice'; $compiler - ->write('echo $context[\'_view\'][\'translator\']->'.$method.'(') + ->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(') ->subcompile($msg) ; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index fd529afaad..a03360bdc1 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -37,6 +37,11 @@ + + + + +