comparison vendor/symfony/translation/LoggingTranslator.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 /**
28 * @var LoggerInterface
29 */
30 private $logger;
31
32 /**
33 * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
34 * @param LoggerInterface $logger
35 */
36 public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
37 {
38 if (!$translator instanceof TranslatorBagInterface) {
39 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
40 }
41
42 $this->translator = $translator;
43 $this->logger = $logger;
44 }
45
46 /**
47 * {@inheritdoc}
48 */
49 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
50 {
51 $trans = $this->translator->trans($id, $parameters, $domain, $locale);
52 $this->log($id, $domain, $locale);
53
54 return $trans;
55 }
56
57 /**
58 * {@inheritdoc}
59 */
60 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
61 {
62 $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
63 $this->log($id, $domain, $locale);
64
65 return $trans;
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function setLocale($locale)
72 {
73 $this->translator->setLocale($locale);
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function getLocale()
80 {
81 return $this->translator->getLocale();
82 }
83
84 /**
85 * {@inheritdoc}
86 */
87 public function getCatalogue($locale = null)
88 {
89 return $this->translator->getCatalogue($locale);
90 }
91
92 /**
93 * Gets the fallback locales.
94 *
95 * @return array $locales The fallback locales
96 */
97 public function getFallbackLocales()
98 {
99 if ($this->translator instanceof Translator) {
100 return $this->translator->getFallbackLocales();
101 }
102
103 return array();
104 }
105
106 /**
107 * Passes through all unknown calls onto the translator object.
108 */
109 public function __call($method, $args)
110 {
111 return call_user_func_array(array($this->translator, $method), $args);
112 }
113
114 /**
115 * Logs for missing translations.
116 *
117 * @param string $id
118 * @param string|null $domain
119 * @param string|null $locale
120 */
121 private function log($id, $domain, $locale)
122 {
123 if (null === $domain) {
124 $domain = 'messages';
125 }
126
127 $id = (string) $id;
128 $catalogue = $this->translator->getCatalogue($locale);
129 if ($catalogue->defines($id, $domain)) {
130 return;
131 }
132
133 if ($catalogue->has($id, $domain)) {
134 $this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
135 } else {
136 $this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
137 }
138 }
139 }