[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> TranslationExtension.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Bridge\Twig\Extension;
  13  
  14  use Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor;
  15  use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
  16  use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
  17  use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
  18  use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
  19  use Symfony\Component\Translation\TranslatorInterface;
  20  use Twig\Extension\AbstractExtension;
  21  use Twig\NodeVisitor\NodeVisitorInterface;
  22  use Twig\TokenParser\AbstractTokenParser;
  23  use Twig\TwigFilter;
  24  
  25  /**
  26   * Provides integration of the Translation component with Twig.
  27   *
  28   * @author Fabien Potencier <fabien@symfony.com>
  29   */
  30  class TranslationExtension extends AbstractExtension
  31  {
  32      private $translator;
  33      private $translationNodeVisitor;
  34  
  35      public function __construct(TranslatorInterface $translator, NodeVisitorInterface $translationNodeVisitor = null)
  36      {
  37          if (!$translationNodeVisitor) {
  38              $translationNodeVisitor = new TranslationNodeVisitor();
  39          }
  40  
  41          $this->translator = $translator;
  42          $this->translationNodeVisitor = $translationNodeVisitor;
  43      }
  44  
  45      public function getTranslator()
  46      {
  47          return $this->translator;
  48      }
  49  
  50      /**
  51       * {@inheritdoc}
  52       */
  53      public function getFilters()
  54      {
  55          return array(
  56              new TwigFilter('trans', array($this, 'trans')),
  57              new TwigFilter('transchoice', array($this, 'transchoice')),
  58          );
  59      }
  60  
  61      /**
  62       * Returns the token parser instance to add to the existing list.
  63       *
  64       * @return AbstractTokenParser[]
  65       */
  66      public function getTokenParsers()
  67      {
  68          return array(
  69              // {% trans %}Symfony is great!{% endtrans %}
  70              new TransTokenParser(),
  71  
  72              // {% transchoice count %}
  73              //     {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
  74              // {% endtranschoice %}
  75              new TransChoiceTokenParser(),
  76  
  77              // {% trans_default_domain "foobar" %}
  78              new TransDefaultDomainTokenParser(),
  79          );
  80      }
  81  
  82      /**
  83       * {@inheritdoc}
  84       */
  85      public function getNodeVisitors()
  86      {
  87          return array($this->translationNodeVisitor, new TranslationDefaultDomainNodeVisitor());
  88      }
  89  
  90      public function getTranslationNodeVisitor()
  91      {
  92          return $this->translationNodeVisitor;
  93      }
  94  
  95      public function trans($message, array $arguments = array(), $domain = null, $locale = null)
  96      {
  97          return $this->translator->trans($message, $arguments, $domain, $locale);
  98      }
  99  
 100      public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
 101      {
 102          return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
 103      }
 104  
 105      /**
 106       * {@inheritdoc}
 107       */
 108      public function getName()
 109      {
 110          return 'translator';
 111      }
 112  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1