MessageCatalogue.php 8.9 KB

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