vendor/symfony/translation/MessageCatalogue.php line 58

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