MessageCatalogue.php 9.8 KB

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