MessageCatalogue.php 8.2 KB

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