菜谱项目

MessageCatalogue.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
  17. {
  18. private $messages = array();
  19. private $metadata = array();
  20. private $resources = array();
  21. private $locale;
  22. private $fallbackCatalogue;
  23. private $parent;
  24. /**
  25. * @param string $locale The locale
  26. * @param array $messages An array of messages classified by domain
  27. */
  28. public function __construct($locale, array $messages = array())
  29. {
  30. $this->locale = $locale;
  31. $this->messages = $messages;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getLocale()
  37. {
  38. return $this->locale;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getDomains()
  44. {
  45. return array_keys($this->messages);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function all($domain = null)
  51. {
  52. if (null === $domain) {
  53. return $this->messages;
  54. }
  55. return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function set($id, $translation, $domain = 'messages')
  61. {
  62. $this->add(array($id => $translation), $domain);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function has($id, $domain = 'messages')
  68. {
  69. if (isset($this->messages[$domain][$id])) {
  70. return true;
  71. }
  72. if (null !== $this->fallbackCatalogue) {
  73. return $this->fallbackCatalogue->has($id, $domain);
  74. }
  75. return false;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function defines($id, $domain = 'messages')
  81. {
  82. return isset($this->messages[$domain][$id]);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function get($id, $domain = 'messages')
  88. {
  89. if (isset($this->messages[$domain][$id])) {
  90. return $this->messages[$domain][$id];
  91. }
  92. if (null !== $this->fallbackCatalogue) {
  93. return $this->fallbackCatalogue->get($id, $domain);
  94. }
  95. return $id;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function replace($messages, $domain = 'messages')
  101. {
  102. $this->messages[$domain] = array();
  103. $this->add($messages, $domain);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function add($messages, $domain = 'messages')
  109. {
  110. if (!isset($this->messages[$domain])) {
  111. $this->messages[$domain] = $messages;
  112. } else {
  113. $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function addCatalogue(MessageCatalogueInterface $catalogue)
  120. {
  121. if ($catalogue->getLocale() !== $this->locale) {
  122. throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
  123. }
  124. foreach ($catalogue->all() as $domain => $messages) {
  125. $this->add($messages, $domain);
  126. }
  127. foreach ($catalogue->getResources() as $resource) {
  128. $this->addResource($resource);
  129. }
  130. if ($catalogue instanceof MetadataAwareInterface) {
  131. $metadata = $catalogue->getMetadata('', '');
  132. $this->addMetadata($metadata);
  133. }
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  139. {
  140. // detect circular references
  141. $c = $catalogue;
  142. while ($c = $c->getFallbackCatalogue()) {
  143. if ($c->getLocale() === $this->getLocale()) {
  144. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  145. }
  146. }
  147. $c = $this;
  148. do {
  149. if ($c->getLocale() === $catalogue->getLocale()) {
  150. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  151. }
  152. foreach ($catalogue->getResources() as $resource) {
  153. $c->addResource($resource);
  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. }