Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

TranslationExtension.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 3.24 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Bridge\Twig\Extension;
013   
014  use Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor;
015  use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
016  use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
017  use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
018  use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
019  use Symfony\Component\Translation\TranslatorInterface;
020  use Twig\Extension\AbstractExtension;
021  use Twig\NodeVisitor\NodeVisitorInterface;
022  use Twig\TokenParser\AbstractTokenParser;
023  use Twig\TwigFilter;
024   
025  /**
026   * Provides integration of the Translation component with Twig.
027   *
028   * @author Fabien Potencier <fabien@symfony.com>
029   */
030  class TranslationExtension extends AbstractExtension
031  {
032      private $translator;
033      private $translationNodeVisitor;
034   
035      public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
036      {
037          $this->translator = $translator;
038          $this->translationNodeVisitor = $translationNodeVisitor;
039      }
040   
041      public function getTranslator()
042      {
043          return $this->translator;
044      }
045   
046      /**
047       * {@inheritdoc}
048       */
049      public function getFilters()
050      {
051          return [
052              new TwigFilter('trans', [$this, 'trans']),
053              new TwigFilter('transchoice', [$this, 'transchoice']),
054          ];
055      }
056   
057      /**
058       * Returns the token parser instance to add to the existing list.
059       *
060       * @return AbstractTokenParser[]
061       */
062      public function getTokenParsers()
063      {
064          return [
065              // {% trans %}Symfony is great!{% endtrans %}
066              new TransTokenParser(),
067   
068              // {% transchoice count %}
069              //     {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
070              // {% endtranschoice %}
071              new TransChoiceTokenParser(),
072   
073              // {% trans_default_domain "foobar" %}
074              new TransDefaultDomainTokenParser(),
075          ];
076      }
077   
078      /**
079       * {@inheritdoc}
080       */
081      public function getNodeVisitors()
082      {
083          return [$this->getTranslationNodeVisitor(), new TranslationDefaultDomainNodeVisitor()];
084      }
085   
086      public function getTranslationNodeVisitor()
087      {
088          return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor();
089      }
090   
091      public function trans($message, array $arguments = [], $domain = null, $locale = null)
092      {
093          if (null === $this->translator) {
094              return strtr($message, $arguments);
095          }
096   
097          return $this->translator->trans($message, $arguments, $domain, $locale);
098      }
099   
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  }
117