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) |
35
|
|
|
{ |
36
|
3 |
|
$this->catalogues = []; |
37
|
3 |
|
foreach ($catalogues as $c) { |
38
|
3 |
|
$this->catalogues[$c->getLocale()] = $c; |
39
|
|
|
} |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getDomains() |
46
|
|
|
{ |
47
|
|
|
/** @var MessageCatalogueInterface $c */ |
48
|
|
|
$c = \reset($this->catalogues); |
49
|
|
|
|
50
|
|
|
return $c->getDomains(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $locale |
55
|
|
|
* @param string $domain |
56
|
|
|
* |
57
|
|
|
* @return CatalogueMessage[] |
58
|
|
|
*/ |
59
|
1 |
|
public function getMessages($locale, $domain) |
60
|
|
|
{ |
61
|
1 |
|
$messages = []; |
62
|
1 |
|
if (!isset($this->catalogues[$locale])) { |
63
|
|
|
return $messages; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
foreach ($this->catalogues[$locale]->all($domain) as $key => $text) { |
67
|
1 |
|
$messages[] = $this->createMessage($this->catalogues[$locale], $locale, $domain, $key, $text); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $messages; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $config { |
75
|
|
|
* |
76
|
|
|
* @var string $domain |
77
|
|
|
* @var string $locale |
78
|
|
|
* @var bool $isNew |
79
|
|
|
* @var bool $isObsolete |
80
|
|
|
* @var bool $isApproved |
81
|
|
|
* } |
82
|
|
|
* |
83
|
|
|
* @return CatalogueMessage[] |
84
|
|
|
*/ |
85
|
2 |
|
public function findMessages(array $config = []) |
86
|
|
|
{ |
87
|
2 |
|
$inputDomain = isset($config['domain']) ? $config['domain'] : null; |
88
|
2 |
|
$isNew = isset($config['isNew']) ? $config['isNew'] : null; |
89
|
2 |
|
$isObsolete = isset($config['isObsolete']) ? $config['isObsolete'] : null; |
90
|
2 |
|
$isApproved = isset($config['isApproved']) ? $config['isApproved'] : null; |
91
|
|
|
|
92
|
2 |
|
$messages = []; |
93
|
2 |
|
$catalogues = []; |
94
|
2 |
|
if (isset($config['locale'])) { |
95
|
2 |
|
$locale = $config['locale']; |
96
|
2 |
|
if (isset($this->catalogues[$locale])) { |
97
|
2 |
|
$catalogues = [$locale => $this->catalogues[$locale]]; |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
1 |
|
$catalogues = $this->catalogues; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
foreach ($catalogues as $locale => $catalogue) { |
104
|
2 |
|
$domains = $catalogue->getDomains(); |
105
|
2 |
|
if (null !== $inputDomain) { |
106
|
|
|
$domains = [$inputDomain]; |
107
|
|
|
} |
108
|
2 |
|
foreach ($domains as $domain) { |
109
|
2 |
|
foreach ($catalogue->all($domain) as $key => $text) { |
110
|
2 |
|
$messages[] = $this->createMessage($catalogue, $locale, $domain, $key, $text); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$messages = \array_filter($messages, function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) { |
116
|
2 |
|
if (null !== $isNew && $m->isNew() !== $isNew) { |
117
|
1 |
|
return false; |
118
|
|
|
} |
119
|
2 |
|
if (null !== $isObsolete && $m->isObsolete() !== $isObsolete) { |
120
|
1 |
|
return false; |
121
|
|
|
} |
122
|
2 |
|
if (null !== $isApproved && $m->isApproved() !== $isApproved) { |
123
|
1 |
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return true; |
127
|
2 |
|
}); |
128
|
|
|
|
129
|
2 |
|
return $messages; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $domain |
134
|
|
|
* @param string $key |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getTranslations($domain, $key) |
139
|
|
|
{ |
140
|
|
|
$translations = []; |
141
|
|
|
foreach ($this->catalogues as $locale => $catalogue) { |
142
|
|
|
if ($catalogue->has($key, $domain)) { |
143
|
|
|
$translations[$locale] = $catalogue->get($key, $domain); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $translations; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $locale |
152
|
|
|
* @param $domain |
153
|
|
|
* @param $key |
154
|
|
|
* @param $text |
155
|
|
|
* |
156
|
|
|
* @return CatalogueMessage |
157
|
|
|
*/ |
158
|
3 |
|
private function createMessage(MessageCatalogueInterface $catalogue, $locale, $domain, $key, $text) |
159
|
|
|
{ |
160
|
3 |
|
$catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text); |
161
|
|
|
|
162
|
3 |
|
if ($catalogue instanceof MetadataAwareInterface) { |
163
|
3 |
|
$catalogueMessage->setMetadata(new Metadata($catalogue->getMetadata($key, $domain))); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
return $catalogueMessage; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|