vendor/symfony/symfony/src/Symfony/Component/Translation/LoggingTranslator.php line 48

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. /**
  14.  * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  15.  */
  16. class LoggingTranslator implements TranslatorInterfaceTranslatorBagInterface
  17. {
  18.     /**
  19.      * @var TranslatorInterface|TranslatorBagInterface
  20.      */
  21.     private $translator;
  22.     private $logger;
  23.     /**
  24.      * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
  25.      * @param LoggerInterface     $logger
  26.      */
  27.     public function __construct(TranslatorInterface $translatorLoggerInterface $logger)
  28.     {
  29.         if (!$translator instanceof TranslatorBagInterface) {
  30.             throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.'get_class($translator)));
  31.         }
  32.         $this->translator $translator;
  33.         $this->logger $logger;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function trans($id, array $parameters = array(), $domain null$locale null)
  39.     {
  40.         $trans $this->translator->trans($id$parameters$domain$locale);
  41.         $this->log($id$domain$locale);
  42.         return $trans;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function transChoice($id$number, array $parameters = array(), $domain null$locale null)
  48.     {
  49.         $trans $this->translator->transChoice($id$number$parameters$domain$locale);
  50.         $this->log($id$domain$locale);
  51.         return $trans;
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function setLocale($locale)
  57.     {
  58.         $this->translator->setLocale($locale);
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function getLocale()
  64.     {
  65.         return $this->translator->getLocale();
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getCatalogue($locale null)
  71.     {
  72.         return $this->translator->getCatalogue($locale);
  73.     }
  74.     /**
  75.      * Gets the fallback locales.
  76.      *
  77.      * @return array $locales The fallback locales
  78.      */
  79.     public function getFallbackLocales()
  80.     {
  81.         if ($this->translator instanceof Translator || method_exists($this->translator'getFallbackLocales')) {
  82.             return $this->translator->getFallbackLocales();
  83.         }
  84.         return array();
  85.     }
  86.     /**
  87.      * Passes through all unknown calls onto the translator object.
  88.      */
  89.     public function __call($method$args)
  90.     {
  91.         return call_user_func_array(array($this->translator$method), $args);
  92.     }
  93.     /**
  94.      * Logs for missing translations.
  95.      *
  96.      * @param string      $id
  97.      * @param string|null $domain
  98.      * @param string|null $locale
  99.      */
  100.     private function log($id$domain$locale)
  101.     {
  102.         if (null === $domain) {
  103.             $domain 'messages';
  104.         }
  105.         $id = (string) $id;
  106.         $catalogue $this->translator->getCatalogue($locale);
  107.         if ($catalogue->defines($id$domain)) {
  108.             return;
  109.         }
  110.         if ($catalogue->has($id$domain)) {
  111.             $this->logger->debug('Translation use fallback catalogue.', array('id' => $id'domain' => $domain'locale' => $catalogue->getLocale()));
  112.         } else {
  113.             $this->logger->warning('Translation not found.', array('id' => $id'domain' => $domain'locale' => $catalogue->getLocale()));
  114.         }
  115.     }
  116. }