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