blob: 6a2b1ba86ded3db35eb4fcc6409190596278ea0d [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Command;
13
14use Symfony\Component\Translation\MessageCatalogue;
15use Symfony\Component\Translation\MessageCatalogueInterface;
16use Symfony\Component\Translation\TranslatorBag;
17
18/**
19 * @internal
20 */
21trait 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) {
35 $catalogue = $this->filterCatalogue($catalogue, $domain);
36 $bag->addCatalogue($catalogue);
37 }
38 } else {
39 $bag->addCatalogue($catalogue);
40 }
41 }
42
43 return $bag;
44 }
45
46 private function filterCatalogue(MessageCatalogue $catalogue, string $domain): MessageCatalogue
47 {
48 $filteredCatalogue = new MessageCatalogue($catalogue->getLocale());
49
50 // extract intl-icu messages only
51 $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
52 if ($intlMessages = $catalogue->all($intlDomain)) {
53 $filteredCatalogue->add($intlMessages, $intlDomain);
54 }
55
56 // extract all messages and subtract intl-icu messages
57 if ($messages = array_diff($catalogue->all($domain), $intlMessages)) {
58 $filteredCatalogue->add($messages, $domain);
59 }
60 foreach ($catalogue->getResources() as $resource) {
61 $filteredCatalogue->addResource($resource);
62 }
63
64 if ($metadata = $catalogue->getMetadata('', $intlDomain)) {
65 foreach ($metadata as $k => $v) {
66 $filteredCatalogue->setMetadata($k, $v, $intlDomain);
67 }
68 }
69
70 if ($metadata = $catalogue->getMetadata('', $domain)) {
71 foreach ($metadata as $k => $v) {
72 $filteredCatalogue->setMetadata($k, $v, $domain);
73 }
74 }
75
76 return $filteredCatalogue;
77 }
78}