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
|
|
|
/* @var KeyCreated $keyCreated */ |
77
|
|
|
$keyCreated = $this->client->key()->create($this->projectId, $message->getDomain().'::'.$message->getKey(), [ |
78
|
|
|
'tags' => $message->getDomain(), |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$this->client->translation()->create( |
82
|
|
|
$this->projectId, |
83
|
|
|
$this->getLocaleId($message->getLocale()), |
84
|
|
|
$keyCreated->getId(), |
85
|
|
|
$message->getTranslation() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function update(Message $message) |
90
|
|
|
{ |
91
|
|
|
/* @var Index $index */ |
92
|
|
|
$index = $this->client->translation()->indexLocale($this->projectId, $this->getLocaleId($message->getLocale()), [ |
93
|
|
|
'tags' => $message->getDomain() |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
foreach ($index->getTranslations() as $translation) { |
97
|
|
|
if ($translation->getKey() === $message->getDomain().'::'.$message->getKey()) { |
98
|
|
|
$this->client->translation()->update($this->projectId, $translation->getId(), $message->getTranslation()); |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function delete($locale, $domain, $key) |
105
|
|
|
{ |
106
|
|
|
/* @var KeySearchResults $results */ |
107
|
|
|
$results = $this->client->key()->search($this->projectId, $this->getLocaleId($locale), [ |
108
|
|
|
'tags' => $domain, |
109
|
|
|
'name' => $domain.'::'.$key |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
foreach ($results->getSearchResults() as $searchResult) { |
113
|
|
|
if ($searchResult->getName() === $domain.'::'.$key) { |
114
|
|
|
$this->client->key()->delete($this->projectId, $searchResult->getId()); |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function export(MessageCatalogueInterface $catalogue) |
124
|
|
|
{ |
125
|
|
|
$locale = $catalogue->getLocale(); |
126
|
|
|
$localeId = $this->getLocaleId($locale); |
127
|
|
|
|
128
|
|
|
foreach ($this->domains as $domain) { |
129
|
|
|
try { |
130
|
|
|
$response = $this->client->locale()->download($this->projectId, $localeId, 'symfony_xliff', [ |
131
|
|
|
'tag' => $domain |
132
|
|
|
]); |
133
|
|
|
} catch (\Throwable $e) { |
134
|
|
|
throw new StorageException($e->getMessage()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
$newCatalogue = XliffConverter::contentToCatalogue($response, $locale, $domain); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$messages = []; |
141
|
|
|
|
142
|
|
|
foreach ($newCatalogue->all($domain) as $message => $translation) { |
143
|
|
|
$messages[substr($message, strlen($domain) + 2)] = $translation; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$newCatalogue->replace($messages, $domain); |
147
|
|
|
|
148
|
|
|
$catalogue->addCatalogue($newCatalogue); |
149
|
|
|
} catch (\Throwable $e) { |
150
|
|
|
// ignore empty translation files |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $catalogue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function import(MessageCatalogueInterface $catalogue) |
161
|
|
|
{ |
162
|
|
|
$locale = $catalogue->getLocale(); |
163
|
|
|
$localeId = $this->getLocaleId($locale); |
164
|
|
|
|
165
|
|
|
foreach ($this->domains as $domain) { |
166
|
|
|
$messages = []; |
167
|
|
|
|
168
|
|
|
foreach ($catalogue->all($domain) as $message => $translation) { |
169
|
|
|
$messages[$domain . '::' . $message] = $translation; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$catalogue->replace($messages, $domain); |
173
|
|
|
|
174
|
|
|
if ($this->defaultLocale) { |
|
|
|
|
175
|
|
|
$options = ['default_locale' => $this->defaultLocale]; |
176
|
|
|
} else { |
177
|
|
|
$options = []; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$data = XliffConverter::catalogueToContent($catalogue, $domain, $options); |
|
|
|
|
181
|
|
|
|
182
|
|
|
$file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf'; |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
file_put_contents($file, $data); |
186
|
|
|
|
187
|
|
|
$this->client->upload()->upload($this->projectId, 'symfony_xliff', $file, [ |
188
|
|
|
'locale_id' => $localeId, |
189
|
|
|
'tags' => $domain, |
190
|
|
|
]); |
191
|
|
|
} catch (\Throwable $e) { |
192
|
|
|
throw new StorageException($e->getMessage()); |
193
|
|
|
} finally { |
194
|
|
|
unlink($file); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function getLocaleId(string $locale): string |
200
|
|
|
{ |
201
|
|
|
if (isset($this->localeToIdMapping[$locale])) { |
202
|
|
|
return $this->localeToIdMapping[$locale]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.