123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?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;
- }
- /**
- * {@inheritdoc}
- */
- public function getFilters()
- {
- return array(
- 'trans' => new \Twig_Filter_Method($this, 'trans'),
- );
- }
- /**
- * 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(),
- );
- }
- public function trans($message, array $arguments = array(), $domain = "messages")
- {
- return $this->translator->trans($message, $arguments, $domain);
- }
- /**
- * Returns the name of the extension.
- *
- * @return string The extension name
- */
- public function getName()
- {
- return 'translator';
- }
- }
|