SymfonyComponentTranslationMessageCatalogue

MessageCatalogue.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/MessageCatalogue.php  
  1. class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface 
  2. private $messages = array(); 
  3. private $metadata = array(); 
  4. private $resources = array(); 
  5. private $locale; 
  6. private $fallbackCatalogue; 
  7. private $parent; 
  8.  
  9. /** 
  10. * Constructor. 
  11. * @param string $locale The locale 
  12. * @param array $messages An array of messages classified by domain 
  13. */ 
  14. public function __construct($locale, array $messages = array()) 
  15. $this->locale = $locale; 
  16. $this->messages = $messages; 
  17.  
  18. /** 
  19. * {@inheritdoc} 
  20. */ 
  21. public function getLocale() 
  22. return $this->locale; 
  23.  
  24. /** 
  25. * {@inheritdoc} 
  26. */ 
  27. public function getDomains() 
  28. return array_keys($this->messages); 
  29.  
  30. /** 
  31. * {@inheritdoc} 
  32. */ 
  33. public function all($domain = null) 
  34. if (null === $domain) { 
  35. return $this->messages; 
  36.  
  37. return isset($this->messages[$domain]) ? $this->messages[$domain] : array(); 
  38.  
  39. /** 
  40. * {@inheritdoc} 
  41. */ 
  42. public function set($id, $translation, $domain = 'messages') 
  43. $this->add(array($id => $translation), $domain); 
  44.  
  45. /** 
  46. * {@inheritdoc} 
  47. */ 
  48. public function has($id, $domain = 'messages') 
  49. if (isset($this->messages[$domain][$id])) { 
  50. return true; 
  51.  
  52. if (null !== $this->fallbackCatalogue) { 
  53. return $this->fallbackCatalogue->has($id, $domain); 
  54.  
  55. return false; 
  56.  
  57. /** 
  58. * {@inheritdoc} 
  59. */ 
  60. public function defines($id, $domain = 'messages') 
  61. return isset($this->messages[$domain][$id]); 
  62.  
  63. /** 
  64. * {@inheritdoc} 
  65. */ 
  66. public function get($id, $domain = 'messages') 
  67. if (isset($this->messages[$domain][$id])) { 
  68. return $this->messages[$domain][$id]; 
  69.  
  70. if (null !== $this->fallbackCatalogue) { 
  71. return $this->fallbackCatalogue->get($id, $domain); 
  72.  
  73. return $id; 
  74.  
  75. /** 
  76. * {@inheritdoc} 
  77. */ 
  78. public function replace($messages, $domain = 'messages') 
  79. $this->messages[$domain] = array(); 
  80.  
  81. $this->add($messages, $domain); 
  82.  
  83. /** 
  84. * {@inheritdoc} 
  85. */ 
  86. public function add($messages, $domain = 'messages') 
  87. if (!isset($this->messages[$domain])) { 
  88. $this->messages[$domain] = $messages; 
  89. } else { 
  90. $this->messages[$domain] = array_replace($this->messages[$domain], $messages); 
  91.  
  92. /** 
  93. * {@inheritdoc} 
  94. */ 
  95. public function addCatalogue(MessageCatalogueInterface $catalogue) 
  96. if ($catalogue->getLocale() !== $this->locale) { 
  97. throw new \LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale)); 
  98.  
  99. foreach ($catalogue->all() as $domain => $messages) { 
  100. $this->add($messages, $domain); 
  101.  
  102. foreach ($catalogue->getResources() as $resource) { 
  103. $this->addResource($resource); 
  104.  
  105. if ($catalogue instanceof MetadataAwareInterface) { 
  106. $metadata = $catalogue->getMetadata('', ''); 
  107. $this->addMetadata($metadata); 
  108.  
  109. /** 
  110. * {@inheritdoc} 
  111. */ 
  112. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue) 
  113. // detect circular references 
  114. $c = $catalogue; 
  115. while ($c = $c->getFallbackCatalogue()) { 
  116. if ($c->getLocale() === $this->getLocale()) { 
  117. throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); 
  118.  
  119. $c = $this; 
  120. do { 
  121. if ($c->getLocale() === $catalogue->getLocale()) { 
  122. throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale())); 
  123. } while ($c = $c->parent); 
  124.  
  125. $catalogue->parent = $this; 
  126. $this->fallbackCatalogue = $catalogue; 
  127.  
  128. foreach ($catalogue->getResources() as $resource) { 
  129. $this->addResource($resource); 
  130.  
  131. /** 
  132. * {@inheritdoc} 
  133. */ 
  134. public function getFallbackCatalogue() 
  135. return $this->fallbackCatalogue; 
  136.  
  137. /** 
  138. * {@inheritdoc} 
  139. */ 
  140. public function getResources() 
  141. return array_values($this->resources); 
  142.  
  143. /** 
  144. * {@inheritdoc} 
  145. */ 
  146. public function addResource(ResourceInterface $resource) 
  147. $this->resources[$resource->__toString()] = $resource; 
  148.  
  149. /** 
  150. * {@inheritdoc} 
  151. */ 
  152. public function getMetadata($key = '', $domain = 'messages') 
  153. if ('' == $domain) { 
  154. return $this->metadata; 
  155.  
  156. if (isset($this->metadata[$domain])) { 
  157. if ('' == $key) { 
  158. return $this->metadata[$domain]; 
  159.  
  160. if (isset($this->metadata[$domain][$key])) { 
  161. return $this->metadata[$domain][$key]; 
  162.  
  163. /** 
  164. * {@inheritdoc} 
  165. */ 
  166. public function setMetadata($key, $value, $domain = 'messages') 
  167. $this->metadata[$domain][$key] = $value; 
  168.  
  169. /** 
  170. * {@inheritdoc} 
  171. */ 
  172. public function deleteMetadata($key = '', $domain = 'messages') 
  173. if ('' == $domain) { 
  174. $this->metadata = array(); 
  175. } elseif ('' == $key) { 
  176. unset($this->metadata[$domain]); 
  177. } else { 
  178. unset($this->metadata[$domain][$key]); 
  179.  
  180. /** 
  181. * Adds current values with the new values. 
  182. * @param array $values Values to add 
  183. */ 
  184. private function addMetadata(array $values) 
  185. foreach ($values as $domain => $keys) { 
  186. foreach ($keys as $key => $value) { 
  187. $this->setMetadata($key, $value, $domain);