PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.5
Independent Analytics – WordPress Analytics Plugin v2.15.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / translation / MessageCatalogue.php
independent-analytics / vendor / symfony / translation Last commit date
Catalogue 1 month ago Command 1 month ago DataCollector 1 month ago DependencyInjection 1 month ago Dumper 1 month ago Exception 1 month ago Extractor 1 month ago Formatter 1 month ago Loader 1 month ago Provider 1 month ago Reader 1 month ago Resources 1 month ago Test 1 month ago Util 1 month ago Writer 1 month ago DataCollectorTranslator.php 1 month ago IdentityTranslator.php 2 years ago LoggingTranslator.php 1 month ago MessageCatalogue.php 1 month ago MessageCatalogueInterface.php 1 month ago MetadataAwareInterface.php 1 month ago PseudoLocalizationTranslator.php 1 month ago TranslatableMessage.php 1 month ago Translator.php 1 month ago TranslatorBag.php 1 month ago TranslatorBagInterface.php 1 month ago
MessageCatalogue.php
267 lines
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 namespace IAWPSCOPED\Symfony\Component\Translation;
12
13 use IAWPSCOPED\Symfony\Component\Config\Resource\ResourceInterface;
14 use IAWPSCOPED\Symfony\Component\Translation\Exception\LogicException;
15 /**
16 * @author Fabien Potencier <fabien@symfony.com>
17 * @internal
18 */
19 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
20 {
21 private array $messages = [];
22 private array $metadata = [];
23 private array $resources = [];
24 private string $locale;
25 private $fallbackCatalogue = null;
26 private ?self $parent = null;
27 /**
28 * @param array $messages An array of messages classified by domain
29 */
30 public function __construct(string $locale, array $messages = [])
31 {
32 $this->locale = $locale;
33 $this->messages = $messages;
34 }
35 /**
36 * {@inheritdoc}
37 */
38 public function getLocale() : string
39 {
40 return $this->locale;
41 }
42 /**
43 * {@inheritdoc}
44 */
45 public function getDomains() : array
46 {
47 $domains = [];
48 foreach ($this->messages as $domain => $messages) {
49 if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
50 $domain = \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
51 }
52 $domains[$domain] = $domain;
53 }
54 return \array_values($domains);
55 }
56 /**
57 * {@inheritdoc}
58 */
59 public function all(string $domain = null) : array
60 {
61 if (null !== $domain) {
62 // skip messages merge if intl-icu requested explicitly
63 if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
64 return $this->messages[$domain] ?? [];
65 }
66 return ($this->messages[$domain . self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
67 }
68 $allMessages = [];
69 foreach ($this->messages as $domain => $messages) {
70 if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
71 $domain = \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
72 $allMessages[$domain] = $messages + ($allMessages[$domain] ?? []);
73 } else {
74 $allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages;
75 }
76 }
77 return $allMessages;
78 }
79 /**
80 * {@inheritdoc}
81 */
82 public function set(string $id, string $translation, string $domain = 'messages')
83 {
84 $this->add([$id => $translation], $domain);
85 }
86 /**
87 * {@inheritdoc}
88 */
89 public function has(string $id, string $domain = 'messages') : bool
90 {
91 if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id])) {
92 return \true;
93 }
94 if (null !== $this->fallbackCatalogue) {
95 return $this->fallbackCatalogue->has($id, $domain);
96 }
97 return \false;
98 }
99 /**
100 * {@inheritdoc}
101 */
102 public function defines(string $id, string $domain = 'messages') : bool
103 {
104 return isset($this->messages[$domain][$id]) || isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id]);
105 }
106 /**
107 * {@inheritdoc}
108 */
109 public function get(string $id, string $domain = 'messages') : string
110 {
111 if (isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id])) {
112 return $this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id];
113 }
114 if (isset($this->messages[$domain][$id])) {
115 return $this->messages[$domain][$id];
116 }
117 if (null !== $this->fallbackCatalogue) {
118 return $this->fallbackCatalogue->get($id, $domain);
119 }
120 return $id;
121 }
122 /**
123 * {@inheritdoc}
124 */
125 public function replace(array $messages, string $domain = 'messages')
126 {
127 unset($this->messages[$domain], $this->messages[$domain . self::INTL_DOMAIN_SUFFIX]);
128 $this->add($messages, $domain);
129 }
130 /**
131 * {@inheritdoc}
132 */
133 public function add(array $messages, string $domain = 'messages')
134 {
135 $altDomain = \str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain . self::INTL_DOMAIN_SUFFIX;
136 foreach ($messages as $id => $message) {
137 unset($this->messages[$altDomain][$id]);
138 $this->messages[$domain][$id] = $message;
139 }
140 if ([] === ($this->messages[$altDomain] ?? null)) {
141 unset($this->messages[$altDomain]);
142 }
143 }
144 /**
145 * {@inheritdoc}
146 */
147 public function addCatalogue(MessageCatalogueInterface $catalogue)
148 {
149 if ($catalogue->getLocale() !== $this->locale) {
150 throw new LogicException(\sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
151 }
152 foreach ($catalogue->all() as $domain => $messages) {
153 if ($intlMessages = $catalogue->all($domain . self::INTL_DOMAIN_SUFFIX)) {
154 $this->add($intlMessages, $domain . self::INTL_DOMAIN_SUFFIX);
155 $messages = \array_diff_key($messages, $intlMessages);
156 }
157 $this->add($messages, $domain);
158 }
159 foreach ($catalogue->getResources() as $resource) {
160 $this->addResource($resource);
161 }
162 if ($catalogue instanceof MetadataAwareInterface) {
163 $metadata = $catalogue->getMetadata('', '');
164 $this->addMetadata($metadata);
165 }
166 }
167 /**
168 * {@inheritdoc}
169 */
170 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
171 {
172 // detect circular references
173 $c = $catalogue;
174 while ($c = $c->getFallbackCatalogue()) {
175 if ($c->getLocale() === $this->getLocale()) {
176 throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
177 }
178 }
179 $c = $this;
180 do {
181 if ($c->getLocale() === $catalogue->getLocale()) {
182 throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
183 }
184 foreach ($catalogue->getResources() as $resource) {
185 $c->addResource($resource);
186 }
187 } while ($c = $c->parent);
188 $catalogue->parent = $this;
189 $this->fallbackCatalogue = $catalogue;
190 foreach ($catalogue->getResources() as $resource) {
191 $this->addResource($resource);
192 }
193 }
194 /**
195 * {@inheritdoc}
196 */
197 public function getFallbackCatalogue() : ?MessageCatalogueInterface
198 {
199 return $this->fallbackCatalogue;
200 }
201 /**
202 * {@inheritdoc}
203 */
204 public function getResources() : array
205 {
206 return \array_values($this->resources);
207 }
208 /**
209 * {@inheritdoc}
210 */
211 public function addResource(ResourceInterface $resource)
212 {
213 $this->resources[$resource->__toString()] = $resource;
214 }
215 /**
216 * {@inheritdoc}
217 */
218 public function getMetadata(string $key = '', string $domain = 'messages') : mixed
219 {
220 if ('' == $domain) {
221 return $this->metadata;
222 }
223 if (isset($this->metadata[$domain])) {
224 if ('' == $key) {
225 return $this->metadata[$domain];
226 }
227 if (isset($this->metadata[$domain][$key])) {
228 return $this->metadata[$domain][$key];
229 }
230 }
231 return null;
232 }
233 /**
234 * {@inheritdoc}
235 */
236 public function setMetadata(string $key, mixed $value, string $domain = 'messages')
237 {
238 $this->metadata[$domain][$key] = $value;
239 }
240 /**
241 * {@inheritdoc}
242 */
243 public function deleteMetadata(string $key = '', string $domain = 'messages')
244 {
245 if ('' == $domain) {
246 $this->metadata = [];
247 } elseif ('' == $key) {
248 unset($this->metadata[$domain]);
249 } else {
250 unset($this->metadata[$domain][$key]);
251 }
252 }
253 /**
254 * Adds current values with the new values.
255 *
256 * @param array $values Values to add
257 */
258 private function addMetadata(array $values)
259 {
260 foreach ($values as $domain => $keys) {
261 foreach ($keys as $key => $value) {
262 $this->setMetadata($key, $value, $domain);
263 }
264 }
265 }
266 }
267