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;
15 use Symfony\Component\Translation\Exception\LogicException;
18 * @author Fabien Potencier <fabien@symfony.com>
20 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
22 private $messages = array();
23 private $metadata = array();
24 private $resources = array();
26 private $fallbackCatalogue;
30 * @param string $locale The locale
31 * @param array $messages An array of messages classified by domain
33 public function __construct($locale, array $messages = array())
35 $this->locale = $locale;
36 $this->messages = $messages;
42 public function getLocale()
50 public function getDomains()
52 return array_keys($this->messages);
58 public function all($domain = null)
60 if (null === $domain) {
61 return $this->messages;
64 return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
70 public function set($id, $translation, $domain = 'messages')
72 $this->add(array($id => $translation), $domain);
78 public function has($id, $domain = 'messages')
80 if (isset($this->messages[$domain][$id])) {
84 if (null !== $this->fallbackCatalogue) {
85 return $this->fallbackCatalogue->has($id, $domain);
94 public function defines($id, $domain = 'messages')
96 return isset($this->messages[$domain][$id]);
102 public function get($id, $domain = 'messages')
104 if (isset($this->messages[$domain][$id])) {
105 return $this->messages[$domain][$id];
108 if (null !== $this->fallbackCatalogue) {
109 return $this->fallbackCatalogue->get($id, $domain);
118 public function replace($messages, $domain = 'messages')
120 $this->messages[$domain] = array();
122 $this->add($messages, $domain);
128 public function add($messages, $domain = 'messages')
130 if (!isset($this->messages[$domain])) {
131 $this->messages[$domain] = $messages;
133 $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
140 public function addCatalogue(MessageCatalogueInterface $catalogue)
142 if ($catalogue->getLocale() !== $this->locale) {
143 throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
146 foreach ($catalogue->all() as $domain => $messages) {
147 $this->add($messages, $domain);
150 foreach ($catalogue->getResources() as $resource) {
151 $this->addResource($resource);
154 if ($catalogue instanceof MetadataAwareInterface) {
155 $metadata = $catalogue->getMetadata('', '');
156 $this->addMetadata($metadata);
163 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
165 // detect circular references
167 while ($c = $c->getFallbackCatalogue()) {
168 if ($c->getLocale() === $this->getLocale()) {
169 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
175 if ($c->getLocale() === $catalogue->getLocale()) {
176 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
179 foreach ($catalogue->getResources() as $resource) {
180 $c->addResource($resource);
182 } while ($c = $c->parent);
184 $catalogue->parent = $this;
185 $this->fallbackCatalogue = $catalogue;
187 foreach ($catalogue->getResources() as $resource) {
188 $this->addResource($resource);
195 public function getFallbackCatalogue()
197 return $this->fallbackCatalogue;
203 public function getResources()
205 return array_values($this->resources);
211 public function addResource(ResourceInterface $resource)
213 $this->resources[$resource->__toString()] = $resource;
219 public function getMetadata($key = '', $domain = 'messages')
222 return $this->metadata;
225 if (isset($this->metadata[$domain])) {
227 return $this->metadata[$domain];
230 if (isset($this->metadata[$domain][$key])) {
231 return $this->metadata[$domain][$key];
239 public function setMetadata($key, $value, $domain = 'messages')
241 $this->metadata[$domain][$key] = $value;
247 public function deleteMetadata($key = '', $domain = 'messages')
250 $this->metadata = array();
251 } elseif ('' == $key) {
252 unset($this->metadata[$domain]);
254 unset($this->metadata[$domain][$key]);
259 * Adds current values with the new values.
261 * @param array $values Values to add
263 private function addMetadata(array $values)
265 foreach ($values as $domain => $keys) {
266 foreach ($keys as $key => $value) {
267 $this->setMetadata($key, $value, $domain);