1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,157 +0,0 @@ |
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 |
-use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; |
|
17 |
-use Symfony\Contracts\Translation\LocaleAwareInterface; |
|
18 |
-use Symfony\Contracts\Translation\TranslatorInterface; |
|
19 |
- |
|
20 |
-/** |
|
21 |
- * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> |
|
22 |
- */ |
|
23 |
-class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterface, TranslatorBagInterface |
|
24 |
-{ |
|
25 |
- /** |
|
26 |
- * @var TranslatorInterface|TranslatorBagInterface |
|
27 |
- */ |
|
28 |
- private $translator; |
|
29 |
- |
|
30 |
- private $logger; |
|
31 |
- |
|
32 |
- /** |
|
33 |
- * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface |
|
34 |
- * @param LoggerInterface $logger |
|
35 |
- */ |
|
36 |
- public function __construct($translator, LoggerInterface $logger) |
|
37 |
- { |
|
38 |
- if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { |
|
39 |
- throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator))); |
|
40 |
- } |
|
41 |
- if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) { |
|
42 |
- throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator))); |
|
43 |
- } |
|
44 |
- |
|
45 |
- $this->translator = $translator; |
|
46 |
- $this->logger = $logger; |
|
47 |
- } |
|
48 |
- |
|
49 |
- /** |
|
50 |
- * {@inheritdoc} |
|
51 |
- */ |
|
52 |
- public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
53 |
- { |
|
54 |
- $trans = $this->translator->trans($id, $parameters, $domain, $locale); |
|
55 |
- $this->log($id, $domain, $locale); |
|
56 |
- |
|
57 |
- return $trans; |
|
58 |
- } |
|
59 |
- |
|
60 |
- /** |
|
61 |
- * {@inheritdoc} |
|
62 |
- * |
|
63 |
- * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter |
|
64 |
- */ |
|
65 |
- public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
|
66 |
- { |
|
67 |
- @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED); |
|
68 |
- |
|
69 |
- if ($this->translator instanceof TranslatorInterface) { |
|
70 |
- $trans = $this->translator->trans($id, ['%count%' => $number] + $parameters, $domain, $locale); |
|
71 |
- } else { |
|
72 |
- $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale); |
|
73 |
- } |
|
74 |
- |
|
75 |
- $this->log($id, $domain, $locale); |
|
76 |
- |
|
77 |
- return $trans; |
|
78 |
- } |
|
79 |
- |
|
80 |
- /** |
|
81 |
- * {@inheritdoc} |
|
82 |
- */ |
|
83 |
- public function setLocale($locale) |
|
84 |
- { |
|
85 |
- $prev = $this->translator->getLocale(); |
|
86 |
- $this->translator->setLocale($locale); |
|
87 |
- if ($prev === $locale) { |
|
88 |
- return; |
|
89 |
- } |
|
90 |
- |
|
91 |
- $this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale)); |
|
92 |
- } |
|
93 |
- |
|
94 |
- /** |
|
95 |
- * {@inheritdoc} |
|
96 |
- */ |
|
97 |
- public function getLocale() |
|
98 |
- { |
|
99 |
- return $this->translator->getLocale(); |
|
100 |
- } |
|
101 |
- |
|
102 |
- /** |
|
103 |
- * {@inheritdoc} |
|
104 |
- */ |
|
105 |
- public function getCatalogue($locale = null) |
|
106 |
- { |
|
107 |
- return $this->translator->getCatalogue($locale); |
|
108 |
- } |
|
109 |
- |
|
110 |
- /** |
|
111 |
- * Gets the fallback locales. |
|
112 |
- * |
|
113 |
- * @return array The fallback locales |
|
114 |
- */ |
|
115 |
- public function getFallbackLocales() |
|
116 |
- { |
|
117 |
- if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { |
|
118 |
- return $this->translator->getFallbackLocales(); |
|
119 |
- } |
|
120 |
- |
|
121 |
- return []; |
|
122 |
- } |
|
123 |
- |
|
124 |
- /** |
|
125 |
- * Passes through all unknown calls onto the translator object. |
|
126 |
- */ |
|
127 |
- public function __call($method, $args) |
|
128 |
- { |
|
129 |
- return $this->translator->{$method}(...$args); |
|
130 |
- } |
|
131 |
- |
|
132 |
- /** |
|
133 |
- * Logs for missing translations. |
|
134 |
- * |
|
135 |
- * @param string $id |
|
136 |
- * @param string|null $domain |
|
137 |
- * @param string|null $locale |
|
138 |
- */ |
|
139 |
- private function log($id, $domain, $locale) |
|
140 |
- { |
|
141 |
- if (null === $domain) { |
|
142 |
- $domain = 'messages'; |
|
143 |
- } |
|
144 |
- |
|
145 |
- $id = (string) $id; |
|
146 |
- $catalogue = $this->translator->getCatalogue($locale); |
|
147 |
- if ($catalogue->defines($id, $domain)) { |
|
148 |
- return; |
|
149 |
- } |
|
150 |
- |
|
151 |
- if ($catalogue->has($id, $domain)) { |
|
152 |
- $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]); |
|
153 |
- } else { |
|
154 |
- $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]); |
|
155 |
- } |
|
156 |
- } |
|
157 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,157 @@ |
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 |
+use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; |
|
17 |
+use Symfony\Contracts\Translation\LocaleAwareInterface; |
|
18 |
+use Symfony\Contracts\Translation\TranslatorInterface; |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> |
|
22 |
+ */ |
|
23 |
+class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterface, TranslatorBagInterface |
|
24 |
+{ |
|
25 |
+ /** |
|
26 |
+ * @var TranslatorInterface|TranslatorBagInterface |
|
27 |
+ */ |
|
28 |
+ private $translator; |
|
29 |
+ |
|
30 |
+ private $logger; |
|
31 |
+ |
|
32 |
+ /** |
|
33 |
+ * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface |
|
34 |
+ * @param LoggerInterface $logger |
|
35 |
+ */ |
|
36 |
+ public function __construct($translator, LoggerInterface $logger) |
|
37 |
+ { |
|
38 |
+ if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { |
|
39 |
+ throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator))); |
|
40 |
+ } |
|
41 |
+ if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) { |
|
42 |
+ throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator))); |
|
43 |
+ } |
|
44 |
+ |
|
45 |
+ $this->translator = $translator; |
|
46 |
+ $this->logger = $logger; |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+ /** |
|
50 |
+ * {@inheritdoc} |
|
51 |
+ */ |
|
52 |
+ public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
53 |
+ { |
|
54 |
+ $trans = $this->translator->trans($id, $parameters, $domain, $locale); |
|
55 |
+ $this->log($id, $domain, $locale); |
|
56 |
+ |
|
57 |
+ return $trans; |
|
58 |
+ } |
|
59 |
+ |
|
60 |
+ /** |
|
61 |
+ * {@inheritdoc} |
|
62 |
+ * |
|
63 |
+ * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter |
|
64 |
+ */ |
|
65 |
+ public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
|
66 |
+ { |
|
67 |
+ @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED); |
|
68 |
+ |
|
69 |
+ if ($this->translator instanceof TranslatorInterface) { |
|
70 |
+ $trans = $this->translator->trans($id, ['%count%' => $number] + $parameters, $domain, $locale); |
|
71 |
+ } else { |
|
72 |
+ $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale); |
|
73 |
+ } |
|
74 |
+ |
|
75 |
+ $this->log($id, $domain, $locale); |
|
76 |
+ |
|
77 |
+ return $trans; |
|
78 |
+ } |
|
79 |
+ |
|
80 |
+ /** |
|
81 |
+ * {@inheritdoc} |
|
82 |
+ */ |
|
83 |
+ public function setLocale($locale) |
|
84 |
+ { |
|
85 |
+ $prev = $this->translator->getLocale(); |
|
86 |
+ $this->translator->setLocale($locale); |
|
87 |
+ if ($prev === $locale) { |
|
88 |
+ return; |
|
89 |
+ } |
|
90 |
+ |
|
91 |
+ $this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale)); |
|
92 |
+ } |
|
93 |
+ |
|
94 |
+ /** |
|
95 |
+ * {@inheritdoc} |
|
96 |
+ */ |
|
97 |
+ public function getLocale() |
|
98 |
+ { |
|
99 |
+ return $this->translator->getLocale(); |
|
100 |
+ } |
|
101 |
+ |
|
102 |
+ /** |
|
103 |
+ * {@inheritdoc} |
|
104 |
+ */ |
|
105 |
+ public function getCatalogue($locale = null) |
|
106 |
+ { |
|
107 |
+ return $this->translator->getCatalogue($locale); |
|
108 |
+ } |
|
109 |
+ |
|
110 |
+ /** |
|
111 |
+ * Gets the fallback locales. |
|
112 |
+ * |
|
113 |
+ * @return array The fallback locales |
|
114 |
+ */ |
|
115 |
+ public function getFallbackLocales() |
|
116 |
+ { |
|
117 |
+ if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { |
|
118 |
+ return $this->translator->getFallbackLocales(); |
|
119 |
+ } |
|
120 |
+ |
|
121 |
+ return []; |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+ /** |
|
125 |
+ * Passes through all unknown calls onto the translator object. |
|
126 |
+ */ |
|
127 |
+ public function __call($method, $args) |
|
128 |
+ { |
|
129 |
+ return $this->translator->{$method}(...$args); |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ /** |
|
133 |
+ * Logs for missing translations. |
|
134 |
+ * |
|
135 |
+ * @param string $id |
|
136 |
+ * @param string|null $domain |
|
137 |
+ * @param string|null $locale |
|
138 |
+ */ |
|
139 |
+ private function log($id, $domain, $locale) |
|
140 |
+ { |
|
141 |
+ if (null === $domain) { |
|
142 |
+ $domain = 'messages'; |
|
143 |
+ } |
|
144 |
+ |
|
145 |
+ $id = (string) $id; |
|
146 |
+ $catalogue = $this->translator->getCatalogue($locale); |
|
147 |
+ if ($catalogue->defines($id, $domain)) { |
|
148 |
+ return; |
|
149 |
+ } |
|
150 |
+ |
|
151 |
+ if ($catalogue->has($id, $domain)) { |
|
152 |
+ $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]); |
|
153 |
+ } else { |
|
154 |
+ $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]); |
|
155 |
+ } |
|
156 |
+ } |
|
157 |
+} |