MessageCatalogue.php 8.6 KB

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