MessageCatalogue.php 8.5 KB

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