vendor/symfony/translation/LoggingTranslator.php line 76

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Translation;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  13. use Symfony\Contracts\Translation\LocaleAwareInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. /**
  16.  * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  17.  */
  18. class LoggingTranslator implements TranslatorInterfaceTranslatorBagInterfaceLocaleAwareInterface
  19. {
  20.     private $translator;
  21.     private $logger;
  22.     /**
  23.      * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator The translator must implement TranslatorBagInterface
  24.      */
  25.     public function __construct(TranslatorInterface $translatorLoggerInterface $logger)
  26.     {
  27.         if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
  28.             throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.'get_debug_type($translator)));
  29.         }
  30.         $this->translator $translator;
  31.         $this->logger $logger;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function trans(?string $id, array $parameters = [], string $domain nullstring $locale null)
  37.     {
  38.         $trans $this->translator->trans($id = (string) $id$parameters$domain$locale);
  39.         $this->log($id$domain$locale);
  40.         return $trans;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function setLocale(string $locale)
  46.     {
  47.         $prev $this->translator->getLocale();
  48.         $this->translator->setLocale($locale);
  49.         if ($prev === $locale) {
  50.             return;
  51.         }
  52.         $this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".'$prev$locale));
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function getLocale()
  58.     {
  59.         return $this->translator->getLocale();
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function getCatalogue(string $locale null)
  65.     {
  66.         return $this->translator->getCatalogue($locale);
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function getCatalogues(): array
  72.     {
  73.         return $this->translator->getCatalogues();
  74.     }
  75.     /**
  76.      * Gets the fallback locales.
  77.      *
  78.      * @return array
  79.      */
  80.     public function getFallbackLocales()
  81.     {
  82.         if ($this->translator instanceof Translator || method_exists($this->translator'getFallbackLocales')) {
  83.             return $this->translator->getFallbackLocales();
  84.         }
  85.         return [];
  86.     }
  87.     /**
  88.      * Passes through all unknown calls onto the translator object.
  89.      */
  90.     public function __call(string $method, array $args)
  91.     {
  92.         return $this->translator->{$method}(...$args);
  93.     }
  94.     /**
  95.      * Logs for missing translations.
  96.      */
  97.     private function log(string $id, ?string $domain, ?string $locale)
  98.     {
  99.         if (null === $domain) {
  100.             $domain 'messages';
  101.         }
  102.         $catalogue $this->translator->getCatalogue($locale);
  103.         if ($catalogue->defines($id$domain)) {
  104.             return;
  105.         }
  106.         if ($catalogue->has($id$domain)) {
  107.             $this->logger->debug('Translation use fallback catalogue.', ['id' => $id'domain' => $domain'locale' => $catalogue->getLocale()]);
  108.         } else {
  109.             $this->logger->warning('Translation not found.', ['id' => $id'domain' => $domain'locale' => $catalogue->getLocale()]);
  110.         }
  111.     }
  112. }