[ Index ]

PHP Cross Reference of phpBB-3.3.11-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 = null, NodeVisitorInterface $translationNodeVisitor = null)
  36      {
  37          $this->translator = $translator;
  38          $this->translationNodeVisitor = $translationNodeVisitor;
  39      }
  40  
  41      public function getTranslator()
  42      {
  43          return $this->translator;
  44      }
  45  
  46      /**
  47       * {@inheritdoc}
  48       */
  49      public function getFilters()
  50      {
  51          return [
  52              new TwigFilter('trans', [$this, 'trans']),
  53              new TwigFilter('transchoice', [$this, 'transchoice']),
  54          ];
  55      }
  56  
  57      /**
  58       * Returns the token parser instance to add to the existing list.
  59       *
  60       * @return AbstractTokenParser[]
  61       */
  62      public function getTokenParsers()
  63      {
  64          return [
  65              // {% trans %}Symfony is great!{% endtrans %}
  66              new TransTokenParser(),
  67  
  68              // {% transchoice count %}
  69              //     {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
  70              // {% endtranschoice %}
  71              new TransChoiceTokenParser(),
  72  
  73              // {% trans_default_domain "foobar" %}
  74              new TransDefaultDomainTokenParser(),
  75          ];
  76      }
  77  
  78      /**
  79       * {@inheritdoc}
  80       */
  81      public function getNodeVisitors()
  82      {
  83          return [$this->getTranslationNodeVisitor(), new TranslationDefaultDomainNodeVisitor()];
  84      }
  85  
  86      public function getTranslationNodeVisitor()
  87      {
  88          return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor();
  89      }
  90  
  91      public function trans($message, array $arguments = [], $domain = null, $locale = null)
  92      {
  93          if (null === $this->translator) {
  94              return strtr($message, $arguments);
  95          }
  96  
  97          return $this->translator->trans($message, $arguments, $domain, $locale);
  98      }
  99  
 100      public function transchoice($message, $count, array $arguments = [], $domain = null, $locale = null)
 101      {
 102          if (null === $this->translator) {
 103              return strtr($message, $arguments);
 104          }
 105  
 106          return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
 107      }
 108  
 109      /**
 110       * {@inheritdoc}
 111       */
 112      public function getName()
 113      {
 114          return 'translator';
 115      }
 116  }


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1