4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation;
14 use Symfony\Component\Config\Resource\ResourceInterface;
19 * @author Fabien Potencier <fabien@symfony.com>
21 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
23 private $messages = array();
24 private $metadata = array();
25 private $resources = array();
27 private $fallbackCatalogue;
33 * @param string $locale The locale
34 * @param array $messages An array of messages classified by domain
36 public function __construct($locale, array $messages = array())
38 $this->locale = $locale;
39 $this->messages = $messages;
45 public function getLocale()
53 public function getDomains()
55 return array_keys($this->messages);
61 public function all($domain = null)
63 if (null === $domain) {
64 return $this->messages;
67 return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
73 public function set($id, $translation, $domain = 'messages')
75 $this->add(array($id => $translation), $domain);
81 public function has($id, $domain = 'messages')
83 if (isset($this->messages[$domain][$id])) {
87 if (null !== $this->fallbackCatalogue) {
88 return $this->fallbackCatalogue->has($id, $domain);
97 public function defines($id, $domain = 'messages')
99 return isset($this->messages[$domain][$id]);
105 public function get($id, $domain = 'messages')
107 if (isset($this->messages[$domain][$id])) {
108 return $this->messages[$domain][$id];
111 if (null !== $this->fallbackCatalogue) {
112 return $this->fallbackCatalogue->get($id, $domain);
121 public function replace($messages, $domain = 'messages')
123 $this->messages[$domain] = array();
125 $this->add($messages, $domain);
131 public function add($messages, $domain = 'messages')
133 if (!isset($this->messages[$domain])) {
134 $this->messages[$domain] = $messages;
136 $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
143 public function addCatalogue(MessageCatalogueInterface $catalogue)
145 if ($catalogue->getLocale() !== $this->locale) {
146 throw new \LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
149 foreach ($catalogue->all() as $domain => $messages) {
150 $this->add($messages, $domain);
153 foreach ($catalogue->getResources() as $resource) {
154 $this->addResource($resource);
157 if ($catalogue instanceof MetadataAwareInterface) {
158 $metadata = $catalogue->getMetadata('', '');
159 $this->addMetadata($metadata);
166 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
168 // detect circular references
170 while ($c = $c->getFallbackCatalogue()) {
171 if ($c->getLocale() === $this->getLocale()) {
172 throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
178 if ($c->getLocale() === $catalogue->getLocale()) {
179 throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
182 foreach ($catalogue->getResources() as $resource) {
183 $c->addResource($resource);
185 } while ($c = $c->parent);
187 $catalogue->parent = $this;
188 $this->fallbackCatalogue = $catalogue;
190 foreach ($catalogue->getResources() as $resource) {
191 $this->addResource($resource);
198 public function getFallbackCatalogue()
200 return $this->fallbackCatalogue;
206 public function getResources()
208 return array_values($this->resources);
214 public function addResource(ResourceInterface $resource)
216 $this->resources[$resource->__toString()] = $resource;
222 public function getMetadata($key = '', $domain = 'messages')
225 return $this->metadata;
228 if (isset($this->metadata[$domain])) {
230 return $this->metadata[$domain];
233 if (isset($this->metadata[$domain][$key])) {
234 return $this->metadata[$domain][$key];
242 public function setMetadata($key, $value, $domain = 'messages')
244 $this->metadata[$domain][$key] = $value;
250 public function deleteMetadata($key = '', $domain = 'messages')
253 $this->metadata = array();
254 } elseif ('' == $key) {
255 unset($this->metadata[$domain]);
257 unset($this->metadata[$domain][$key]);
262 * Adds current values with the new values.
264 * @param array $values Values to add
266 private function addMetadata(array $values)
268 foreach ($values as $domain => $keys) {
269 foreach ($keys as $key => $value) {
270 $this->setMetadata($key, $value, $domain);