Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/translation/MessageCatalogue.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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\Config\Resource\ResourceInterface; | |
15 use Symfony\Component\Translation\Exception\LogicException; | |
16 | |
17 /** | |
18 * MessageCatalogue. | |
19 * | |
20 * @author Fabien Potencier <fabien@symfony.com> | |
21 */ | |
22 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface | |
23 { | |
24 private $messages = array(); | |
25 private $metadata = array(); | |
26 private $resources = array(); | |
27 private $locale; | |
28 private $fallbackCatalogue; | |
29 private $parent; | |
30 | |
31 /** | |
32 * Constructor. | |
33 * | |
34 * @param string $locale The locale | |
35 * @param array $messages An array of messages classified by domain | |
36 */ | |
37 public function __construct($locale, array $messages = array()) | |
38 { | |
39 $this->locale = $locale; | |
40 $this->messages = $messages; | |
41 } | |
42 | |
43 /** | |
44 * {@inheritdoc} | |
45 */ | |
46 public function getLocale() | |
47 { | |
48 return $this->locale; | |
49 } | |
50 | |
51 /** | |
52 * {@inheritdoc} | |
53 */ | |
54 public function getDomains() | |
55 { | |
56 return array_keys($this->messages); | |
57 } | |
58 | |
59 /** | |
60 * {@inheritdoc} | |
61 */ | |
62 public function all($domain = null) | |
63 { | |
64 if (null === $domain) { | |
65 return $this->messages; | |
66 } | |
67 | |
68 return isset($this->messages[$domain]) ? $this->messages[$domain] : array(); | |
69 } | |
70 | |
71 /** | |
72 * {@inheritdoc} | |
73 */ | |
74 public function set($id, $translation, $domain = 'messages') | |
75 { | |
76 $this->add(array($id => $translation), $domain); | |
77 } | |
78 | |
79 /** | |
80 * {@inheritdoc} | |
81 */ | |
82 public function has($id, $domain = 'messages') | |
83 { | |
84 if (isset($this->messages[$domain][$id])) { | |
85 return true; | |
86 } | |
87 | |
88 if (null !== $this->fallbackCatalogue) { | |
89 return $this->fallbackCatalogue->has($id, $domain); | |
90 } | |
91 | |
92 return false; | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
98 public function defines($id, $domain = 'messages') | |
99 { | |
100 return isset($this->messages[$domain][$id]); | |
101 } | |
102 | |
103 /** | |
104 * {@inheritdoc} | |
105 */ | |
106 public function get($id, $domain = 'messages') | |
107 { | |
108 if (isset($this->messages[$domain][$id])) { | |
109 return $this->messages[$domain][$id]; | |
110 } | |
111 | |
112 if (null !== $this->fallbackCatalogue) { | |
113 return $this->fallbackCatalogue->get($id, $domain); | |
114 } | |
115 | |
116 return $id; | |
117 } | |
118 | |
119 /** | |
120 * {@inheritdoc} | |
121 */ | |
122 public function replace($messages, $domain = 'messages') | |
123 { | |
124 $this->messages[$domain] = array(); | |
125 | |
126 $this->add($messages, $domain); | |
127 } | |
128 | |
129 /** | |
130 * {@inheritdoc} | |
131 */ | |
132 public function add($messages, $domain = 'messages') | |
133 { | |
134 if (!isset($this->messages[$domain])) { | |
135 $this->messages[$domain] = $messages; | |
136 } else { | |
137 $this->messages[$domain] = array_replace($this->messages[$domain], $messages); | |
138 } | |
139 } | |
140 | |
141 /** | |
142 * {@inheritdoc} | |
143 */ | |
144 public function addCatalogue(MessageCatalogueInterface $catalogue) | |
145 { | |
146 if ($catalogue->getLocale() !== $this->locale) { | |
147 throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale)); | |
148 } | |
149 | |
150 foreach ($catalogue->all() as $domain => $messages) { | |
151 $this->add($messages, $domain); | |
152 } | |
153 | |
154 foreach ($catalogue->getResources() as $resource) { | |
155 $this->addResource($resource); | |
156 } | |
157 | |
158 if ($catalogue instanceof MetadataAwareInterface) { | |
159 $metadata = $catalogue->getMetadata('', ''); | |
160 $this->addMetadata($metadata); | |
161 } | |
162 } | |
163 | |
164 /** | |
165 * {@inheritdoc} | |
166 */ | |
167 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue) | |
168 { | |
169 // detect circular references | |
170 $c = $catalogue; | |
171 while ($c = $c->getFallbackCatalogue()) { | |
172 if ($c->getLocale() === $this->getLocale()) { | |
173 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); | |
174 } | |
175 } | |
176 | |
177 $c = $this; | |
178 do { | |
179 if ($c->getLocale() === $catalogue->getLocale()) { | |
180 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); | |
181 } | |
182 | |
183 foreach ($catalogue->getResources() as $resource) { | |
184 $c->addResource($resource); | |
185 } | |
186 } while ($c = $c->parent); | |
187 | |
188 $catalogue->parent = $this; | |
189 $this->fallbackCatalogue = $catalogue; | |
190 | |
191 foreach ($catalogue->getResources() as $resource) { | |
192 $this->addResource($resource); | |
193 } | |
194 } | |
195 | |
196 /** | |
197 * {@inheritdoc} | |
198 */ | |
199 public function getFallbackCatalogue() | |
200 { | |
201 return $this->fallbackCatalogue; | |
202 } | |
203 | |
204 /** | |
205 * {@inheritdoc} | |
206 */ | |
207 public function getResources() | |
208 { | |
209 return array_values($this->resources); | |
210 } | |
211 | |
212 /** | |
213 * {@inheritdoc} | |
214 */ | |
215 public function addResource(ResourceInterface $resource) | |
216 { | |
217 $this->resources[$resource->__toString()] = $resource; | |
218 } | |
219 | |
220 /** | |
221 * {@inheritdoc} | |
222 */ | |
223 public function getMetadata($key = '', $domain = 'messages') | |
224 { | |
225 if ('' == $domain) { | |
226 return $this->metadata; | |
227 } | |
228 | |
229 if (isset($this->metadata[$domain])) { | |
230 if ('' == $key) { | |
231 return $this->metadata[$domain]; | |
232 } | |
233 | |
234 if (isset($this->metadata[$domain][$key])) { | |
235 return $this->metadata[$domain][$key]; | |
236 } | |
237 } | |
238 } | |
239 | |
240 /** | |
241 * {@inheritdoc} | |
242 */ | |
243 public function setMetadata($key, $value, $domain = 'messages') | |
244 { | |
245 $this->metadata[$domain][$key] = $value; | |
246 } | |
247 | |
248 /** | |
249 * {@inheritdoc} | |
250 */ | |
251 public function deleteMetadata($key = '', $domain = 'messages') | |
252 { | |
253 if ('' == $domain) { | |
254 $this->metadata = array(); | |
255 } elseif ('' == $key) { | |
256 unset($this->metadata[$domain]); | |
257 } else { | |
258 unset($this->metadata[$domain][$key]); | |
259 } | |
260 } | |
261 | |
262 /** | |
263 * Adds current values with the new values. | |
264 * | |
265 * @param array $values Values to add | |
266 */ | |
267 private function addMetadata(array $values) | |
268 { | |
269 foreach ($values as $domain => $keys) { | |
270 foreach ($keys as $key => $value) { | |
271 $this->setMetadata($key, $value, $domain); | |
272 } | |
273 } | |
274 } | |
275 } |