MessageCatalogue.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. $this->locale = $locale;
  31. $this->messages = $messages;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getLocale()
  37. {
  38. return $this->locale;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getDomains()
  44. {
  45. $domains = [];
  46. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  47. foreach ($this->messages as $domain => $messages) {
  48. if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  49. $domain = substr($domain, 0, $i);
  50. }
  51. $domains[$domain] = $domain;
  52. }
  53. return array_values($domains);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function all(string $domain = null)
  59. {
  60. if (null !== $domain) {
  61. // skip messages merge if intl-icu requested explicitly
  62. if (false !== strpos($domain, self::INTL_DOMAIN_SUFFIX)) {
  63. return $this->messages[$domain] ?? [];
  64. }
  65. return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
  66. }
  67. $allMessages = [];
  68. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  69. foreach ($this->messages as $domain => $messages) {
  70. if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  71. $domain = substr($domain, 0, $i);
  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(string $id, string $translation, string $domain = 'messages')
  83. {
  84. $this->add([$id => $translation], $domain);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function has(string $id, string $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(string $id, string $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(string $id, string $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(array $messages, string $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(array $messages, string $domain = 'messages')
  134. {
  135. if (!isset($this->messages[$domain])) {
  136. $this->messages[$domain] = [];
  137. }
  138. $intlDomain = $domain;
  139. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  140. if (\strlen($domain) < $suffixLength || false === strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  141. $intlDomain .= self::INTL_DOMAIN_SUFFIX;
  142. }
  143. foreach ($messages as $id => $message) {
  144. if (isset($this->messages[$intlDomain]) && \array_key_exists($id, $this->messages[$intlDomain])) {
  145. $this->messages[$intlDomain][$id] = $message;
  146. } else {
  147. $this->messages[$domain][$id] = $message;
  148. }
  149. }
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function addCatalogue(MessageCatalogueInterface $catalogue)
  155. {
  156. if ($catalogue->getLocale() !== $this->locale) {
  157. throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
  158. }
  159. foreach ($catalogue->all() as $domain => $messages) {
  160. if ($intlMessages = $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {
  161. $this->add($intlMessages, $domain.self::INTL_DOMAIN_SUFFIX);
  162. $messages = array_diff_key($messages, $intlMessages);
  163. }
  164. $this->add($messages, $domain);
  165. }
  166. foreach ($catalogue->getResources() as $resource) {
  167. $this->addResource($resource);
  168. }
  169. if ($catalogue instanceof MetadataAwareInterface) {
  170. $metadata = $catalogue->getMetadata('', '');
  171. $this->addMetadata($metadata);
  172. }
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  178. {
  179. // detect circular references
  180. $c = $catalogue;
  181. while ($c = $c->getFallbackCatalogue()) {
  182. if ($c->getLocale() === $this->getLocale()) {
  183. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  184. }
  185. }
  186. $c = $this;
  187. do {
  188. if ($c->getLocale() === $catalogue->getLocale()) {
  189. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  190. }
  191. foreach ($catalogue->getResources() as $resource) {
  192. $c->addResource($resource);
  193. }
  194. } while ($c = $c->parent);
  195. $catalogue->parent = $this;
  196. $this->fallbackCatalogue = $catalogue;
  197. foreach ($catalogue->getResources() as $resource) {
  198. $this->addResource($resource);
  199. }
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function getFallbackCatalogue()
  205. {
  206. return $this->fallbackCatalogue;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function getResources()
  212. {
  213. return array_values($this->resources);
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function addResource(ResourceInterface $resource)
  219. {
  220. $this->resources[$resource->__toString()] = $resource;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function getMetadata(string $key = '', string $domain = 'messages')
  226. {
  227. if ('' == $domain) {
  228. return $this->metadata;
  229. }
  230. if (isset($this->metadata[$domain])) {
  231. if ('' == $key) {
  232. return $this->metadata[$domain];
  233. }
  234. if (isset($this->metadata[$domain][$key])) {
  235. return $this->metadata[$domain][$key];
  236. }
  237. }
  238. return null;
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function setMetadata(string $key, $value, string $domain = 'messages')
  244. {
  245. $this->metadata[$domain][$key] = $value;
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function deleteMetadata(string $key = '', string $domain = 'messages')
  251. {
  252. if ('' == $domain) {
  253. $this->metadata = [];
  254. } elseif ('' == $key) {
  255. unset($this->metadata[$domain]);
  256. } else {
  257. unset($this->metadata[$domain][$key]);
  258. }
  259. }
  260. /**
  261. * Adds current values with the new values.
  262. *
  263. * @param array $values Values to add
  264. */
  265. private function addMetadata(array $values)
  266. {
  267. foreach ($values as $domain => $keys) {
  268. foreach ($keys as $key => $value) {
  269. $this->setMetadata($key, $value, $domain);
  270. }
  271. }
  272. }
  273. }