| 148 |
lars |
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\Contracts\Translation\LocaleAwareInterface;
|
|
|
17 |
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
|
|
|
21 |
*/
|
|
|
22 |
class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface, WarmableInterface
|
|
|
23 |
{
|
|
|
24 |
public const MESSAGE_DEFINED = 0;
|
|
|
25 |
public const MESSAGE_MISSING = 1;
|
|
|
26 |
public const MESSAGE_EQUALS_FALLBACK = 2;
|
|
|
27 |
|
|
|
28 |
private TranslatorInterface $translator;
|
|
|
29 |
private array $messages = [];
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator
|
|
|
33 |
*/
|
|
|
34 |
public function __construct(TranslatorInterface $translator)
|
|
|
35 |
{
|
|
|
36 |
if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
|
|
|
37 |
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', get_debug_type($translator)));
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$this->translator = $translator;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string
|
|
|
44 |
{
|
|
|
45 |
$trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
|
|
|
46 |
$this->collectMessage($locale, $domain, $id, $trans, $parameters);
|
|
|
47 |
|
|
|
48 |
return $trans;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function setLocale(string $locale)
|
|
|
52 |
{
|
|
|
53 |
$this->translator->setLocale($locale);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function getLocale(): string
|
|
|
57 |
{
|
|
|
58 |
return $this->translator->getLocale();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function getCatalogue(string $locale = null): MessageCatalogueInterface
|
|
|
62 |
{
|
|
|
63 |
return $this->translator->getCatalogue($locale);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function getCatalogues(): array
|
|
|
67 |
{
|
|
|
68 |
return $this->translator->getCatalogues();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @return string[]
|
|
|
73 |
*/
|
|
|
74 |
public function warmUp(string $cacheDir): array
|
|
|
75 |
{
|
|
|
76 |
if ($this->translator instanceof WarmableInterface) {
|
|
|
77 |
return (array) $this->translator->warmUp($cacheDir);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return [];
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Gets the fallback locales.
|
|
|
85 |
*/
|
|
|
86 |
public function getFallbackLocales(): array
|
|
|
87 |
{
|
|
|
88 |
if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
|
|
|
89 |
return $this->translator->getFallbackLocales();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return [];
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Passes through all unknown calls onto the translator object.
|
|
|
97 |
*/
|
|
|
98 |
public function __call(string $method, array $args)
|
|
|
99 |
{
|
|
|
100 |
return $this->translator->{$method}(...$args);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public function getCollectedMessages(): array
|
|
|
104 |
{
|
|
|
105 |
return $this->messages;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = [])
|
|
|
109 |
{
|
|
|
110 |
$domain ??= 'messages';
|
|
|
111 |
|
|
|
112 |
$catalogue = $this->translator->getCatalogue($locale);
|
|
|
113 |
$locale = $catalogue->getLocale();
|
|
|
114 |
$fallbackLocale = null;
|
|
|
115 |
if ($catalogue->defines($id, $domain)) {
|
|
|
116 |
$state = self::MESSAGE_DEFINED;
|
|
|
117 |
} elseif ($catalogue->has($id, $domain)) {
|
|
|
118 |
$state = self::MESSAGE_EQUALS_FALLBACK;
|
|
|
119 |
|
|
|
120 |
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
|
|
|
121 |
while ($fallbackCatalogue) {
|
|
|
122 |
if ($fallbackCatalogue->defines($id, $domain)) {
|
|
|
123 |
$fallbackLocale = $fallbackCatalogue->getLocale();
|
|
|
124 |
break;
|
|
|
125 |
}
|
|
|
126 |
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
|
|
|
127 |
}
|
|
|
128 |
} else {
|
|
|
129 |
$state = self::MESSAGE_MISSING;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
$this->messages[] = [
|
|
|
133 |
'locale' => $locale,
|
|
|
134 |
'fallbackLocale' => $fallbackLocale,
|
|
|
135 |
'domain' => $domain,
|
|
|
136 |
'id' => $id,
|
|
|
137 |
'translation' => $translation,
|
|
|
138 |
'parameters' => $parameters,
|
|
|
139 |
'state' => $state,
|
|
|
140 |
'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
|
|
|
141 |
];
|
|
|
142 |
}
|
|
|
143 |
}
|