| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation; |
| 13 |
|
| 14 |
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
| 15 |
use Symfony\Contracts\Service\ResetInterface; |
| 16 |
use Symfony\Contracts\Translation\LocaleAwareInterface; |
| 17 |
use Symfony\Contracts\Translation\TranslatorInterface; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface, WarmableInterface, ResetInterface |
| 25 |
{ |
| 26 |
public const MESSAGE_DEFINED = 0; |
| 27 |
public const MESSAGE_MISSING = 1; |
| 28 |
public const MESSAGE_EQUALS_FALLBACK = 2; |
| 29 |
|
| 30 |
private array $messages = []; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
private TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator, |
| 34 |
) { |
| 35 |
} |
| 36 |
|
| 37 |
public function reset(): void |
| 38 |
{ |
| 39 |
$this->messages = []; |
| 40 |
} |
| 41 |
|
| 42 |
public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string |
| 43 |
{ |
| 44 |
$trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale); |
| 45 |
$this->collectMessage($locale, $domain, $id, $trans, $parameters); |
| 46 |
|
| 47 |
return $trans; |
| 48 |
} |
| 49 |
|
| 50 |
public function setLocale(string $locale): void |
| 51 |
{ |
| 52 |
$this->translator->setLocale($locale); |
| 53 |
} |
| 54 |
|
| 55 |
public function getLocale(): string |
| 56 |
{ |
| 57 |
return $this->translator->getLocale(); |
| 58 |
} |
| 59 |
|
| 60 |
public function getCatalogue(?string $locale = null): MessageCatalogueInterface |
| 61 |
{ |
| 62 |
return $this->translator->getCatalogue($locale); |
| 63 |
} |
| 64 |
|
| 65 |
public function getCatalogues(): array |
| 66 |
{ |
| 67 |
return $this->translator->getCatalogues(); |
| 68 |
} |
| 69 |
|
| 70 |
public function warmUp(string $cacheDir, ?string $buildDir = null): array |
| 71 |
{ |
| 72 |
if ($this->translator instanceof WarmableInterface) { |
| 73 |
return $this->translator->warmUp($cacheDir, $buildDir); |
| 74 |
} |
| 75 |
|
| 76 |
return []; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function getFallbackLocales(): array |
| 83 |
{ |
| 84 |
if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { |
| 85 |
return $this->translator->getFallbackLocales(); |
| 86 |
} |
| 87 |
|
| 88 |
return []; |
| 89 |
} |
| 90 |
|
| 91 |
public function getGlobalParameters(): array |
| 92 |
{ |
| 93 |
if ($this->translator instanceof Translator || method_exists($this->translator, 'getGlobalParameters')) { |
| 94 |
return $this->translator->getGlobalParameters(); |
| 95 |
} |
| 96 |
|
| 97 |
return []; |
| 98 |
} |
| 99 |
|
| 100 |
public function __call(string $method, array $args): mixed |
| 101 |
{ |
| 102 |
return $this->translator->{$method}(...$args); |
| 103 |
} |
| 104 |
|
| 105 |
public function getCollectedMessages(): array |
| 106 |
{ |
| 107 |
return $this->messages; |
| 108 |
} |
| 109 |
|
| 110 |
private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = []): void |
| 111 |
{ |
| 112 |
$domain ??= 'messages'; |
| 113 |
|
| 114 |
$catalogue = $this->translator->getCatalogue($locale); |
| 115 |
$locale = $catalogue->getLocale(); |
| 116 |
$fallbackLocale = null; |
| 117 |
if ($catalogue->defines($id, $domain)) { |
| 118 |
$state = self::MESSAGE_DEFINED; |
| 119 |
} elseif ($catalogue->has($id, $domain)) { |
| 120 |
$state = self::MESSAGE_EQUALS_FALLBACK; |
| 121 |
|
| 122 |
$fallbackCatalogue = $catalogue->getFallbackCatalogue(); |
| 123 |
while ($fallbackCatalogue) { |
| 124 |
if ($fallbackCatalogue->defines($id, $domain)) { |
| 125 |
$fallbackLocale = $fallbackCatalogue->getLocale(); |
| 126 |
break; |
| 127 |
} |
| 128 |
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); |
| 129 |
} |
| 130 |
} else { |
| 131 |
$state = self::MESSAGE_MISSING; |
| 132 |
} |
| 133 |
|
| 134 |
$this->messages[] = [ |
| 135 |
'locale' => $locale, |
| 136 |
'fallbackLocale' => $fallbackLocale, |
| 137 |
'domain' => $domain, |
| 138 |
'id' => $id, |
| 139 |
'translation' => $translation, |
| 140 |
'parameters' => $parameters, |
| 141 |
'state' => $state, |
| 142 |
'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null, |
| 143 |
]; |
| 144 |
} |
| 145 |
} |