Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Translation;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Config\ConfigCacheFactory;
|
Chris@17
|
15 use Symfony\Component\Config\ConfigCacheFactoryInterface;
|
Chris@17
|
16 use Symfony\Component\Config\ConfigCacheInterface;
|
Chris@0
|
17 use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
Chris@14
|
18 use Symfony\Component\Translation\Exception\LogicException;
|
Chris@17
|
19 use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
Chris@0
|
20 use Symfony\Component\Translation\Exception\RuntimeException;
|
Chris@14
|
21 use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface;
|
Chris@14
|
22 use Symfony\Component\Translation\Formatter\MessageFormatter;
|
Chris@17
|
23 use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
|
Chris@17
|
24 use Symfony\Component\Translation\Loader\LoaderInterface;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
28 */
|
Chris@0
|
29 class Translator implements TranslatorInterface, TranslatorBagInterface
|
Chris@0
|
30 {
|
Chris@0
|
31 /**
|
Chris@0
|
32 * @var MessageCatalogueInterface[]
|
Chris@0
|
33 */
|
Chris@17
|
34 protected $catalogues = [];
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 private $locale;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * @var array
|
Chris@0
|
43 */
|
Chris@17
|
44 private $fallbackLocales = [];
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * @var LoaderInterface[]
|
Chris@0
|
48 */
|
Chris@17
|
49 private $loaders = [];
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * @var array
|
Chris@0
|
53 */
|
Chris@17
|
54 private $resources = [];
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@14
|
57 * @var MessageFormatterInterface
|
Chris@0
|
58 */
|
Chris@14
|
59 private $formatter;
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * @var string
|
Chris@0
|
63 */
|
Chris@0
|
64 private $cacheDir;
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * @var bool
|
Chris@0
|
68 */
|
Chris@0
|
69 private $debug;
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * @var ConfigCacheFactoryInterface|null
|
Chris@0
|
73 */
|
Chris@0
|
74 private $configCacheFactory;
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@14
|
77 * @param string $locale The locale
|
Chris@14
|
78 * @param MessageFormatterInterface|null $formatter The message formatter
|
Chris@14
|
79 * @param string|null $cacheDir The directory to use for the cache
|
Chris@14
|
80 * @param bool $debug Use cache in debug mode ?
|
Chris@0
|
81 *
|
Chris@0
|
82 * @throws InvalidArgumentException If a locale contains invalid characters
|
Chris@0
|
83 */
|
Chris@14
|
84 public function __construct($locale, $formatter = null, $cacheDir = null, $debug = false)
|
Chris@0
|
85 {
|
Chris@0
|
86 $this->setLocale($locale);
|
Chris@14
|
87
|
Chris@14
|
88 if ($formatter instanceof MessageSelector) {
|
Chris@14
|
89 $formatter = new MessageFormatter($formatter);
|
Chris@17
|
90 @trigger_error(sprintf('Passing a "%s" instance into the "%s()" method as a second argument is deprecated since Symfony 3.4 and will be removed in 4.0. Inject a "%s" implementation instead.', MessageSelector::class, __METHOD__, MessageFormatterInterface::class), E_USER_DEPRECATED);
|
Chris@14
|
91 } elseif (null === $formatter) {
|
Chris@14
|
92 $formatter = new MessageFormatter();
|
Chris@14
|
93 }
|
Chris@14
|
94
|
Chris@14
|
95 $this->formatter = $formatter;
|
Chris@0
|
96 $this->cacheDir = $cacheDir;
|
Chris@0
|
97 $this->debug = $debug;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
|
Chris@0
|
101 {
|
Chris@0
|
102 $this->configCacheFactory = $configCacheFactory;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Adds a Loader.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @param string $format The name of the loader (@see addResource())
|
Chris@0
|
109 * @param LoaderInterface $loader A LoaderInterface instance
|
Chris@0
|
110 */
|
Chris@0
|
111 public function addLoader($format, LoaderInterface $loader)
|
Chris@0
|
112 {
|
Chris@0
|
113 $this->loaders[$format] = $loader;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Adds a Resource.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param string $format The name of the loader (@see addLoader())
|
Chris@0
|
120 * @param mixed $resource The resource name
|
Chris@0
|
121 * @param string $locale The locale
|
Chris@0
|
122 * @param string $domain The domain
|
Chris@0
|
123 *
|
Chris@0
|
124 * @throws InvalidArgumentException If the locale contains invalid characters
|
Chris@0
|
125 */
|
Chris@0
|
126 public function addResource($format, $resource, $locale, $domain = null)
|
Chris@0
|
127 {
|
Chris@0
|
128 if (null === $domain) {
|
Chris@0
|
129 $domain = 'messages';
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 $this->assertValidLocale($locale);
|
Chris@0
|
133
|
Chris@17
|
134 $this->resources[$locale][] = [$format, $resource, $domain];
|
Chris@0
|
135
|
Chris@17
|
136 if (\in_array($locale, $this->fallbackLocales)) {
|
Chris@17
|
137 $this->catalogues = [];
|
Chris@0
|
138 } else {
|
Chris@0
|
139 unset($this->catalogues[$locale]);
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 public function setLocale($locale)
|
Chris@0
|
147 {
|
Chris@0
|
148 $this->assertValidLocale($locale);
|
Chris@0
|
149 $this->locale = $locale;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * {@inheritdoc}
|
Chris@0
|
154 */
|
Chris@0
|
155 public function getLocale()
|
Chris@0
|
156 {
|
Chris@0
|
157 return $this->locale;
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * Sets the fallback locales.
|
Chris@0
|
162 *
|
Chris@0
|
163 * @param array $locales The fallback locales
|
Chris@0
|
164 *
|
Chris@0
|
165 * @throws InvalidArgumentException If a locale contains invalid characters
|
Chris@0
|
166 */
|
Chris@0
|
167 public function setFallbackLocales(array $locales)
|
Chris@0
|
168 {
|
Chris@0
|
169 // needed as the fallback locales are linked to the already loaded catalogues
|
Chris@17
|
170 $this->catalogues = [];
|
Chris@0
|
171
|
Chris@0
|
172 foreach ($locales as $locale) {
|
Chris@0
|
173 $this->assertValidLocale($locale);
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 $this->fallbackLocales = $locales;
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@0
|
180 * Gets the fallback locales.
|
Chris@0
|
181 *
|
Chris@17
|
182 * @return array The fallback locales
|
Chris@0
|
183 */
|
Chris@0
|
184 public function getFallbackLocales()
|
Chris@0
|
185 {
|
Chris@0
|
186 return $this->fallbackLocales;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * {@inheritdoc}
|
Chris@0
|
191 */
|
Chris@17
|
192 public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
Chris@0
|
193 {
|
Chris@0
|
194 if (null === $domain) {
|
Chris@0
|
195 $domain = 'messages';
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@14
|
198 return $this->formatter->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters);
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 /**
|
Chris@0
|
202 * {@inheritdoc}
|
Chris@0
|
203 */
|
Chris@17
|
204 public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
|
Chris@0
|
205 {
|
Chris@14
|
206 if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
|
Chris@17
|
207 throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', \get_class($this->formatter)));
|
Chris@14
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 if (null === $domain) {
|
Chris@0
|
211 $domain = 'messages';
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 $id = (string) $id;
|
Chris@0
|
215 $catalogue = $this->getCatalogue($locale);
|
Chris@0
|
216 $locale = $catalogue->getLocale();
|
Chris@0
|
217 while (!$catalogue->defines($id, $domain)) {
|
Chris@0
|
218 if ($cat = $catalogue->getFallbackCatalogue()) {
|
Chris@0
|
219 $catalogue = $cat;
|
Chris@0
|
220 $locale = $catalogue->getLocale();
|
Chris@0
|
221 } else {
|
Chris@0
|
222 break;
|
Chris@0
|
223 }
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@14
|
226 return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 /**
|
Chris@0
|
230 * {@inheritdoc}
|
Chris@0
|
231 */
|
Chris@0
|
232 public function getCatalogue($locale = null)
|
Chris@0
|
233 {
|
Chris@0
|
234 if (null === $locale) {
|
Chris@0
|
235 $locale = $this->getLocale();
|
Chris@0
|
236 } else {
|
Chris@0
|
237 $this->assertValidLocale($locale);
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 if (!isset($this->catalogues[$locale])) {
|
Chris@0
|
241 $this->loadCatalogue($locale);
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 return $this->catalogues[$locale];
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * Gets the loaders.
|
Chris@0
|
249 *
|
Chris@0
|
250 * @return array LoaderInterface[]
|
Chris@0
|
251 */
|
Chris@0
|
252 protected function getLoaders()
|
Chris@0
|
253 {
|
Chris@0
|
254 return $this->loaders;
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * @param string $locale
|
Chris@0
|
259 */
|
Chris@0
|
260 protected function loadCatalogue($locale)
|
Chris@0
|
261 {
|
Chris@0
|
262 if (null === $this->cacheDir) {
|
Chris@0
|
263 $this->initializeCatalogue($locale);
|
Chris@0
|
264 } else {
|
Chris@0
|
265 $this->initializeCacheCatalogue($locale);
|
Chris@0
|
266 }
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 /**
|
Chris@0
|
270 * @param string $locale
|
Chris@0
|
271 */
|
Chris@0
|
272 protected function initializeCatalogue($locale)
|
Chris@0
|
273 {
|
Chris@0
|
274 $this->assertValidLocale($locale);
|
Chris@0
|
275
|
Chris@0
|
276 try {
|
Chris@0
|
277 $this->doLoadCatalogue($locale);
|
Chris@0
|
278 } catch (NotFoundResourceException $e) {
|
Chris@0
|
279 if (!$this->computeFallbackLocales($locale)) {
|
Chris@0
|
280 throw $e;
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|
Chris@0
|
283 $this->loadFallbackCatalogues($locale);
|
Chris@0
|
284 }
|
Chris@0
|
285
|
Chris@0
|
286 /**
|
Chris@0
|
287 * @param string $locale
|
Chris@0
|
288 */
|
Chris@0
|
289 private function initializeCacheCatalogue($locale)
|
Chris@0
|
290 {
|
Chris@0
|
291 if (isset($this->catalogues[$locale])) {
|
Chris@0
|
292 /* Catalogue already initialized. */
|
Chris@0
|
293 return;
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 $this->assertValidLocale($locale);
|
Chris@0
|
297 $cache = $this->getConfigCacheFactory()->cache($this->getCatalogueCachePath($locale),
|
Chris@0
|
298 function (ConfigCacheInterface $cache) use ($locale) {
|
Chris@0
|
299 $this->dumpCatalogue($locale, $cache);
|
Chris@0
|
300 }
|
Chris@0
|
301 );
|
Chris@0
|
302
|
Chris@0
|
303 if (isset($this->catalogues[$locale])) {
|
Chris@0
|
304 /* Catalogue has been initialized as it was written out to cache. */
|
Chris@0
|
305 return;
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 /* Read catalogue from cache. */
|
Chris@0
|
309 $this->catalogues[$locale] = include $cache->getPath();
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 private function dumpCatalogue($locale, ConfigCacheInterface $cache)
|
Chris@0
|
313 {
|
Chris@0
|
314 $this->initializeCatalogue($locale);
|
Chris@0
|
315 $fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);
|
Chris@0
|
316
|
Chris@0
|
317 $content = sprintf(<<<EOF
|
Chris@0
|
318 <?php
|
Chris@0
|
319
|
Chris@0
|
320 use Symfony\Component\Translation\MessageCatalogue;
|
Chris@0
|
321
|
Chris@0
|
322 \$catalogue = new MessageCatalogue('%s', %s);
|
Chris@0
|
323
|
Chris@0
|
324 %s
|
Chris@0
|
325 return \$catalogue;
|
Chris@0
|
326
|
Chris@0
|
327 EOF
|
Chris@0
|
328 ,
|
Chris@0
|
329 $locale,
|
Chris@0
|
330 var_export($this->catalogues[$locale]->all(), true),
|
Chris@0
|
331 $fallbackContent
|
Chris@0
|
332 );
|
Chris@0
|
333
|
Chris@0
|
334 $cache->write($content, $this->catalogues[$locale]->getResources());
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 private function getFallbackContent(MessageCatalogue $catalogue)
|
Chris@0
|
338 {
|
Chris@0
|
339 $fallbackContent = '';
|
Chris@0
|
340 $current = '';
|
Chris@0
|
341 $replacementPattern = '/[^a-z0-9_]/i';
|
Chris@0
|
342 $fallbackCatalogue = $catalogue->getFallbackCatalogue();
|
Chris@0
|
343 while ($fallbackCatalogue) {
|
Chris@0
|
344 $fallback = $fallbackCatalogue->getLocale();
|
Chris@0
|
345 $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
|
Chris@0
|
346 $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
|
Chris@0
|
347
|
Chris@0
|
348 $fallbackContent .= sprintf(<<<'EOF'
|
Chris@0
|
349 $catalogue%s = new MessageCatalogue('%s', %s);
|
Chris@0
|
350 $catalogue%s->addFallbackCatalogue($catalogue%s);
|
Chris@0
|
351
|
Chris@0
|
352 EOF
|
Chris@0
|
353 ,
|
Chris@0
|
354 $fallbackSuffix,
|
Chris@0
|
355 $fallback,
|
Chris@0
|
356 var_export($fallbackCatalogue->all(), true),
|
Chris@0
|
357 $currentSuffix,
|
Chris@0
|
358 $fallbackSuffix
|
Chris@0
|
359 );
|
Chris@0
|
360 $current = $fallbackCatalogue->getLocale();
|
Chris@0
|
361 $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
|
Chris@0
|
362 }
|
Chris@0
|
363
|
Chris@0
|
364 return $fallbackContent;
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 private function getCatalogueCachePath($locale)
|
Chris@0
|
368 {
|
Chris@14
|
369 return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php';
|
Chris@0
|
370 }
|
Chris@0
|
371
|
Chris@0
|
372 private function doLoadCatalogue($locale)
|
Chris@0
|
373 {
|
Chris@0
|
374 $this->catalogues[$locale] = new MessageCatalogue($locale);
|
Chris@0
|
375
|
Chris@0
|
376 if (isset($this->resources[$locale])) {
|
Chris@0
|
377 foreach ($this->resources[$locale] as $resource) {
|
Chris@0
|
378 if (!isset($this->loaders[$resource[0]])) {
|
Chris@0
|
379 throw new RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
|
Chris@0
|
380 }
|
Chris@0
|
381 $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
|
Chris@0
|
382 }
|
Chris@0
|
383 }
|
Chris@0
|
384 }
|
Chris@0
|
385
|
Chris@0
|
386 private function loadFallbackCatalogues($locale)
|
Chris@0
|
387 {
|
Chris@0
|
388 $current = $this->catalogues[$locale];
|
Chris@0
|
389
|
Chris@0
|
390 foreach ($this->computeFallbackLocales($locale) as $fallback) {
|
Chris@0
|
391 if (!isset($this->catalogues[$fallback])) {
|
Chris@0
|
392 $this->initializeCatalogue($fallback);
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 $fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
|
Chris@0
|
396 foreach ($this->catalogues[$fallback]->getResources() as $resource) {
|
Chris@0
|
397 $fallbackCatalogue->addResource($resource);
|
Chris@0
|
398 }
|
Chris@0
|
399 $current->addFallbackCatalogue($fallbackCatalogue);
|
Chris@0
|
400 $current = $fallbackCatalogue;
|
Chris@0
|
401 }
|
Chris@0
|
402 }
|
Chris@0
|
403
|
Chris@0
|
404 protected function computeFallbackLocales($locale)
|
Chris@0
|
405 {
|
Chris@17
|
406 $locales = [];
|
Chris@0
|
407 foreach ($this->fallbackLocales as $fallback) {
|
Chris@0
|
408 if ($fallback === $locale) {
|
Chris@0
|
409 continue;
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 $locales[] = $fallback;
|
Chris@0
|
413 }
|
Chris@0
|
414
|
Chris@14
|
415 if (false !== strrchr($locale, '_')) {
|
Chris@17
|
416 array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '_'))));
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 return array_unique($locales);
|
Chris@0
|
420 }
|
Chris@0
|
421
|
Chris@0
|
422 /**
|
Chris@0
|
423 * Asserts that the locale is valid, throws an Exception if not.
|
Chris@0
|
424 *
|
Chris@0
|
425 * @param string $locale Locale to tests
|
Chris@0
|
426 *
|
Chris@0
|
427 * @throws InvalidArgumentException If the locale contains invalid characters
|
Chris@0
|
428 */
|
Chris@0
|
429 protected function assertValidLocale($locale)
|
Chris@0
|
430 {
|
Chris@0
|
431 if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
Chris@0
|
432 throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
Chris@0
|
433 }
|
Chris@0
|
434 }
|
Chris@0
|
435
|
Chris@0
|
436 /**
|
Chris@0
|
437 * Provides the ConfigCache factory implementation, falling back to a
|
Chris@0
|
438 * default implementation if necessary.
|
Chris@0
|
439 *
|
Chris@0
|
440 * @return ConfigCacheFactoryInterface $configCacheFactory
|
Chris@0
|
441 */
|
Chris@0
|
442 private function getConfigCacheFactory()
|
Chris@0
|
443 {
|
Chris@0
|
444 if (!$this->configCacheFactory) {
|
Chris@0
|
445 $this->configCacheFactory = new ConfigCacheFactory($this->debug);
|
Chris@0
|
446 }
|
Chris@0
|
447
|
Chris@0
|
448 return $this->configCacheFactory;
|
Chris@0
|
449 }
|
Chris@0
|
450 }
|