vendor/symfony/translation/MessageCatalogue.php line 66

Open in your IDE?
  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 MessageCatalogueInterfaceMetadataAwareInterface
  17. {
  18.     private $messages = [];
  19.     private $metadata = [];
  20.     private $resources = [];
  21.     private $locale;
  22.     private $fallbackCatalogue;
  23.     private $parent;
  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()
  36.     {
  37.         return $this->locale;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function getDomains()
  43.     {
  44.         $domains = [];
  45.         foreach ($this->messages as $domain => $messages) {
  46.             if (str_ends_with($domainself::INTL_DOMAIN_SUFFIX)) {
  47.                 $domain substr($domain0, -\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)
  57.     {
  58.         if (null !== $domain) {
  59.             // skip messages merge if intl-icu requested explicitly
  60.             if (str_ends_with($domainself::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($domainself::INTL_DOMAIN_SUFFIX)) {
  68.                 $domain substr($domain0, -\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 $idstring $translationstring $domain 'messages')
  80.     {
  81.         $this->add([$id => $translation], $domain);
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     public function has(string $idstring $domain 'messages')
  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 $idstring $domain 'messages')
  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 $idstring $domain 'messages')
  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 $messagesstring $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 $messagesstring $domain 'messages')
  131.     {
  132.         $altDomain str_ends_with($domainself::INTL_DOMAIN_SUFFIX) ? substr($domain0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.self::INTL_DOMAIN_SUFFIX;
  133.         foreach ($messages as $id => $message) {
  134.             unset($this->messages[$altDomain][$id]);
  135.             $this->messages[$domain][$id] = $message;
  136.         }
  137.         if ([] === ($this->messages[$altDomain] ?? null)) {
  138.             unset($this->messages[$altDomain]);
  139.         }
  140.     }
  141.     /**
  142.      * {@inheritdoc}
  143.      */
  144.     public function addCatalogue(MessageCatalogueInterface $catalogue)
  145.     {
  146.         if ($catalogue->getLocale() !== $this->locale) {
  147.             throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".'$catalogue->getLocale(), $this->locale));
  148.         }
  149.         foreach ($catalogue->all() as $domain => $messages) {
  150.             if ($intlMessages $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {
  151.                 $this->add($intlMessages$domain.self::INTL_DOMAIN_SUFFIX);
  152.                 $messages array_diff_key($messages$intlMessages);
  153.             }
  154.             $this->add($messages$domain);
  155.         }
  156.         foreach ($catalogue->getResources() as $resource) {
  157.             $this->addResource($resource);
  158.         }
  159.         if ($catalogue instanceof MetadataAwareInterface) {
  160.             $metadata $catalogue->getMetadata('''');
  161.             $this->addMetadata($metadata);
  162.         }
  163.     }
  164.     /**
  165.      * {@inheritdoc}
  166.      */
  167.     public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  168.     {
  169.         // detect circular references
  170.         $c $catalogue;
  171.         while ($c $c->getFallbackCatalogue()) {
  172.             if ($c->getLocale() === $this->getLocale()) {
  173.                 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".'$catalogue->getLocale()));
  174.             }
  175.         }
  176.         $c $this;
  177.         do {
  178.             if ($c->getLocale() === $catalogue->getLocale()) {
  179.                 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".'$catalogue->getLocale()));
  180.             }
  181.             foreach ($catalogue->getResources() as $resource) {
  182.                 $c->addResource($resource);
  183.             }
  184.         } while ($c $c->parent);
  185.         $catalogue->parent $this;
  186.         $this->fallbackCatalogue $catalogue;
  187.         foreach ($catalogue->getResources() as $resource) {
  188.             $this->addResource($resource);
  189.         }
  190.     }
  191.     /**
  192.      * {@inheritdoc}
  193.      */
  194.     public function getFallbackCatalogue()
  195.     {
  196.         return $this->fallbackCatalogue;
  197.     }
  198.     /**
  199.      * {@inheritdoc}
  200.      */
  201.     public function getResources()
  202.     {
  203.         return array_values($this->resources);
  204.     }
  205.     /**
  206.      * {@inheritdoc}
  207.      */
  208.     public function addResource(ResourceInterface $resource)
  209.     {
  210.         $this->resources[$resource->__toString()] = $resource;
  211.     }
  212.     /**
  213.      * {@inheritdoc}
  214.      */
  215.     public function getMetadata(string $key ''string $domain 'messages')
  216.     {
  217.         if ('' == $domain) {
  218.             return $this->metadata;
  219.         }
  220.         if (isset($this->metadata[$domain])) {
  221.             if ('' == $key) {
  222.                 return $this->metadata[$domain];
  223.             }
  224.             if (isset($this->metadata[$domain][$key])) {
  225.                 return $this->metadata[$domain][$key];
  226.             }
  227.         }
  228.         return null;
  229.     }
  230.     /**
  231.      * {@inheritdoc}
  232.      */
  233.     public function setMetadata(string $key$valuestring $domain 'messages')
  234.     {
  235.         $this->metadata[$domain][$key] = $value;
  236.     }
  237.     /**
  238.      * {@inheritdoc}
  239.      */
  240.     public function deleteMetadata(string $key ''string $domain 'messages')
  241.     {
  242.         if ('' == $domain) {
  243.             $this->metadata = [];
  244.         } elseif ('' == $key) {
  245.             unset($this->metadata[$domain]);
  246.         } else {
  247.             unset($this->metadata[$domain][$key]);
  248.         }
  249.     }
  250.     /**
  251.      * Adds current values with the new values.
  252.      *
  253.      * @param array $values Values to add
  254.      */
  255.     private function addMetadata(array $values)
  256.     {
  257.         foreach ($values as $domain => $keys) {
  258.             foreach ($keys as $key => $value) {
  259.                 $this->setMetadata($key$value$domain);
  260.             }
  261.         }
  262.     }
  263. }