1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
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 Translation\Bundle\Catalogue; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
15
|
|
|
use Symfony\Component\Translation\MetadataAwareInterface; |
16
|
|
|
use Translation\Bundle\Model\CatalogueMessage; |
17
|
|
|
use Translation\Bundle\Model\Metadata; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A manager that handle loaded catalogues. |
21
|
|
|
* |
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class CatalogueManager |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var MessageCatalogueInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $catalogues = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param MessageCatalogueInterface[] $catalogues |
33
|
|
|
*/ |
34
|
3 |
|
public function load(array $catalogues): void |
35
|
|
|
{ |
36
|
3 |
|
$this->catalogues = []; |
37
|
3 |
|
foreach ($catalogues as $c) { |
38
|
3 |
|
$this->catalogues[$c->getLocale()] = $c; |
39
|
|
|
} |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
public function getDomains(): array |
43
|
|
|
{ |
44
|
|
|
/** @var MessageCatalogueInterface $c */ |
45
|
|
|
$c = \reset($this->catalogues); |
46
|
|
|
|
47
|
|
|
return $c->getDomains(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return CatalogueMessage[] |
52
|
|
|
*/ |
53
|
1 |
|
public function getMessages(string $locale, string $domain): array |
54
|
|
|
{ |
55
|
1 |
|
$messages = []; |
56
|
1 |
|
if (!isset($this->catalogues[$locale])) { |
57
|
|
|
return $messages; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
foreach ($this->catalogues[$locale]->all($domain) as $key => $text) { |
61
|
1 |
|
$messages[] = $this->createMessage($this->catalogues[$locale], $locale, $domain, $key, $text); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $messages; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $config { |
69
|
|
|
* |
70
|
|
|
* @var string $domain |
71
|
|
|
* @var string $locale |
72
|
|
|
* @var bool $isNew |
73
|
|
|
* @var bool $isObsolete |
74
|
|
|
* @var bool $isApproved |
75
|
|
|
* } |
76
|
|
|
* |
77
|
|
|
* @return CatalogueMessage[] |
78
|
|
|
*/ |
79
|
2 |
|
public function findMessages(array $config = []): array |
80
|
|
|
{ |
81
|
2 |
|
$inputDomain = $config['domain'] ?? null; |
82
|
2 |
|
$isNew = $config['isNew'] ?? null; |
83
|
2 |
|
$isObsolete = $config['isObsolete'] ?? null; |
84
|
2 |
|
$isApproved = $config['isApproved'] ?? null; |
85
|
|
|
|
86
|
2 |
|
$messages = []; |
87
|
2 |
|
$catalogues = []; |
88
|
2 |
|
if (isset($config['locale'])) { |
89
|
2 |
|
$locale = $config['locale']; |
90
|
2 |
|
if (isset($this->catalogues[$locale])) { |
91
|
2 |
|
$catalogues = [$locale => $this->catalogues[$locale]]; |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
1 |
|
$catalogues = $this->catalogues; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
foreach ($catalogues as $locale => $catalogue) { |
98
|
2 |
|
$domains = $catalogue->getDomains(); |
99
|
2 |
|
if (null !== $inputDomain) { |
100
|
|
|
$domains = [$inputDomain]; |
101
|
|
|
} |
102
|
2 |
|
foreach ($domains as $domain) { |
103
|
2 |
|
foreach ($catalogue->all($domain) as $key => $text) { |
104
|
2 |
|
$messages[] = $this->createMessage($catalogue, $locale, $domain, $key, $text); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) { |
110
|
2 |
|
if (null !== $isNew && $m->isNew() !== $isNew) { |
111
|
1 |
|
return false; |
112
|
|
|
} |
113
|
2 |
|
if (null !== $isObsolete && $m->isObsolete() !== $isObsolete) { |
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
2 |
|
if (null !== $isApproved && $m->isApproved() !== $isApproved) { |
117
|
1 |
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
return true; |
121
|
2 |
|
}); |
122
|
|
|
|
123
|
2 |
|
return $messages; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $domain |
128
|
|
|
* @param string $key |
129
|
|
|
*/ |
130
|
|
|
public function getTranslations($domain, $key): array |
131
|
|
|
{ |
132
|
|
|
$translations = []; |
133
|
|
|
foreach ($this->catalogues as $locale => $catalogue) { |
134
|
|
|
if ($catalogue->has($key, $domain)) { |
135
|
|
|
$translations[$locale] = $catalogue->get($key, $domain); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $translations; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
private function createMessage(MessageCatalogueInterface $catalogue, string $locale, string $domain, string $key, string $text): CatalogueMessage |
143
|
|
|
{ |
144
|
3 |
|
$catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text); |
145
|
|
|
|
146
|
3 |
|
if ($catalogue instanceof MetadataAwareInterface) { |
147
|
3 |
|
$catalogueMessage->setMetadata(new Metadata($catalogue->getMetadata($key, $domain))); |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
return $catalogueMessage; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|