MessageCatalogue.php 6.4 KB

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