Catalogue
1 month ago
Command
1 month ago
DataCollector
1 month ago
DependencyInjection
1 month ago
Dumper
1 month ago
Exception
1 month ago
Extractor
1 month ago
Formatter
1 month ago
Loader
1 month ago
Provider
1 month ago
Reader
1 month ago
Resources
1 month ago
Test
1 month ago
Util
1 month ago
Writer
1 month ago
DataCollectorTranslator.php
1 month ago
IdentityTranslator.php
2 years ago
LoggingTranslator.php
1 month ago
MessageCatalogue.php
1 month ago
MessageCatalogueInterface.php
1 month ago
MetadataAwareInterface.php
1 month ago
PseudoLocalizationTranslator.php
1 month ago
TranslatableMessage.php
1 month ago
Translator.php
1 month ago
TranslatorBag.php
1 month ago
TranslatorBagInterface.php
1 month ago
TranslatorBag.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWPSCOPED\Symfony\Component\Translation; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Translation\Catalogue\AbstractOperation; |
| 14 | use IAWPSCOPED\Symfony\Component\Translation\Catalogue\TargetOperation; |
| 15 | /** @internal */ |
| 16 | final class TranslatorBag implements TranslatorBagInterface |
| 17 | { |
| 18 | /** @var MessageCatalogue[] */ |
| 19 | private array $catalogues = []; |
| 20 | public function addCatalogue(MessageCatalogue $catalogue) : void |
| 21 | { |
| 22 | if (null !== ($existingCatalogue = $this->getCatalogue($catalogue->getLocale()))) { |
| 23 | $catalogue->addCatalogue($existingCatalogue); |
| 24 | } |
| 25 | $this->catalogues[$catalogue->getLocale()] = $catalogue; |
| 26 | } |
| 27 | public function addBag(TranslatorBagInterface $bag) : void |
| 28 | { |
| 29 | foreach ($bag->getCatalogues() as $catalogue) { |
| 30 | $this->addCatalogue($catalogue); |
| 31 | } |
| 32 | } |
| 33 | /** |
| 34 | * {@inheritdoc} |
| 35 | */ |
| 36 | public function getCatalogue(string $locale = null) : MessageCatalogueInterface |
| 37 | { |
| 38 | if (null === $locale || !isset($this->catalogues[$locale])) { |
| 39 | $this->catalogues[$locale] = new MessageCatalogue($locale); |
| 40 | } |
| 41 | return $this->catalogues[$locale]; |
| 42 | } |
| 43 | /** |
| 44 | * {@inheritdoc} |
| 45 | */ |
| 46 | public function getCatalogues() : array |
| 47 | { |
| 48 | return \array_values($this->catalogues); |
| 49 | } |
| 50 | public function diff(TranslatorBagInterface $diffBag) : self |
| 51 | { |
| 52 | $diff = new self(); |
| 53 | foreach ($this->catalogues as $locale => $catalogue) { |
| 54 | if (null === ($diffCatalogue = $diffBag->getCatalogue($locale))) { |
| 55 | $diff->addCatalogue($catalogue); |
| 56 | continue; |
| 57 | } |
| 58 | $operation = new TargetOperation($diffCatalogue, $catalogue); |
| 59 | $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::NEW_BATCH); |
| 60 | $newCatalogue = new MessageCatalogue($locale); |
| 61 | foreach ($operation->getDomains() as $domain) { |
| 62 | $newCatalogue->add($operation->getNewMessages($domain), $domain); |
| 63 | } |
| 64 | $diff->addCatalogue($newCatalogue); |
| 65 | } |
| 66 | return $diff; |
| 67 | } |
| 68 | public function intersect(TranslatorBagInterface $intersectBag) : self |
| 69 | { |
| 70 | $diff = new self(); |
| 71 | foreach ($this->catalogues as $locale => $catalogue) { |
| 72 | if (null === ($intersectCatalogue = $intersectBag->getCatalogue($locale))) { |
| 73 | continue; |
| 74 | } |
| 75 | $operation = new TargetOperation($catalogue, $intersectCatalogue); |
| 76 | $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::OBSOLETE_BATCH); |
| 77 | $obsoleteCatalogue = new MessageCatalogue($locale); |
| 78 | foreach ($operation->getDomains() as $domain) { |
| 79 | $obsoleteCatalogue->add(\array_diff($operation->getMessages($domain), $operation->getNewMessages($domain)), $domain); |
| 80 | } |
| 81 | $diff->addCatalogue($obsoleteCatalogue); |
| 82 | } |
| 83 | return $diff; |
| 84 | } |
| 85 | } |
| 86 |