1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Translation\PlatformAdapter\PhraseApp; |
4
|
|
|
|
5
|
|
|
use FAPI\PhraseApp\Model\Key\KeyCreated; |
6
|
|
|
use FAPI\PhraseApp\Model\Key\KeySearchResults; |
7
|
|
|
use FAPI\PhraseApp\Model\Translation\Index; |
8
|
|
|
use FAPI\PhraseApp\PhraseAppClient; |
9
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
10
|
|
|
use Translation\Common\Exception\StorageException; |
11
|
|
|
use Translation\Common\Model\Message; |
12
|
|
|
use Translation\Common\Storage; |
13
|
|
|
use Translation\Common\TransferableStorage; |
14
|
|
|
use Translation\PlatformAdapter\PhraseApp\Bridge\Symfony\XliffConverter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PhraseApp implements Storage, TransferableStorage |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var PhraseAppClient |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $projectId; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $localeToIdMapping; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $domains; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private $defaultLocale; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
PhraseAppClient $client, |
48
|
|
|
string $projectId, |
49
|
|
|
array $localeToIdMapping, |
50
|
|
|
array $domains, |
51
|
|
|
string $defaultLocale = null |
52
|
|
|
) { |
53
|
|
|
$this->client = $client; |
54
|
|
|
$this->projectId = $projectId; |
55
|
|
|
$this->localeToIdMapping = $localeToIdMapping; |
56
|
|
|
$this->domains = $domains; |
57
|
|
|
$this->defaultLocale = $defaultLocale; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function get($locale, $domain, $key) |
61
|
|
|
{ |
62
|
|
|
/* @var Index $index */ |
63
|
|
|
$index = $this->client->translation()->indexLocale($this->projectId, $this->getLocaleId($locale), [ |
64
|
|
|
'tags' => $domain |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
foreach ($index->getTranslations() as $translation) { |
|
|
|
|
68
|
|
|
if ($translation->getKey()->getName() === $domain.'::'.$key) { |
69
|
|
|
return new Message($key, $domain, $locale, $translation->getContent(), []); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function create(Message $message) |
75
|
|
|
{ |
76
|
|
|
$localeId = $this->getLocaleId($message->getLocale()); |
77
|
|
|
|
78
|
|
|
/* @var KeySearchResults $result */ |
79
|
|
|
$result = $this->client->key()->search($this->projectId, [ |
80
|
|
|
'tags' => $message->getDomain(), |
81
|
|
|
'name' => $message->getDomain().'::'.$message->getKey(), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
foreach ($result as $key) { |
85
|
|
|
if ($key->getName() === $message->getDomain().'::'.$message->getKey()) { |
86
|
|
|
/* @var Index $index */ |
87
|
|
|
$index = $this->client->translation()->indexKey($this->projectId, $key->getId(), ['tags' => $message->getDomain()]); |
88
|
|
|
|
89
|
|
|
foreach ($index as $translation) { |
90
|
|
|
if ($translation->getLocale()->getId() === $localeId |
91
|
|
|
&& $translation->getContent() !== $message->getTranslation() |
92
|
|
|
) { |
93
|
|
|
$this->client->translation()->update($this->projectId, $translation->getId(), $message->getTranslation()); |
94
|
|
|
|
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->client->translation()->create($this->projectId, $localeId, $key->getId(), $message->getTranslation()); |
100
|
|
|
|
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/* @var KeyCreated $keyCreated */ |
106
|
|
|
$keyCreated = $this->client->key()->create($this->projectId, $message->getDomain().'::'.$message->getKey(), [ |
107
|
|
|
'tags' => $message->getDomain(), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$this->client->translation()->create( |
111
|
|
|
$this->projectId, |
112
|
|
|
$this->getLocaleId($message->getLocale()), |
113
|
|
|
$keyCreated->getId(), |
114
|
|
|
$message->getTranslation() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function update(Message $message) |
119
|
|
|
{ |
120
|
|
|
$localeId = $this->getLocaleId($message->getLocale()); |
121
|
|
|
/* @var KeySearchResults $results */ |
122
|
|
|
$results = $this->client->key()->search($this->projectId, [ |
123
|
|
|
'tags' => $message->getDomain(), |
124
|
|
|
'name' => $message->getDomain().'::'.$message->getKey() |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
foreach ($results as $searchResult) { |
128
|
|
|
if ($searchResult->getName() === $message->getDomain().'::'.$message->getKey()) { |
129
|
|
|
|
130
|
|
|
/* @var Index $translations */ |
131
|
|
|
$translations = $this->client->translation()->indexKey($this->projectId, $searchResult->getId(), [ |
132
|
|
|
'tags' => $message->getDomain(), |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
foreach ($translations as $translation) { |
136
|
|
|
if ($translation->getLocale()->getId() === $localeId) { |
137
|
|
|
$this->client->translation()->update( |
138
|
|
|
$this->projectId, |
139
|
|
|
$translation->getId(), |
140
|
|
|
$message->getTranslation() |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function delete($locale, $domain, $key) |
151
|
|
|
{ |
152
|
|
|
/* @var KeySearchResults $results */ |
153
|
|
|
$results = $this->client->key()->search($this->projectId, $this->getLocaleId($locale), [ |
|
|
|
|
154
|
|
|
'tags' => $domain, |
155
|
|
|
'name' => $domain.'::'.$key |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
foreach ($results->getSearchResults() as $searchResult) { |
|
|
|
|
159
|
|
|
if ($searchResult->getName() === $domain.'::'.$key) { |
160
|
|
|
$this->client->key()->delete($this->projectId, $searchResult->getId()); |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function export(MessageCatalogueInterface $catalogue) |
170
|
|
|
{ |
171
|
|
|
$locale = $catalogue->getLocale(); |
172
|
|
|
$localeId = $this->getLocaleId($locale); |
173
|
|
|
|
174
|
|
|
foreach ($this->domains as $domain) { |
175
|
|
|
try { |
176
|
|
|
$response = $this->client->locale()->download($this->projectId, $localeId, 'symfony_xliff', [ |
177
|
|
|
'tag' => $domain |
178
|
|
|
]); |
179
|
|
|
} catch (\Throwable $e) { |
180
|
|
|
throw new StorageException($e->getMessage()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
try { |
184
|
|
|
$newCatalogue = XliffConverter::contentToCatalogue($response, $locale, $domain); |
|
|
|
|
185
|
|
|
|
186
|
|
|
$messages = []; |
187
|
|
|
|
188
|
|
|
foreach ($newCatalogue->all($domain) as $message => $translation) { |
189
|
|
|
$messages[substr($message, strlen($domain) + 2)] = $translation; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$newCatalogue->replace($messages, $domain); |
193
|
|
|
|
194
|
|
|
$catalogue->addCatalogue($newCatalogue); |
195
|
|
|
} catch (\Throwable $e) { |
196
|
|
|
// ignore empty translation files |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $catalogue; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
|
|
public function import(MessageCatalogueInterface $catalogue) |
207
|
|
|
{ |
208
|
|
|
$locale = $catalogue->getLocale(); |
209
|
|
|
$localeId = $this->getLocaleId($locale); |
210
|
|
|
|
211
|
|
|
foreach ($this->domains as $domain) { |
212
|
|
|
$messages = []; |
213
|
|
|
|
214
|
|
|
foreach ($catalogue->all($domain) as $message => $translation) { |
215
|
|
|
$messages[$domain . '::' . $message] = $translation; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$catalogue->replace($messages, $domain); |
219
|
|
|
|
220
|
|
|
if ($this->defaultLocale) { |
|
|
|
|
221
|
|
|
$options = ['default_locale' => $this->defaultLocale]; |
222
|
|
|
} else { |
223
|
|
|
$options = []; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$data = XliffConverter::catalogueToContent($catalogue, $domain, $options); |
|
|
|
|
227
|
|
|
|
228
|
|
|
$file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf'; |
229
|
|
|
|
230
|
|
|
try { |
231
|
|
|
file_put_contents($file, $data); |
232
|
|
|
|
233
|
|
|
$this->client->upload()->upload($this->projectId, 'symfony_xliff', $file, [ |
234
|
|
|
'locale_id' => $localeId, |
235
|
|
|
'tags' => $domain, |
236
|
|
|
]); |
237
|
|
|
} catch (\Throwable $e) { |
238
|
|
|
throw new StorageException($e->getMessage()); |
239
|
|
|
} finally { |
240
|
|
|
unlink($file); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
private function getLocaleId(string $locale): string |
246
|
|
|
{ |
247
|
|
|
if (isset($this->localeToIdMapping[$locale])) { |
248
|
|
|
return $this->localeToIdMapping[$locale]; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale)); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.