No Description

MessageCatalogue.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. if (!isset($this->messages[$domain])) {
  136. $this->messages[$domain] = [];
  137. }
  138. $intlDomain = $domain;
  139. if (!str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
  140. $intlDomain .= self::INTL_DOMAIN_SUFFIX;
  141. }
  142. foreach ($messages as $id => $message) {
  143. if (isset($this->messages[$intlDomain]) && \array_key_exists($id, $this->messages[$intlDomain])) {
  144. $this->messages[$intlDomain][$id] = $message;
  145. } else {
  146. $this->messages[$domain][$id] = $message;
  147. }
  148. }
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function addCatalogue(MessageCatalogueInterface $catalogue)
  154. {
  155. if ($catalogue->getLocale() !== $this->locale) {
  156. throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
  157. }
  158. foreach ($catalogue->all() as $domain => $messages) {
  159. if ($intlMessages = $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {
  160. $this->add($intlMessages, $domain.self::INTL_DOMAIN_SUFFIX);
  161. $messages = array_diff_key($messages, $intlMessages);
  162. }
  163. $this->add($messages, $domain);
  164. }
  165. foreach ($catalogue->getResources() as $resource) {
  166. $this->addResource($resource);
  167. }
  168. if ($catalogue instanceof MetadataAwareInterface) {
  169. $metadata = $catalogue->getMetadata('', '');
  170. $this->addMetadata($metadata);
  171. }
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  177. {
  178. // detect circular references
  179. $c = $catalogue;
  180. while ($c = $c->getFallbackCatalogue()) {
  181. if ($c->getLocale() === $this->getLocale()) {
  182. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  183. }
  184. }
  185. $c = $this;
  186. do {
  187. if ($c->getLocale() === $catalogue->getLocale()) {
  188. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  189. }
  190. foreach ($catalogue->getResources() as $resource) {
  191. $c->addResource($resource);
  192. }
  193. } while ($c = $c->parent);
  194. $catalogue->parent = $this;
  195. $this->fallbackCatalogue = $catalogue;
  196. foreach ($catalogue->getResources() as $resource) {
  197. $this->addResource($resource);
  198. }
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function getFallbackCatalogue()
  204. {
  205. return $this->fallbackCatalogue;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function getResources()
  211. {
  212. return array_values($this->resources);
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function addResource(ResourceInterface $resource)
  218. {
  219. $this->resources[$resource->__toString()] = $resource;
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function getMetadata($key = '', $domain = 'messages')
  225. {
  226. if ('' == $domain) {
  227. return $this->metadata;
  228. }
  229. if (isset($this->metadata[$domain])) {
  230. if ('' == $key) {
  231. return $this->metadata[$domain];
  232. }
  233. if (isset($this->metadata[$domain][$key])) {
  234. return $this->metadata[$domain][$key];
  235. }
  236. }
  237. return null;
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function setMetadata($key, $value, $domain = 'messages')
  243. {
  244. $this->metadata[$domain][$key] = $value;
  245. }
  246. /**
  247. * {@inheritdoc}
  248. */
  249. public function deleteMetadata($key = '', $domain = 'messages')
  250. {
  251. if ('' == $domain) {
  252. $this->metadata = [];
  253. } elseif ('' == $key) {
  254. unset($this->metadata[$domain]);
  255. } else {
  256. unset($this->metadata[$domain][$key]);
  257. }
  258. }
  259. /**
  260. * Adds current values with the new values.
  261. *
  262. * @param array $values Values to add
  263. */
  264. private function addMetadata(array $values)
  265. {
  266. foreach ($values as $domain => $keys) {
  267. foreach ($keys as $key => $value) {
  268. $this->setMetadata($key, $value, $domain);
  269. }
  270. }
  271. }
  272. }