<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation;
use Symfony\Component\Config\Resource\ResourceInterface;
/**
* MessageCatalogue.
* @author Fabien Potencier <fabien@symfony.com>
class MessageCatalogue implements MessageCatalogueInterface
{
private $messages = array();
private $locale;
private $resources;
* Constructor.
* @param string $locale The locale
* @param array $messages An array of messages classified by domain
public function __construct($locale, array $messages = array())
$this->locale = $locale;
$this->messages = $messages;
$this->resources = array();
}
* {@inheritdoc}
public function getLocale()
return $this->locale;
public function getDomains()
return array_keys($this->messages);
public function all($domain = null)
if (null === $domain) {
return $this->messages;
return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
public function set($id, $translation, $domain = 'messages')
$this->add(array($id => $translation), $domain);
public function has($id, $domain = 'messages')
return isset($this->messages[$domain][$id]);
public function get($id, $domain = 'messages')
return isset($this->messages[$domain][$id]) ? $this->messages[$domain][$id] : $id;
public function replace($messages, $domain = 'messages')
$this->messages[$domain] = array();
$this->add($messages, $domain);
public function add($messages, $domain = 'messages')
if (!isset($this->messages[$domain])) {
$this->messages[$domain] = $messages;
} else {
$this->messages[$domain] = array_replace($this->messages[$domain], $messages);
public function addCatalogue(MessageCatalogueInterface $catalogue)
if ($catalogue->getLocale() !== $this->locale) {
throw new \LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
foreach ($catalogue->all() as $domain => $messages) {
foreach ($catalogue->getResources() as $resource) {
$this->addResource($resource);
public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
foreach ($catalogue->getDomains() as $domain) {
foreach ($catalogue->all($domain) as $id => $translation) {
if (false === $this->has($id, $domain)) {
$this->set($id, $translation, $domain);
public function getResources()
return array_values(array_unique($this->resources));
public function addResource(ResourceInterface $resource)
$this->resources[] = $resource;