Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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 | |
| 12 | namespace Symfony\Component\Translation\Command; |
| 13 | |
| 14 | use Symfony\Component\Translation\MessageCatalogue; |
| 15 | use Symfony\Component\Translation\MessageCatalogueInterface; |
| 16 | use Symfony\Component\Translation\TranslatorBag; |
| 17 | |
| 18 | /** |
| 19 | * @internal |
| 20 | */ |
| 21 | trait TranslationTrait |
| 22 | { |
| 23 | private function readLocalTranslations(array $locales, array $domains, array $transPaths): TranslatorBag |
| 24 | { |
| 25 | $bag = new TranslatorBag(); |
| 26 | |
| 27 | foreach ($locales as $locale) { |
| 28 | $catalogue = new MessageCatalogue($locale); |
| 29 | foreach ($transPaths as $path) { |
| 30 | $this->reader->read($path, $catalogue); |
| 31 | } |
| 32 | |
| 33 | if ($domains) { |
| 34 | foreach ($domains as $domain) { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 35 | $bag->addCatalogue($this->filterCatalogue($catalogue, $domain)); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 36 | } |
| 37 | } else { |
| 38 | $bag->addCatalogue($catalogue); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return $bag; |
| 43 | } |
| 44 | |
| 45 | private function filterCatalogue(MessageCatalogue $catalogue, string $domain): MessageCatalogue |
| 46 | { |
| 47 | $filteredCatalogue = new MessageCatalogue($catalogue->getLocale()); |
| 48 | |
| 49 | // extract intl-icu messages only |
| 50 | $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX; |
| 51 | if ($intlMessages = $catalogue->all($intlDomain)) { |
| 52 | $filteredCatalogue->add($intlMessages, $intlDomain); |
| 53 | } |
| 54 | |
| 55 | // extract all messages and subtract intl-icu messages |
| 56 | if ($messages = array_diff($catalogue->all($domain), $intlMessages)) { |
| 57 | $filteredCatalogue->add($messages, $domain); |
| 58 | } |
| 59 | foreach ($catalogue->getResources() as $resource) { |
| 60 | $filteredCatalogue->addResource($resource); |
| 61 | } |
| 62 | |
| 63 | if ($metadata = $catalogue->getMetadata('', $intlDomain)) { |
| 64 | foreach ($metadata as $k => $v) { |
| 65 | $filteredCatalogue->setMetadata($k, $v, $intlDomain); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if ($metadata = $catalogue->getMetadata('', $domain)) { |
| 70 | foreach ($metadata as $k => $v) { |
| 71 | $filteredCatalogue->setMetadata($k, $v, $domain); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $filteredCatalogue; |
| 76 | } |
| 77 | } |