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