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
MessageCatalogue.php
195 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Translation; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Config\Resource\ResourceInterface; |
| 5 | use MailPoetVendor\Symfony\Component\Translation\Exception\LogicException; |
| 6 | class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface |
| 7 | { |
| 8 | private $messages = []; |
| 9 | private $metadata = []; |
| 10 | private $resources = []; |
| 11 | private $locale; |
| 12 | private $fallbackCatalogue; |
| 13 | private $parent; |
| 14 | public function __construct(string $locale, array $messages = []) |
| 15 | { |
| 16 | $this->locale = $locale; |
| 17 | $this->messages = $messages; |
| 18 | } |
| 19 | public function getLocale() |
| 20 | { |
| 21 | return $this->locale; |
| 22 | } |
| 23 | public function getDomains() |
| 24 | { |
| 25 | $domains = []; |
| 26 | foreach ($this->messages as $domain => $messages) { |
| 27 | if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) { |
| 28 | $domain = \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)); |
| 29 | } |
| 30 | $domains[$domain] = $domain; |
| 31 | } |
| 32 | return \array_values($domains); |
| 33 | } |
| 34 | public function all(?string $domain = null) |
| 35 | { |
| 36 | if (null !== $domain) { |
| 37 | // skip messages merge if intl-icu requested explicitly |
| 38 | if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) { |
| 39 | return $this->messages[$domain] ?? []; |
| 40 | } |
| 41 | return ($this->messages[$domain . self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []); |
| 42 | } |
| 43 | $allMessages = []; |
| 44 | foreach ($this->messages as $domain => $messages) { |
| 45 | if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) { |
| 46 | $domain = \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)); |
| 47 | $allMessages[$domain] = $messages + ($allMessages[$domain] ?? []); |
| 48 | } else { |
| 49 | $allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages; |
| 50 | } |
| 51 | } |
| 52 | return $allMessages; |
| 53 | } |
| 54 | public function set(string $id, string $translation, string $domain = 'messages') |
| 55 | { |
| 56 | $this->add([$id => $translation], $domain); |
| 57 | } |
| 58 | public function has(string $id, string $domain = 'messages') |
| 59 | { |
| 60 | if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id])) { |
| 61 | return \true; |
| 62 | } |
| 63 | if (null !== $this->fallbackCatalogue) { |
| 64 | return $this->fallbackCatalogue->has($id, $domain); |
| 65 | } |
| 66 | return \false; |
| 67 | } |
| 68 | public function defines(string $id, string $domain = 'messages') |
| 69 | { |
| 70 | return isset($this->messages[$domain][$id]) || isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id]); |
| 71 | } |
| 72 | public function get(string $id, string $domain = 'messages') |
| 73 | { |
| 74 | if (isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id])) { |
| 75 | return $this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id]; |
| 76 | } |
| 77 | if (isset($this->messages[$domain][$id])) { |
| 78 | return $this->messages[$domain][$id]; |
| 79 | } |
| 80 | if (null !== $this->fallbackCatalogue) { |
| 81 | return $this->fallbackCatalogue->get($id, $domain); |
| 82 | } |
| 83 | return $id; |
| 84 | } |
| 85 | public function replace(array $messages, string $domain = 'messages') |
| 86 | { |
| 87 | unset($this->messages[$domain], $this->messages[$domain . self::INTL_DOMAIN_SUFFIX]); |
| 88 | $this->add($messages, $domain); |
| 89 | } |
| 90 | public function add(array $messages, string $domain = 'messages') |
| 91 | { |
| 92 | $altDomain = \str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain . self::INTL_DOMAIN_SUFFIX; |
| 93 | foreach ($messages as $id => $message) { |
| 94 | unset($this->messages[$altDomain][$id]); |
| 95 | $this->messages[$domain][$id] = $message; |
| 96 | } |
| 97 | if ([] === ($this->messages[$altDomain] ?? null)) { |
| 98 | unset($this->messages[$altDomain]); |
| 99 | } |
| 100 | } |
| 101 | public function addCatalogue(MessageCatalogueInterface $catalogue) |
| 102 | { |
| 103 | if ($catalogue->getLocale() !== $this->locale) { |
| 104 | throw new LogicException(\sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale)); |
| 105 | } |
| 106 | foreach ($catalogue->all() as $domain => $messages) { |
| 107 | if ($intlMessages = $catalogue->all($domain . self::INTL_DOMAIN_SUFFIX)) { |
| 108 | $this->add($intlMessages, $domain . self::INTL_DOMAIN_SUFFIX); |
| 109 | $messages = \array_diff_key($messages, $intlMessages); |
| 110 | } |
| 111 | $this->add($messages, $domain); |
| 112 | } |
| 113 | foreach ($catalogue->getResources() as $resource) { |
| 114 | $this->addResource($resource); |
| 115 | } |
| 116 | if ($catalogue instanceof MetadataAwareInterface) { |
| 117 | $metadata = $catalogue->getMetadata('', ''); |
| 118 | $this->addMetadata($metadata); |
| 119 | } |
| 120 | } |
| 121 | public function addFallbackCatalogue(MessageCatalogueInterface $catalogue) |
| 122 | { |
| 123 | // detect circular references |
| 124 | $c = $catalogue; |
| 125 | while ($c = $c->getFallbackCatalogue()) { |
| 126 | if ($c->getLocale() === $this->getLocale()) { |
| 127 | throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); |
| 128 | } |
| 129 | } |
| 130 | $c = $this; |
| 131 | do { |
| 132 | if ($c->getLocale() === $catalogue->getLocale()) { |
| 133 | throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); |
| 134 | } |
| 135 | foreach ($catalogue->getResources() as $resource) { |
| 136 | $c->addResource($resource); |
| 137 | } |
| 138 | } while ($c = $c->parent); |
| 139 | $catalogue->parent = $this; |
| 140 | $this->fallbackCatalogue = $catalogue; |
| 141 | foreach ($catalogue->getResources() as $resource) { |
| 142 | $this->addResource($resource); |
| 143 | } |
| 144 | } |
| 145 | public function getFallbackCatalogue() |
| 146 | { |
| 147 | return $this->fallbackCatalogue; |
| 148 | } |
| 149 | public function getResources() |
| 150 | { |
| 151 | return \array_values($this->resources); |
| 152 | } |
| 153 | public function addResource(ResourceInterface $resource) |
| 154 | { |
| 155 | $this->resources[$resource->__toString()] = $resource; |
| 156 | } |
| 157 | public function getMetadata(string $key = '', string $domain = 'messages') |
| 158 | { |
| 159 | if ('' == $domain) { |
| 160 | return $this->metadata; |
| 161 | } |
| 162 | if (isset($this->metadata[$domain])) { |
| 163 | if ('' == $key) { |
| 164 | return $this->metadata[$domain]; |
| 165 | } |
| 166 | if (isset($this->metadata[$domain][$key])) { |
| 167 | return $this->metadata[$domain][$key]; |
| 168 | } |
| 169 | } |
| 170 | return null; |
| 171 | } |
| 172 | public function setMetadata(string $key, $value, string $domain = 'messages') |
| 173 | { |
| 174 | $this->metadata[$domain][$key] = $value; |
| 175 | } |
| 176 | public function deleteMetadata(string $key = '', string $domain = 'messages') |
| 177 | { |
| 178 | if ('' == $domain) { |
| 179 | $this->metadata = []; |
| 180 | } elseif ('' == $key) { |
| 181 | unset($this->metadata[$domain]); |
| 182 | } else { |
| 183 | unset($this->metadata[$domain][$key]); |
| 184 | } |
| 185 | } |
| 186 | private function addMetadata(array $values) |
| 187 | { |
| 188 | foreach ($values as $domain => $keys) { |
| 189 | foreach ($keys as $key => $value) { |
| 190 | $this->setMetadata($key, $value, $domain); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 |