MessageCatalogue.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  13. * MessageCatalogue.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class MessageCatalogue implements MessageCatalogueInterface
  18. {
  19. private $messages = array();
  20. private $locale;
  21. private $resources;
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $locale The locale
  26. * @param array $messages An array of messages classified by domain
  27. */
  28. public function __construct($locale, array $messages = array())
  29. {
  30. $this->locale = $locale;
  31. $this->messages = $messages;
  32. $this->resources = array();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getLocale()
  38. {
  39. return $this->locale;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getDomains()
  45. {
  46. return array_keys($this->messages);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function all($domain = null)
  52. {
  53. if (null === $domain) {
  54. return $this->messages;
  55. }
  56. return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function set($id, $translation, $domain = 'messages')
  62. {
  63. $this->add(array($id => $translation), $domain);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function has($id, $domain = 'messages')
  69. {
  70. return isset($this->messages[$domain][$id]);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function get($id, $domain = 'messages')
  76. {
  77. return isset($this->messages[$domain][$id]) ? $this->messages[$domain][$id] : $id;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function replace($messages, $domain = 'messages')
  83. {
  84. $this->messages[$domain] = array();
  85. $this->add($messages, $domain);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function add($messages, $domain = 'messages')
  91. {
  92. if (!isset($this->messages[$domain])) {
  93. $this->messages[$domain] = $messages;
  94. } else {
  95. $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function addCatalogue(MessageCatalogueInterface $catalogue)
  102. {
  103. if ($catalogue->getLocale() !== $this->locale) {
  104. throw new \LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
  105. }
  106. foreach ($catalogue->all() as $domain => $messages) {
  107. $this->add($messages, $domain);
  108. }
  109. foreach ($catalogue->getResources() as $resource) {
  110. $this->addResource($resource);
  111. }
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  117. {
  118. foreach ($catalogue->getDomains() as $domain) {
  119. foreach ($catalogue->all($domain) as $id => $translation) {
  120. if (false === $this->has($id, $domain)) {
  121. $this->set($id, $translation, $domain);
  122. }
  123. }
  124. }
  125. foreach ($catalogue->getResources() as $resource) {
  126. $this->addResource($resource);
  127. }
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getResources()
  133. {
  134. return array_values(array_unique($this->resources));
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function addResource(ResourceInterface $resource)
  140. {
  141. $this->resources[] = $resource;
  142. }
  143. }