MessageCatalogue.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 = [];
  19. private $metadata = [];
  20. private $resources = [];
  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(?string $locale, array $messages = [])
  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. $domains = [];
  46. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  47. foreach ($this->messages as $domain => $messages) {
  48. if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  49. $domain = substr($domain, 0, $i);
  50. }
  51. $domains[$domain] = $domain;
  52. }
  53. return array_values($domains);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function all($domain = null)
  59. {
  60. if (null !== $domain) {
  61. return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
  62. }
  63. $allMessages = [];
  64. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  65. foreach ($this->messages as $domain => $messages) {
  66. if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  67. $domain = substr($domain, 0, $i);
  68. $allMessages[$domain] = $messages + ($allMessages[$domain] ?? []);
  69. } else {
  70. $allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages;
  71. }
  72. }
  73. return $allMessages;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function set($id, $translation, $domain = 'messages')
  79. {
  80. $this->add([$id => $translation], $domain);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function has($id, $domain = 'messages')
  86. {
  87. if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id])) {
  88. return true;
  89. }
  90. if (null !== $this->fallbackCatalogue) {
  91. return $this->fallbackCatalogue->has($id, $domain);
  92. }
  93. return false;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function defines($id, $domain = 'messages')
  99. {
  100. return isset($this->messages[$domain][$id]) || isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id]);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function get($id, $domain = 'messages')
  106. {
  107. if (isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id])) {
  108. return $this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id];
  109. }
  110. if (isset($this->messages[$domain][$id])) {
  111. return $this->messages[$domain][$id];
  112. }
  113. if (null !== $this->fallbackCatalogue) {
  114. return $this->fallbackCatalogue->get($id, $domain);
  115. }
  116. return $id;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function replace($messages, $domain = 'messages')
  122. {
  123. unset($this->messages[$domain], $this->messages[$domain.self::INTL_DOMAIN_SUFFIX]);
  124. $this->add($messages, $domain);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function add($messages, $domain = 'messages')
  130. {
  131. if (!isset($this->messages[$domain])) {
  132. $this->messages[$domain] = $messages;
  133. } else {
  134. $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
  135. }
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function addCatalogue(MessageCatalogueInterface $catalogue)
  141. {
  142. if ($catalogue->getLocale() !== $this->locale) {
  143. throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
  144. }
  145. foreach ($catalogue->all() as $domain => $messages) {
  146. if ($intlMessages = $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {
  147. $this->add($intlMessages, $domain.self::INTL_DOMAIN_SUFFIX);
  148. $messages = array_diff_key($messages, $intlMessages);
  149. }
  150. $this->add($messages, $domain);
  151. }
  152. foreach ($catalogue->getResources() as $resource) {
  153. $this->addResource($resource);
  154. }
  155. if ($catalogue instanceof MetadataAwareInterface) {
  156. $metadata = $catalogue->getMetadata('', '');
  157. $this->addMetadata($metadata);
  158. }
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  164. {
  165. // detect circular references
  166. $c = $catalogue;
  167. while ($c = $c->getFallbackCatalogue()) {
  168. if ($c->getLocale() === $this->getLocale()) {
  169. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  170. }
  171. }
  172. $c = $this;
  173. do {
  174. if ($c->getLocale() === $catalogue->getLocale()) {
  175. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  176. }
  177. foreach ($catalogue->getResources() as $resource) {
  178. $c->addResource($resource);
  179. }
  180. } while ($c = $c->parent);
  181. $catalogue->parent = $this;
  182. $this->fallbackCatalogue = $catalogue;
  183. foreach ($catalogue->getResources() as $resource) {
  184. $this->addResource($resource);
  185. }
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getFallbackCatalogue()
  191. {
  192. return $this->fallbackCatalogue;
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function getResources()
  198. {
  199. return array_values($this->resources);
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function addResource(ResourceInterface $resource)
  205. {
  206. $this->resources[$resource->__toString()] = $resource;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function getMetadata($key = '', $domain = 'messages')
  212. {
  213. if ('' == $domain) {
  214. return $this->metadata;
  215. }
  216. if (isset($this->metadata[$domain])) {
  217. if ('' == $key) {
  218. return $this->metadata[$domain];
  219. }
  220. if (isset($this->metadata[$domain][$key])) {
  221. return $this->metadata[$domain][$key];
  222. }
  223. }
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function setMetadata($key, $value, $domain = 'messages')
  229. {
  230. $this->metadata[$domain][$key] = $value;
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function deleteMetadata($key = '', $domain = 'messages')
  236. {
  237. if ('' == $domain) {
  238. $this->metadata = [];
  239. } elseif ('' == $key) {
  240. unset($this->metadata[$domain]);
  241. } else {
  242. unset($this->metadata[$domain][$key]);
  243. }
  244. }
  245. /**
  246. * Adds current values with the new values.
  247. *
  248. * @param array $values Values to add
  249. */
  250. private function addMetadata(array $values)
  251. {
  252. foreach ($values as $domain => $keys) {
  253. foreach ($keys as $key => $value) {
  254. $this->setMetadata($key, $value, $domain);
  255. }
  256. }
  257. }
  258. }