MessageCatalogue.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. use Symfony\Component\Translation\Exception\LogicException;
  13. /**
  14. * MessageCatalogue.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
  19. {
  20. private $messages = array();
  21. private $metadata = array();
  22. private $resources = array();
  23. private $locale;
  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. public function __construct($locale, array $messages = array())
  33. {
  34. $this->locale = $locale;
  35. $this->messages = $messages;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getLocale()
  41. {
  42. return $this->locale;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getDomains()
  48. {
  49. return array_keys($this->messages);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function all($domain = null)
  55. {
  56. if (null === $domain) {
  57. return $this->messages;
  58. }
  59. return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function set($id, $translation, $domain = 'messages')
  65. {
  66. $this->add(array($id => $translation), $domain);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function has($id, $domain = 'messages')
  72. {
  73. if (isset($this->messages[$domain][$id])) {
  74. return true;
  75. }
  76. if (null !== $this->fallbackCatalogue) {
  77. return $this->fallbackCatalogue->has($id, $domain);
  78. }
  79. return false;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function defines($id, $domain = 'messages')
  85. {
  86. return isset($this->messages[$domain][$id]);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function get($id, $domain = 'messages')
  92. {
  93. if (isset($this->messages[$domain][$id])) {
  94. return $this->messages[$domain][$id];
  95. }
  96. if (null !== $this->fallbackCatalogue) {
  97. return $this->fallbackCatalogue->get($id, $domain);
  98. }
  99. return $id;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function replace($messages, $domain = 'messages')
  105. {
  106. $this->messages[$domain] = array();
  107. $this->add($messages, $domain);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function add($messages, $domain = 'messages')
  113. {
  114. if (!isset($this->messages[$domain])) {
  115. $this->messages[$domain] = $messages;
  116. } else {
  117. $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
  118. }
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function addCatalogue(MessageCatalogueInterface $catalogue)
  124. {
  125. if ($catalogue->getLocale() !== $this->locale) {
  126. throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
  127. }
  128. foreach ($catalogue->all() as $domain => $messages) {
  129. $this->add($messages, $domain);
  130. }
  131. foreach ($catalogue->getResources() as $resource) {
  132. $this->addResource($resource);
  133. }
  134. if ($catalogue instanceof MetadataAwareInterface) {
  135. $metadata = $catalogue->getMetadata('', '');
  136. $this->addMetadata($metadata);
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  143. {
  144. // detect circular references
  145. $c = $catalogue;
  146. while ($c = $c->getFallbackCatalogue()) {
  147. if ($c->getLocale() === $this->getLocale()) {
  148. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  149. }
  150. }
  151. $c = $this;
  152. do {
  153. if ($c->getLocale() === $catalogue->getLocale()) {
  154. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  155. }
  156. foreach ($catalogue->getResources() as $resource) {
  157. $c->addResource($resource);
  158. }
  159. } while ($c = $c->parent);
  160. $catalogue->parent = $this;
  161. $this->fallbackCatalogue = $catalogue;
  162. foreach ($catalogue->getResources() as $resource) {
  163. $this->addResource($resource);
  164. }
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function getFallbackCatalogue()
  170. {
  171. return $this->fallbackCatalogue;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function getResources()
  177. {
  178. return array_values($this->resources);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function addResource(ResourceInterface $resource)
  184. {
  185. $this->resources[$resource->__toString()] = $resource;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getMetadata($key = '', $domain = 'messages')
  191. {
  192. if ('' == $domain) {
  193. return $this->metadata;
  194. }
  195. if (isset($this->metadata[$domain])) {
  196. if ('' == $key) {
  197. return $this->metadata[$domain];
  198. }
  199. if (isset($this->metadata[$domain][$key])) {
  200. return $this->metadata[$domain][$key];
  201. }
  202. }
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function setMetadata($key, $value, $domain = 'messages')
  208. {
  209. $this->metadata[$domain][$key] = $value;
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function deleteMetadata($key = '', $domain = 'messages')
  215. {
  216. if ('' == $domain) {
  217. $this->metadata = array();
  218. } elseif ('' == $key) {
  219. unset($this->metadata[$domain]);
  220. } else {
  221. unset($this->metadata[$domain][$key]);
  222. }
  223. }
  224. /**
  225. * Adds current values with the new values.
  226. *
  227. * @param array $values Values to add
  228. */
  229. private function addMetadata(array $values)
  230. {
  231. foreach ($values as $domain => $keys) {
  232. foreach ($keys as $key => $value) {
  233. $this->setMetadata($key, $value, $domain);
  234. }
  235. }
  236. }
  237. }