MessageCatalogue.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation;
  11. use Symfony\Component\Config\Resource\ResourceInterface;
  12. /**
  13. * MessageCatalogue.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class MessageCatalogue implements MessageCatalogueInterface
  20. {
  21. private $messages = array();
  22. private $locale;
  23. private $resources;
  24. private $fallbackCatalogue;
  25. private $parent;
  26. /**
  27. * Constructor.
  28. *
  29. * @param string $locale The locale
  30. * @param array $messages An array of messages classified by domain
  31. *
  32. * @api
  33. */
  34. public function __construct($locale, array $messages = array())
  35. {
  36. $this->locale = $locale;
  37. $this->messages = $messages;
  38. $this->resources = array();
  39. }
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @api
  44. */
  45. public function getLocale()
  46. {
  47. return $this->locale;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. *
  52. * @api
  53. */
  54. public function getDomains()
  55. {
  56. return array_keys($this->messages);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. *
  61. * @api
  62. */
  63. public function all($domain = null)
  64. {
  65. if (null === $domain) {
  66. return $this->messages;
  67. }
  68. return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
  69. }
  70. /**
  71. * {@inheritdoc}
  72. *
  73. * @api
  74. */
  75. public function set($id, $translation, $domain = 'messages')
  76. {
  77. $this->add(array($id => $translation), $domain);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. *
  82. * @api
  83. */
  84. public function has($id, $domain = 'messages')
  85. {
  86. if (isset($this->messages[$domain][$id])) {
  87. return true;
  88. }
  89. if (null !== $this->fallbackCatalogue) {
  90. return $this->fallbackCatalogue->has($id, $domain);
  91. }
  92. return false;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function defines($id, $domain = 'messages')
  98. {
  99. return isset($this->messages[$domain][$id]);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. *
  104. * @api
  105. */
  106. public function get($id, $domain = 'messages')
  107. {
  108. if (isset($this->messages[$domain][$id])) {
  109. return $this->messages[$domain][$id];
  110. }
  111. if (null !== $this->fallbackCatalogue) {
  112. return $this->fallbackCatalogue->get($id, $domain);
  113. }
  114. return $id;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. * @api
  120. */
  121. public function replace($messages, $domain = 'messages')
  122. {
  123. $this->messages[$domain] = array();
  124. $this->add($messages, $domain);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. *
  129. * @api
  130. */
  131. public function add($messages, $domain = 'messages')
  132. {
  133. if (!isset($this->messages[$domain])) {
  134. $this->messages[$domain] = $messages;
  135. } else {
  136. $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. *
  142. * @api
  143. */
  144. public function addCatalogue(MessageCatalogueInterface $catalogue)
  145. {
  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));
  148. }
  149. foreach ($catalogue->all() as $domain => $messages) {
  150. $this->add($messages, $domain);
  151. }
  152. foreach ($catalogue->getResources() as $resource) {
  153. $this->addResource($resource);
  154. }
  155. }
  156. /**
  157. * {@inheritdoc}
  158. *
  159. * @api
  160. */
  161. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  162. {
  163. // detect circular references
  164. $c = $this;
  165. do {
  166. if ($c->getLocale() === $catalogue->getLocale()) {
  167. throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  168. }
  169. } while ($c = $c->getParent());
  170. $catalogue->setParent($this);
  171. $this->fallbackCatalogue = $catalogue;
  172. foreach ($catalogue->getResources() as $resource) {
  173. $this->addResource($resource);
  174. }
  175. }
  176. public function getParent()
  177. {
  178. return $this->parent;
  179. }
  180. public function setParent(self $parent)
  181. {
  182. $this->parent = $parent;
  183. }
  184. /**
  185. * {@inheritdoc}
  186. *
  187. * @api
  188. */
  189. public function getResources()
  190. {
  191. return array_values(array_unique($this->resources));
  192. }
  193. /**
  194. * {@inheritdoc}
  195. *
  196. * @api
  197. */
  198. public function addResource(ResourceInterface $resource)
  199. {
  200. $this->resources[] = $resource;
  201. }
  202. }