[TwigBundle] moved translator helpers to their own extension (removed usage of the magic _view variable context)
This commit is contained in:
parent
1a055fc7f0
commit
4297609156
@ -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', '<name>', '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(),
|
||||
);
|
||||
}
|
||||
|
||||
|
63
src/Symfony/Bundle/TwigBundle/Extension/TransExtension.php
Normal file
63
src/Symfony/Bundle/TwigBundle/Extension/TransExtension.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Bundle\TwigBundle\Extension;
|
||||
|
||||
use Symfony\Bundle\TwigBundle\TokenParser\TransTokenParser;
|
||||
use Symfony\Bundle\TwigBundle\TokenParser\TransChoiceTokenParser;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
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';
|
||||
}
|
||||
}
|
@ -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)
|
||||
;
|
||||
|
||||
|
@ -37,6 +37,11 @@
|
||||
<tag name="twig.extension" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.trans" class="Symfony\Bundle\TwigBundle\Extension\TransExtension">
|
||||
<tag name="twig.extension" />
|
||||
<argument type="service" id="translator" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\HelpersExtension">
|
||||
<tag name="twig.extension" />
|
||||
</service>
|
||||
|
Reference in New Issue
Block a user