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;
20 * @author Fabien Potencier <fabien@symfony.com>
22 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
24 private $messages = array();
25 private $metadata = array();
26 private $resources = array();
28 private $fallbackCatalogue;
34 * @param string $locale The locale
35 * @param array $messages An array of messages classified by domain
37 public function __construct($locale, array $messages = array())
39 $this->locale = $locale;
40 $this->messages = $messages;
46 public function getLocale()
54 public function getDomains()
56 return array_keys($this->messages);
62 public function all($domain = null)
64 if (null === $domain) {
65 return $this->messages;
68 return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
74 public function set($id, $translation, $domain = 'messages')
76 $this->add(array($id => $translation), $domain);
82 public function has($id, $domain = 'messages')
84 if (isset($this->messages[$domain][$id])) {
88 if (null !== $this->fallbackCatalogue) {
89 return $this->fallbackCatalogue->has($id, $domain);
98 public function defines($id, $domain = 'messages')
100 return isset($this->messages[$domain][$id]);
106 public function get($id, $domain = 'messages')
108 if (isset($this->messages[$domain][$id])) {
109 return $this->messages[$domain][$id];
112 if (null !== $this->fallbackCatalogue) {
113 return $this->fallbackCatalogue->get($id, $domain);
122 public function replace($messages, $domain = 'messages')
124 $this->messages[$domain] = array();
126 $this->add($messages, $domain);
132 public function add($messages, $domain = 'messages')
134 if (!isset($this->messages[$domain])) {
135 $this->messages[$domain] = $messages;
137 $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
144 public function addCatalogue(MessageCatalogueInterface $catalogue)
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));
150 foreach ($catalogue->all() as $domain => $messages) {
151 $this->add($messages, $domain);
154 foreach ($catalogue->getResources() as $resource) {
155 $this->addResource($resource);
158 if ($catalogue instanceof MetadataAwareInterface) {
159 $metadata = $catalogue->getMetadata('', '');
160 $this->addMetadata($metadata);
167 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
169 // detect circular references
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()));
179 if ($c->getLocale() === $catalogue->getLocale()) {
180 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
183 foreach ($catalogue->getResources() as $resource) {
184 $c->addResource($resource);
186 } while ($c = $c->parent);
188 $catalogue->parent = $this;
189 $this->fallbackCatalogue = $catalogue;
191 foreach ($catalogue->getResources() as $resource) {
192 $this->addResource($resource);
199 public function getFallbackCatalogue()
201 return $this->fallbackCatalogue;
207 public function getResources()
209 return array_values($this->resources);
215 public function addResource(ResourceInterface $resource)
217 $this->resources[$resource->__toString()] = $resource;
223 public function getMetadata($key = '', $domain = 'messages')
226 return $this->metadata;
229 if (isset($this->metadata[$domain])) {
231 return $this->metadata[$domain];
234 if (isset($this->metadata[$domain][$key])) {
235 return $this->metadata[$domain][$key];
243 public function setMetadata($key, $value, $domain = 'messages')
245 $this->metadata[$domain][$key] = $value;
251 public function deleteMetadata($key = '', $domain = 'messages')
254 $this->metadata = array();
255 } elseif ('' == $key) {
256 unset($this->metadata[$domain]);
258 unset($this->metadata[$domain][$key]);
263 * Adds current values with the new values.
265 * @param array $values Values to add
267 private function addMetadata(array $values)
269 foreach ($values as $domain => $keys) {
270 foreach ($keys as $key => $value) {
271 $this->setMetadata($key, $value, $domain);