aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2013-08-03 19:26:54 +0200
committerNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2013-08-03 19:26:54 +0200
commit4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch)
tree6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php
parent2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff)
downloadwallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz
wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst
wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip
twig implementation
Diffstat (limited to 'vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php')
-rw-r--r--vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php295
1 files changed, 295 insertions, 0 deletions
diff --git a/vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php b/vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php
new file mode 100644
index 00000000..1d8a08d8
--- /dev/null
+++ b/vendor/symfony/translation/Symfony/Component/Translation/MessageCatalogue.php
@@ -0,0 +1,295 @@
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
12namespace Symfony\Component\Translation;
13
14use Symfony\Component\Config\Resource\ResourceInterface;
15
16/**
17 * MessageCatalogue.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 *
21 * @api
22 */
23class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
24{
25 private $messages = array();
26 private $metadata = array();
27 private $resources = array();
28 private $locale;
29 private $fallbackCatalogue;
30 private $parent;
31
32 /**
33 * Constructor.
34 *
35 * @param string $locale The locale
36 * @param array $messages An array of messages classified by domain
37 *
38 * @api
39 */
40 public function __construct($locale, array $messages = array())
41 {
42 $this->locale = $locale;
43 $this->messages = $messages;
44 }
45
46 /**
47 * {@inheritdoc}
48 *
49 * @api
50 */
51 public function getLocale()
52 {
53 return $this->locale;
54 }
55
56 /**
57 * {@inheritdoc}
58 *
59 * @api
60 */
61 public function getDomains()
62 {
63 return array_keys($this->messages);
64 }
65
66 /**
67 * {@inheritdoc}
68 *
69 * @api
70 */
71 public function all($domain = null)
72 {
73 if (null === $domain) {
74 return $this->messages;
75 }
76
77 return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
78 }
79
80 /**
81 * {@inheritdoc}
82 *
83 * @api
84 */
85 public function set($id, $translation, $domain = 'messages')
86 {
87 $this->add(array($id => $translation), $domain);
88 }
89
90 /**
91 * {@inheritdoc}
92 *
93 * @api
94 */
95 public function has($id, $domain = 'messages')
96 {
97 if (isset($this->messages[$domain][$id])) {
98 return true;
99 }
100
101 if (null !== $this->fallbackCatalogue) {
102 return $this->fallbackCatalogue->has($id, $domain);
103 }
104
105 return false;
106 }
107
108 /**
109 * {@inheritdoc}
110 */
111 public function defines($id, $domain = 'messages')
112 {
113 return isset($this->messages[$domain][$id]);
114 }
115
116 /**
117 * {@inheritdoc}
118 *
119 * @api
120 */
121 public function get($id, $domain = 'messages')
122 {
123 if (isset($this->messages[$domain][$id])) {
124 return $this->messages[$domain][$id];
125 }
126
127 if (null !== $this->fallbackCatalogue) {
128 return $this->fallbackCatalogue->get($id, $domain);
129 }
130
131 return $id;
132 }
133
134 /**
135 * {@inheritdoc}
136 *
137 * @api
138 */
139 public function replace($messages, $domain = 'messages')
140 {
141 $this->messages[$domain] = array();
142
143 $this->add($messages, $domain);
144 }
145
146 /**
147 * {@inheritdoc}
148 *
149 * @api
150 */
151 public function add($messages, $domain = 'messages')
152 {
153 if (!isset($this->messages[$domain])) {
154 $this->messages[$domain] = $messages;
155 } else {
156 $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
157 }
158 }
159
160 /**
161 * {@inheritdoc}
162 *
163 * @api
164 */
165 public function addCatalogue(MessageCatalogueInterface $catalogue)
166 {
167 if ($catalogue->getLocale() !== $this->locale) {
168 throw new \LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
169 }
170
171 foreach ($catalogue->all() as $domain => $messages) {
172 $this->add($messages, $domain);
173 }
174
175 foreach ($catalogue->getResources() as $resource) {
176 $this->addResource($resource);
177 }
178
179 if ($catalogue instanceof MetadataAwareInterface) {
180 $metadata = $catalogue->getMetadata('', '');
181 $this->addMetadata($metadata);
182 }
183 }
184
185 /**
186 * {@inheritdoc}
187 *
188 * @api
189 */
190 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
191 {
192 // detect circular references
193 $c = $this;
194 do {
195 if ($c->getLocale() === $catalogue->getLocale()) {
196 throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
197 }
198 } while ($c = $c->parent);
199
200 $catalogue->parent = $this;
201 $this->fallbackCatalogue = $catalogue;
202
203 foreach ($catalogue->getResources() as $resource) {
204 $this->addResource($resource);
205 }
206 }
207
208 /**
209 * {@inheritdoc}
210 *
211 * @api
212 */
213 public function getFallbackCatalogue()
214 {
215 return $this->fallbackCatalogue;
216 }
217
218 /**
219 * {@inheritdoc}
220 *
221 * @api
222 */
223 public function getResources()
224 {
225 return array_values($this->resources);
226 }
227
228 /**
229 * {@inheritdoc}
230 *
231 * @api
232 */
233 public function addResource(ResourceInterface $resource)
234 {
235 $this->resources[$resource->__toString()] = $resource;
236 }
237
238 /**
239 * {@inheritdoc}
240 */
241 public function getMetadata($key = '', $domain = 'messages')
242 {
243 if ('' == $domain) {
244 return $this->metadata;
245 }
246
247 if (isset($this->metadata[$domain])) {
248 if ('' == $key) {
249 return $this->metadata[$domain];
250 }
251
252 if (isset($this->metadata[$domain][$key])) {
253 return $this->metadata[$domain][$key];
254 }
255 }
256
257 return null;
258 }
259
260 /**
261 * {@inheritdoc}
262 */
263 public function setMetadata($key, $value, $domain = 'messages')
264 {
265 $this->metadata[$domain][$key] = $value;
266 }
267
268 /**
269 * {@inheritdoc}
270 */
271 public function deleteMetadata($key = '', $domain = 'messages')
272 {
273 if ('' == $domain) {
274 $this->metadata = array();
275 } elseif ('' == $key) {
276 unset($this->metadata[$domain]);
277 } else {
278 unset($this->metadata[$domain][$key]);
279 }
280 }
281
282 /**
283 * Adds current values with the new values.
284 *
285 * @param array $values Values to add
286 */
287 private function addMetadata(array $values)
288 {
289 foreach ($values as $domain => $keys) {
290 foreach ($keys as $key => $value) {
291 $this->setMetadata($key, $value, $domain);
292 }
293 }
294 }
295}