1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,188 +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 Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
|
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 DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface, WarmableInterface |
|
24 |
-{ |
|
25 |
- const MESSAGE_DEFINED = 0; |
|
26 |
- const MESSAGE_MISSING = 1; |
|
27 |
- const MESSAGE_EQUALS_FALLBACK = 2; |
|
28 |
- |
|
29 |
- /** |
|
30 |
- * @var TranslatorInterface|TranslatorBagInterface |
|
31 |
- */ |
|
32 |
- private $translator; |
|
33 |
- |
|
34 |
- private $messages = []; |
|
35 |
- |
|
36 |
- /** |
|
37 |
- * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface |
|
38 |
- */ |
|
39 |
- public function __construct($translator) |
|
40 |
- { |
|
41 |
- if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { |
|
42 |
- 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))); |
|
43 |
- } |
|
44 |
- if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) { |
|
45 |
- throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator))); |
|
46 |
- } |
|
47 |
- |
|
48 |
- $this->translator = $translator; |
|
49 |
- } |
|
50 |
- |
|
51 |
- /** |
|
52 |
- * {@inheritdoc} |
|
53 |
- */ |
|
54 |
- public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
55 |
- { |
|
56 |
- $trans = $this->translator->trans($id, $parameters, $domain, $locale); |
|
57 |
- $this->collectMessage($locale, $domain, $id, $trans, $parameters); |
|
58 |
- |
|
59 |
- return $trans; |
|
60 |
- } |
|
61 |
- |
|
62 |
- /** |
|
63 |
- * {@inheritdoc} |
|
64 |
- * |
|
65 |
- * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter |
|
66 |
- */ |
|
67 |
- public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
|
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->collectMessage($locale, $domain, $id, $trans, ['%count%' => $number] + $parameters); |
|
76 |
- |
|
77 |
- return $trans; |
|
78 |
- } |
|
79 |
- |
|
80 |
- /** |
|
81 |
- * {@inheritdoc} |
|
82 |
- */ |
|
83 |
- public function setLocale($locale) |
|
84 |
- { |
|
85 |
- $this->translator->setLocale($locale); |
|
86 |
- } |
|
87 |
- |
|
88 |
- /** |
|
89 |
- * {@inheritdoc} |
|
90 |
- */ |
|
91 |
- public function getLocale() |
|
92 |
- { |
|
93 |
- return $this->translator->getLocale(); |
|
94 |
- } |
|
95 |
- |
|
96 |
- /** |
|
97 |
- * {@inheritdoc} |
|
98 |
- */ |
|
99 |
- public function getCatalogue($locale = null) |
|
100 |
- { |
|
101 |
- return $this->translator->getCatalogue($locale); |
|
102 |
- } |
|
103 |
- |
|
104 |
- /** |
|
105 |
- * {@inheritdoc} |
|
106 |
- */ |
|
107 |
- public function warmUp($cacheDir) |
|
108 |
- { |
|
109 |
- if ($this->translator instanceof WarmableInterface) { |
|
110 |
- $this->translator->warmUp($cacheDir); |
|
111 |
- } |
|
112 |
- } |
|
113 |
- |
|
114 |
- /** |
|
115 |
- * Gets the fallback locales. |
|
116 |
- * |
|
117 |
- * @return array The fallback locales |
|
118 |
- */ |
|
119 |
- public function getFallbackLocales() |
|
120 |
- { |
|
121 |
- if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { |
|
122 |
- return $this->translator->getFallbackLocales(); |
|
123 |
- } |
|
124 |
- |
|
125 |
- return []; |
|
126 |
- } |
|
127 |
- |
|
128 |
- /** |
|
129 |
- * Passes through all unknown calls onto the translator object. |
|
130 |
- */ |
|
131 |
- public function __call($method, $args) |
|
132 |
- { |
|
133 |
- return $this->translator->{$method}(...$args); |
|
134 |
- } |
|
135 |
- |
|
136 |
- /** |
|
137 |
- * @return array |
|
138 |
- */ |
|
139 |
- public function getCollectedMessages() |
|
140 |
- { |
|
141 |
- return $this->messages; |
|
142 |
- } |
|
143 |
- |
|
144 |
- /** |
|
145 |
- * @param string|null $locale |
|
146 |
- * @param string|null $domain |
|
147 |
- * @param string $id |
|
148 |
- * @param string $translation |
|
149 |
- * @param array|null $parameters |
|
150 |
- */ |
|
151 |
- private function collectMessage($locale, $domain, $id, $translation, $parameters = []) |
|
152 |
- { |
|
153 |
- if (null === $domain) { |
|
154 |
- $domain = 'messages'; |
|
155 |
- } |
|
156 |
- |
|
157 |
- $id = (string) $id; |
|
158 |
- $catalogue = $this->translator->getCatalogue($locale); |
|
159 |
- $locale = $catalogue->getLocale(); |
|
160 |
- if ($catalogue->defines($id, $domain)) { |
|
161 |
- $state = self::MESSAGE_DEFINED; |
|
162 |
- } elseif ($catalogue->has($id, $domain)) { |
|
163 |
- $state = self::MESSAGE_EQUALS_FALLBACK; |
|
164 |
- |
|
165 |
- $fallbackCatalogue = $catalogue->getFallbackCatalogue(); |
|
166 |
- while ($fallbackCatalogue) { |
|
167 |
- if ($fallbackCatalogue->defines($id, $domain)) { |
|
168 |
- $locale = $fallbackCatalogue->getLocale(); |
|
169 |
- break; |
|
170 |
- } |
|
171 |
- |
|
172 |
- $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); |
|
173 |
- } |
|
174 |
- } else { |
|
175 |
- $state = self::MESSAGE_MISSING; |
|
176 |
- } |
|
177 |
- |
|
178 |
- $this->messages[] = [ |
|
179 |
- 'locale' => $locale, |
|
180 |
- 'domain' => $domain, |
|
181 |
- 'id' => $id, |
|
182 |
- 'translation' => $translation, |
|
183 |
- 'parameters' => $parameters, |
|
184 |
- 'state' => $state, |
|
185 |
- 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null, |
|
186 |
- ]; |
|
187 |
- } |
|
188 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,188 @@ |
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 Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
|
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 DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface, WarmableInterface |
|
24 |
+{ |
|
25 |
+ const MESSAGE_DEFINED = 0; |
|
26 |
+ const MESSAGE_MISSING = 1; |
|
27 |
+ const MESSAGE_EQUALS_FALLBACK = 2; |
|
28 |
+ |
|
29 |
+ /** |
|
30 |
+ * @var TranslatorInterface|TranslatorBagInterface |
|
31 |
+ */ |
|
32 |
+ private $translator; |
|
33 |
+ |
|
34 |
+ private $messages = []; |
|
35 |
+ |
|
36 |
+ /** |
|
37 |
+ * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface |
|
38 |
+ */ |
|
39 |
+ public function __construct($translator) |
|
40 |
+ { |
|
41 |
+ if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { |
|
42 |
+ 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))); |
|
43 |
+ } |
|
44 |
+ if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) { |
|
45 |
+ throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator))); |
|
46 |
+ } |
|
47 |
+ |
|
48 |
+ $this->translator = $translator; |
|
49 |
+ } |
|
50 |
+ |
|
51 |
+ /** |
|
52 |
+ * {@inheritdoc} |
|
53 |
+ */ |
|
54 |
+ public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
55 |
+ { |
|
56 |
+ $trans = $this->translator->trans($id, $parameters, $domain, $locale); |
|
57 |
+ $this->collectMessage($locale, $domain, $id, $trans, $parameters); |
|
58 |
+ |
|
59 |
+ return $trans; |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ /** |
|
63 |
+ * {@inheritdoc} |
|
64 |
+ * |
|
65 |
+ * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter |
|
66 |
+ */ |
|
67 |
+ public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
|
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->collectMessage($locale, $domain, $id, $trans, ['%count%' => $number] + $parameters); |
|
76 |
+ |
|
77 |
+ return $trans; |
|
78 |
+ } |
|
79 |
+ |
|
80 |
+ /** |
|
81 |
+ * {@inheritdoc} |
|
82 |
+ */ |
|
83 |
+ public function setLocale($locale) |
|
84 |
+ { |
|
85 |
+ $this->translator->setLocale($locale); |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ /** |
|
89 |
+ * {@inheritdoc} |
|
90 |
+ */ |
|
91 |
+ public function getLocale() |
|
92 |
+ { |
|
93 |
+ return $this->translator->getLocale(); |
|
94 |
+ } |
|
95 |
+ |
|
96 |
+ /** |
|
97 |
+ * {@inheritdoc} |
|
98 |
+ */ |
|
99 |
+ public function getCatalogue($locale = null) |
|
100 |
+ { |
|
101 |
+ return $this->translator->getCatalogue($locale); |
|
102 |
+ } |
|
103 |
+ |
|
104 |
+ /** |
|
105 |
+ * {@inheritdoc} |
|
106 |
+ */ |
|
107 |
+ public function warmUp($cacheDir) |
|
108 |
+ { |
|
109 |
+ if ($this->translator instanceof WarmableInterface) { |
|
110 |
+ $this->translator->warmUp($cacheDir); |
|
111 |
+ } |
|
112 |
+ } |
|
113 |
+ |
|
114 |
+ /** |
|
115 |
+ * Gets the fallback locales. |
|
116 |
+ * |
|
117 |
+ * @return array The fallback locales |
|
118 |
+ */ |
|
119 |
+ public function getFallbackLocales() |
|
120 |
+ { |
|
121 |
+ if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { |
|
122 |
+ return $this->translator->getFallbackLocales(); |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ return []; |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ /** |
|
129 |
+ * Passes through all unknown calls onto the translator object. |
|
130 |
+ */ |
|
131 |
+ public function __call($method, $args) |
|
132 |
+ { |
|
133 |
+ return $this->translator->{$method}(...$args); |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ /** |
|
137 |
+ * @return array |
|
138 |
+ */ |
|
139 |
+ public function getCollectedMessages() |
|
140 |
+ { |
|
141 |
+ return $this->messages; |
|
142 |
+ } |
|
143 |
+ |
|
144 |
+ /** |
|
145 |
+ * @param string|null $locale |
|
146 |
+ * @param string|null $domain |
|
147 |
+ * @param string $id |
|
148 |
+ * @param string $translation |
|
149 |
+ * @param array|null $parameters |
|
150 |
+ */ |
|
151 |
+ private function collectMessage($locale, $domain, $id, $translation, $parameters = []) |
|
152 |
+ { |
|
153 |
+ if (null === $domain) { |
|
154 |
+ $domain = 'messages'; |
|
155 |
+ } |
|
156 |
+ |
|
157 |
+ $id = (string) $id; |
|
158 |
+ $catalogue = $this->translator->getCatalogue($locale); |
|
159 |
+ $locale = $catalogue->getLocale(); |
|
160 |
+ if ($catalogue->defines($id, $domain)) { |
|
161 |
+ $state = self::MESSAGE_DEFINED; |
|
162 |
+ } elseif ($catalogue->has($id, $domain)) { |
|
163 |
+ $state = self::MESSAGE_EQUALS_FALLBACK; |
|
164 |
+ |
|
165 |
+ $fallbackCatalogue = $catalogue->getFallbackCatalogue(); |
|
166 |
+ while ($fallbackCatalogue) { |
|
167 |
+ if ($fallbackCatalogue->defines($id, $domain)) { |
|
168 |
+ $locale = $fallbackCatalogue->getLocale(); |
|
169 |
+ break; |
|
170 |
+ } |
|
171 |
+ |
|
172 |
+ $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); |
|
173 |
+ } |
|
174 |
+ } else { |
|
175 |
+ $state = self::MESSAGE_MISSING; |
|
176 |
+ } |
|
177 |
+ |
|
178 |
+ $this->messages[] = [ |
|
179 |
+ 'locale' => $locale, |
|
180 |
+ 'domain' => $domain, |
|
181 |
+ 'id' => $id, |
|
182 |
+ 'translation' => $translation, |
|
183 |
+ 'parameters' => $parameters, |
|
184 |
+ 'state' => $state, |
|
185 |
+ 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null, |
|
186 |
+ ]; |
|
187 |
+ } |
|
188 |
+} |