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\SymfonyStorage\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
|
1 |
|
public function __construct( |
47
|
|
|
PhraseAppClient $client, |
48
|
|
|
string $projectId, |
49
|
|
|
array $localeToIdMapping, |
50
|
|
|
array $domains, |
51
|
|
|
string $defaultLocale = null |
52
|
|
|
) { |
53
|
1 |
|
$this->client = $client; |
54
|
1 |
|
$this->projectId = $projectId; |
55
|
1 |
|
$this->localeToIdMapping = $localeToIdMapping; |
56
|
1 |
|
$this->domains = $domains; |
57
|
1 |
|
$this->defaultLocale = $defaultLocale; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function get($locale, $domain, $key) |
64
|
|
|
{ |
65
|
|
|
/* @var Index $index */ |
66
|
|
|
$index = $this->client->translation()->indexLocale($this->projectId, $this->getLocaleId($locale), [ |
67
|
|
|
'tags' => $domain |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
foreach ($index as $translation) { |
71
|
|
|
if ($translation->getKey()->getName() === $domain.'::'.$key) { |
72
|
|
|
return new Message($key, $domain, $locale, $translation->getContent(), []); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function create(Message $message) |
81
|
|
|
{ |
82
|
|
|
$localeId = $this->getLocaleId($message->getLocale()); |
83
|
|
|
|
84
|
|
|
/* @var KeySearchResults $result */ |
85
|
|
|
$result = $this->client->key()->search($this->projectId, [ |
86
|
|
|
'tags' => $message->getDomain(), |
87
|
|
|
'name' => $message->getDomain().'::'.$message->getKey(), |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
View Code Duplication |
foreach ($result as $key) { |
|
|
|
|
91
|
|
|
if ($key->getName() === $message->getDomain().'::'.$message->getKey()) { |
92
|
|
|
/* @var Index $index */ |
93
|
|
|
$index = $this->client->translation()->indexKey($this->projectId, $key->getId(), ['tags' => $message->getDomain()]); |
94
|
|
|
foreach ($index as $translation) { |
95
|
|
|
if ($translation->getLocale()->getId() === $localeId) { |
96
|
|
|
// Translation does already exist |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Create a translation with an existing key |
102
|
|
|
$this->client->translation()->create($this->projectId, $localeId, $key->getId(), $message->getTranslation()); |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
/* @var KeyCreated $keyCreated */ |
110
|
|
|
$keyCreated = $this->client->key()->create($this->projectId, $message->getDomain().'::'.$message->getKey(), [ |
111
|
|
|
'tags' => $message->getDomain(), |
112
|
|
|
]); |
113
|
|
|
} catch (UnprocessableEntityException $e) { |
|
|
|
|
114
|
|
|
// Translaton does already exist |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->client->translation()->create( |
119
|
|
|
$this->projectId, |
120
|
|
|
$this->getLocaleId($message->getLocale()), |
121
|
|
|
$keyCreated->getId(), |
122
|
|
|
$message->getTranslation() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function update(Message $message) |
130
|
|
|
{ |
131
|
|
|
$localeId = $this->getLocaleId($message->getLocale()); |
132
|
|
|
/* @var KeySearchResults $results */ |
133
|
|
|
$results = $this->client->key()->search($this->projectId, [ |
134
|
|
|
'tags' => $message->getDomain(), |
135
|
|
|
'name' => $message->getDomain().'::'.$message->getKey() |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
View Code Duplication |
foreach ($results as $searchResult) { |
|
|
|
|
139
|
|
|
if ($searchResult->getName() === $message->getDomain().'::'.$message->getKey()) { |
140
|
|
|
|
141
|
|
|
/* @var Index $translations */ |
142
|
|
|
$translations = $this->client->translation()->indexKey($this->projectId, $searchResult->getId(), [ |
143
|
|
|
'tags' => $message->getDomain(), |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
foreach ($translations as $translation) { |
147
|
|
|
if ($translation->getLocale()->getId() === $localeId) { |
148
|
|
|
$this->client->translation()->update( |
149
|
|
|
$this->projectId, |
150
|
|
|
$translation->getId(), |
151
|
|
|
$message->getTranslation() |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// No translation was found, lets create one. |
161
|
|
|
$this->create($message); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function delete($locale, $domain, $key) |
168
|
|
|
{ |
169
|
|
|
/* @var KeySearchResults $results */ |
170
|
|
|
$results = $this->client->key()->search($this->projectId, $this->getLocaleId($locale), [ |
|
|
|
|
171
|
|
|
'tags' => $domain, |
172
|
|
|
'name' => $domain.'::'.$key |
173
|
|
|
]); |
174
|
|
|
|
175
|
|
|
foreach ($results as $searchResult) { |
176
|
|
|
if ($searchResult->getName() === $domain.'::'.$key) { |
177
|
|
|
$this->client->key()->delete($this->projectId, $searchResult->getId()); |
178
|
|
|
break; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function export(MessageCatalogueInterface $catalogue) |
187
|
|
|
{ |
188
|
|
|
$locale = $catalogue->getLocale(); |
189
|
|
|
$localeId = $this->getLocaleId($locale); |
190
|
|
|
|
191
|
|
|
foreach ($this->domains as $domain) { |
192
|
|
|
try { |
193
|
|
|
$response = $this->client->locale()->download($this->projectId, $localeId, 'symfony_xliff', [ |
194
|
|
|
'tag' => $domain |
195
|
|
|
]); |
196
|
|
|
} catch (\Throwable $e) { |
197
|
|
|
throw new StorageException($e->getMessage()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
try { |
201
|
|
|
$newCatalogue = XliffConverter::contentToCatalogue($response, $locale, $domain); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$messages = []; |
204
|
|
|
|
205
|
|
|
foreach ($newCatalogue->all($domain) as $message => $translation) { |
206
|
|
|
$messages[substr($message, strlen($domain) + 2)] = $translation; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$newCatalogue->replace($messages, $domain); |
210
|
|
|
|
211
|
|
|
$catalogue->addCatalogue($newCatalogue); |
212
|
|
|
} catch (\Throwable $e) { |
213
|
|
|
// ignore empty translation files |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $catalogue; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function import(MessageCatalogueInterface $catalogue) |
224
|
|
|
{ |
225
|
|
|
$locale = $catalogue->getLocale(); |
226
|
|
|
$localeId = $this->getLocaleId($locale); |
227
|
|
|
|
228
|
|
|
foreach ($this->domains as $domain) { |
229
|
|
|
$messages = []; |
230
|
|
|
|
231
|
|
|
foreach ($catalogue->all($domain) as $message => $translation) { |
232
|
|
|
$messages[$domain . '::' . $message] = $translation; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$catalogue->replace($messages, $domain); |
236
|
|
|
|
237
|
|
|
if ($this->defaultLocale) { |
|
|
|
|
238
|
|
|
$options = ['default_locale' => $this->defaultLocale]; |
239
|
|
|
} else { |
240
|
|
|
$options = []; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$data = XliffConverter::catalogueToContent($catalogue, $domain, $options); |
|
|
|
|
244
|
|
|
|
245
|
|
|
$file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf'; |
246
|
|
|
|
247
|
|
|
try { |
248
|
|
|
file_put_contents($file, $data); |
249
|
|
|
|
250
|
|
|
$this->client->upload()->upload($this->projectId, 'symfony_xliff', $file, [ |
251
|
|
|
'locale_id' => $localeId, |
252
|
|
|
'tags' => $domain, |
253
|
|
|
]); |
254
|
|
|
} catch (\Throwable $e) { |
255
|
|
|
throw new StorageException($e->getMessage()); |
256
|
|
|
} finally { |
257
|
|
|
unlink($file); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $locale |
264
|
|
|
* |
265
|
|
|
* @return string |
266
|
|
|
* |
267
|
|
|
* @throws StorageException If no id was found for locale. |
268
|
|
|
*/ |
269
|
|
|
private function getLocaleId(string $locale): string |
270
|
|
|
{ |
271
|
|
|
if (isset($this->localeToIdMapping[$locale])) { |
272
|
|
|
return $this->localeToIdMapping[$locale]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale)); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.