Exception
1 year ago
Provider
1 year ago
Test
1 year ago
Util
1 year ago
DataCollectorTranslator.php
1 year ago
IdentityTranslator.php
1 year ago
LoggingTranslator.php
1 year ago
MessageCatalogue.php
1 year ago
MessageCatalogueInterface.php
1 year ago
MetadataAwareInterface.php
1 year ago
PseudoLocalizationTranslator.php
1 year ago
TranslatableMessage.php
1 year ago
Translator.php
1 year ago
TranslatorBag.php
1 year ago
TranslatorBagInterface.php
1 year ago
index.php
3 years ago
DataCollectorTranslator.php
92 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Translation; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
| 5 | use MailPoetVendor\Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 6 | use MailPoetVendor\Symfony\Contracts\Translation\LocaleAwareInterface; |
| 7 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 8 | class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface, WarmableInterface |
| 9 | { |
| 10 | public const MESSAGE_DEFINED = 0; |
| 11 | public const MESSAGE_MISSING = 1; |
| 12 | public const MESSAGE_EQUALS_FALLBACK = 2; |
| 13 | private $translator; |
| 14 | private $messages = []; |
| 15 | public function __construct(TranslatorInterface $translator) |
| 16 | { |
| 17 | if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) { |
| 18 | throw new InvalidArgumentException(\sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_debug_type($translator))); |
| 19 | } |
| 20 | $this->translator = $translator; |
| 21 | } |
| 22 | public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null) |
| 23 | { |
| 24 | $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale); |
| 25 | $this->collectMessage($locale, $domain, $id, $trans, $parameters); |
| 26 | return $trans; |
| 27 | } |
| 28 | public function setLocale(string $locale) |
| 29 | { |
| 30 | $this->translator->setLocale($locale); |
| 31 | } |
| 32 | public function getLocale() |
| 33 | { |
| 34 | return $this->translator->getLocale(); |
| 35 | } |
| 36 | public function getCatalogue(?string $locale = null) |
| 37 | { |
| 38 | return $this->translator->getCatalogue($locale); |
| 39 | } |
| 40 | public function getCatalogues() : array |
| 41 | { |
| 42 | return $this->translator->getCatalogues(); |
| 43 | } |
| 44 | public function warmUp(string $cacheDir) |
| 45 | { |
| 46 | if ($this->translator instanceof WarmableInterface) { |
| 47 | return (array) $this->translator->warmUp($cacheDir); |
| 48 | } |
| 49 | return []; |
| 50 | } |
| 51 | public function getFallbackLocales() |
| 52 | { |
| 53 | if ($this->translator instanceof Translator || \method_exists($this->translator, 'getFallbackLocales')) { |
| 54 | return $this->translator->getFallbackLocales(); |
| 55 | } |
| 56 | return []; |
| 57 | } |
| 58 | public function __call(string $method, array $args) |
| 59 | { |
| 60 | return $this->translator->{$method}(...$args); |
| 61 | } |
| 62 | public function getCollectedMessages() |
| 63 | { |
| 64 | return $this->messages; |
| 65 | } |
| 66 | private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = []) |
| 67 | { |
| 68 | if (null === $domain) { |
| 69 | $domain = 'messages'; |
| 70 | } |
| 71 | $catalogue = $this->translator->getCatalogue($locale); |
| 72 | $locale = $catalogue->getLocale(); |
| 73 | $fallbackLocale = null; |
| 74 | if ($catalogue->defines($id, $domain)) { |
| 75 | $state = self::MESSAGE_DEFINED; |
| 76 | } elseif ($catalogue->has($id, $domain)) { |
| 77 | $state = self::MESSAGE_EQUALS_FALLBACK; |
| 78 | $fallbackCatalogue = $catalogue->getFallbackCatalogue(); |
| 79 | while ($fallbackCatalogue) { |
| 80 | if ($fallbackCatalogue->defines($id, $domain)) { |
| 81 | $fallbackLocale = $fallbackCatalogue->getLocale(); |
| 82 | break; |
| 83 | } |
| 84 | $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); |
| 85 | } |
| 86 | } else { |
| 87 | $state = self::MESSAGE_MISSING; |
| 88 | } |
| 89 | $this->messages[] = ['locale' => $locale, 'fallbackLocale' => $fallbackLocale, 'domain' => $domain, 'id' => $id, 'translation' => $translation, 'parameters' => $parameters, 'state' => $state, 'transChoiceNumber' => isset($parameters['%count%']) && \is_numeric($parameters['%count%']) ? $parameters['%count%'] : null]; |
| 90 | } |
| 91 | } |
| 92 |