Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 28 | private $translator; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 29 | private array $messages = []; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 30 | |
| 31 | /** |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 32 | * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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 | /** |
| 44 | * {@inheritdoc} |
| 45 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 46 | public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 47 | { |
| 48 | $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale); |
| 49 | $this->collectMessage($locale, $domain, $id, $trans, $parameters); |
| 50 | |
| 51 | return $trans; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritdoc} |
| 56 | */ |
| 57 | public function setLocale(string $locale) |
| 58 | { |
| 59 | $this->translator->setLocale($locale); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * {@inheritdoc} |
| 64 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 65 | public function getLocale(): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 66 | { |
| 67 | return $this->translator->getLocale(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * {@inheritdoc} |
| 72 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 73 | public function getCatalogue(string $locale = null): MessageCatalogueInterface |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 74 | { |
| 75 | return $this->translator->getCatalogue($locale); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function getCatalogues(): array |
| 82 | { |
| 83 | return $this->translator->getCatalogues(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * {@inheritdoc} |
| 88 | * |
| 89 | * @return string[] |
| 90 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 91 | public function warmUp(string $cacheDir): array |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 92 | { |
| 93 | if ($this->translator instanceof WarmableInterface) { |
| 94 | return (array) $this->translator->warmUp($cacheDir); |
| 95 | } |
| 96 | |
| 97 | return []; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Gets the fallback locales. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 102 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 103 | public function getFallbackLocales(): array |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 104 | { |
| 105 | if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) { |
| 106 | return $this->translator->getFallbackLocales(); |
| 107 | } |
| 108 | |
| 109 | return []; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Passes through all unknown calls onto the translator object. |
| 114 | */ |
| 115 | public function __call(string $method, array $args) |
| 116 | { |
| 117 | return $this->translator->{$method}(...$args); |
| 118 | } |
| 119 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 120 | public function getCollectedMessages(): array |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 121 | { |
| 122 | return $this->messages; |
| 123 | } |
| 124 | |
| 125 | private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = []) |
| 126 | { |
| 127 | if (null === $domain) { |
| 128 | $domain = 'messages'; |
| 129 | } |
| 130 | |
| 131 | $catalogue = $this->translator->getCatalogue($locale); |
| 132 | $locale = $catalogue->getLocale(); |
| 133 | $fallbackLocale = null; |
| 134 | if ($catalogue->defines($id, $domain)) { |
| 135 | $state = self::MESSAGE_DEFINED; |
| 136 | } elseif ($catalogue->has($id, $domain)) { |
| 137 | $state = self::MESSAGE_EQUALS_FALLBACK; |
| 138 | |
| 139 | $fallbackCatalogue = $catalogue->getFallbackCatalogue(); |
| 140 | while ($fallbackCatalogue) { |
| 141 | if ($fallbackCatalogue->defines($id, $domain)) { |
| 142 | $fallbackLocale = $fallbackCatalogue->getLocale(); |
| 143 | break; |
| 144 | } |
| 145 | $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); |
| 146 | } |
| 147 | } else { |
| 148 | $state = self::MESSAGE_MISSING; |
| 149 | } |
| 150 | |
| 151 | $this->messages[] = [ |
| 152 | 'locale' => $locale, |
| 153 | 'fallbackLocale' => $fallbackLocale, |
| 154 | 'domain' => $domain, |
| 155 | 'id' => $id, |
| 156 | 'translation' => $translation, |
| 157 | 'parameters' => $parameters, |
| 158 | 'state' => $state, |
| 159 | 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null, |
| 160 | ]; |
| 161 | } |
| 162 | } |