blob: eafaffd3f10d4ddf615100ff6ed1b9d9bce1f1a8 [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) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010035 $bag->addCatalogue($this->filterCatalogue($catalogue, $domain));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020036 }
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}