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\PlatformAdapter\Loco; |
13
|
|
|
|
14
|
|
|
use FAPI\Localise\Exception\Domain\AssetConflictException; |
15
|
|
|
use FAPI\Localise\Exception\Domain\NotFoundException; |
16
|
|
|
use FAPI\Localise\LocoClient; |
17
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
19
|
|
|
use Translation\Common\Exception\StorageException; |
20
|
|
|
use Translation\Common\Model\Message; |
21
|
|
|
use Translation\Common\Storage; |
22
|
|
|
use Translation\Common\TransferableStorage; |
23
|
|
|
use Translation\PlatformAdapter\Loco\Model\LocoProject; |
24
|
|
|
use Translation\SymfonyStorage\XliffConverter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Localize.biz. |
28
|
|
|
* |
29
|
|
|
* @author Tobias Nyholm <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Loco implements Storage, TransferableStorage |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var LocoClient |
35
|
|
|
*/ |
36
|
|
|
private $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LocoProject[] |
40
|
|
|
*/ |
41
|
|
|
private $projects = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param LocoClient $client |
45
|
|
|
* @param LocoProject[] $projects |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(LocoClient $client, array $projects) |
48
|
|
|
{ |
49
|
1 |
|
$this->client = $client; |
50
|
1 |
|
$this->projects = $projects; |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function get($locale, $domain, $key) |
57
|
|
|
{ |
58
|
|
|
$project = $this->getProject($domain); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$translation = $this->client->translations()->get($project->getApiKey(), $key, $locale)->getTranslation(); |
|
|
|
|
62
|
|
|
} catch (\FAPI\Localise\Exception $e) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
$meta = []; |
66
|
|
|
|
67
|
|
|
return new Message($key, $domain, $locale, $translation, $meta); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function create(Message $message) |
74
|
|
|
{ |
75
|
|
|
$project = $this->getProject($message->getDomain()); |
76
|
|
|
$isNewAsset = true; |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
// Create asset first |
80
|
|
|
$this->client->asset()->create($project->getApiKey(), $message->getKey()); |
81
|
|
|
} catch (AssetConflictException $e) { |
82
|
|
|
// This is okey |
83
|
|
|
$isNewAsset = false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$translation = $message->getTranslation(); |
87
|
|
|
|
88
|
|
|
// translation is the same as the key, so we will set it to empty string |
89
|
|
|
// as it was not translated and stats on loco will be unaffected |
90
|
|
|
if ($message->getKey() === $message->getTranslation()) { |
91
|
|
|
$translation = ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($isNewAsset) { |
95
|
|
|
$this->client->translations()->create( |
96
|
|
|
$project->getApiKey(), |
97
|
|
|
$message->getKey(), |
98
|
|
|
$message->getLocale(), |
99
|
|
|
$translation |
100
|
|
|
); |
101
|
|
|
} else { |
102
|
|
|
try { |
103
|
|
|
$this->client->translations()->get( |
104
|
|
|
$project->getApiKey(), |
105
|
|
|
$message->getKey(), |
106
|
|
|
$message->getLocale() |
107
|
|
|
); |
108
|
|
|
} catch (NotFoundException $e) { |
109
|
|
|
// Create only if not found. |
110
|
|
|
$this->client->translations()->create( |
111
|
|
|
$project->getApiKey(), |
112
|
|
|
$message->getKey(), |
113
|
|
|
$message->getLocale(), |
114
|
|
|
$translation |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->client->asset()->tag($project->getApiKey(), $message->getKey(), $message->getDomain()); |
120
|
|
|
|
121
|
|
|
if (!empty($message->getMeta('parameters'))) { |
122
|
|
|
// Pretty print the Meta field via YAML export |
123
|
|
|
$dump = Yaml::dump(['parameters' => $message->getMeta('parameters')], 4, 5); |
124
|
|
|
$dump = str_replace(" -\n", '', $dump); |
125
|
|
|
$dump = str_replace(' ', "\xC2\xA0", $dump); // no break space |
126
|
|
|
|
127
|
|
|
$this->client->asset()->patch( |
128
|
|
|
$project->getApiKey(), |
129
|
|
|
$message->getKey(), |
130
|
|
|
null, |
131
|
|
|
null, |
132
|
|
|
null, |
133
|
|
|
$dump |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function update(Message $message) |
142
|
|
|
{ |
143
|
|
|
$project = $this->getProject($message->getDomain()); |
144
|
|
|
|
145
|
|
|
try { |
146
|
|
|
$this->client->translations()->create($project->getApiKey(), $message->getKey(), $message->getLocale(), $message->getTranslation()); |
147
|
|
|
} catch (NotFoundException $e) { |
148
|
|
|
$this->create($message); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function delete($locale, $domain, $key) |
156
|
|
|
{ |
157
|
|
|
$project = $this->getProject($domain); |
158
|
|
|
|
159
|
|
|
$this->client->translations()->delete($project->getApiKey(), $key, $locale); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function export(MessageCatalogueInterface $catalogue) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$locale = $catalogue->getLocale(); |
168
|
|
|
foreach ($this->projects as $project) { |
169
|
|
|
foreach ($project->getDomains() as $domain) { |
170
|
|
|
try { |
171
|
|
|
$params = [ |
172
|
|
|
'format' => 'symfony', |
173
|
|
|
'status' => 'translated', |
174
|
|
|
'index' => $project->getIndexParameter(), |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
if ($project->isMultiDomain()) { |
178
|
|
|
$params['filter'] = $domain; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$data = $this->client->export()->locale($project->getApiKey(), $locale, 'xliff', $params); |
182
|
|
|
$catalogue->addCatalogue(XliffConverter::contentToCatalogue($data, $locale, $domain)); |
|
|
|
|
183
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function import(MessageCatalogueInterface $catalogue) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$locale = $catalogue->getLocale(); |
195
|
|
|
foreach ($this->projects as $project) { |
196
|
|
|
foreach ($project->getDomains() as $domain) { |
197
|
|
|
$data = XliffConverter::catalogueToContent($catalogue, $domain); |
|
|
|
|
198
|
|
|
$params = [ |
199
|
|
|
'locale' => $locale, |
200
|
|
|
'async' => 1, |
201
|
|
|
'index' => $project->getIndexParameter(), |
202
|
|
|
]; |
203
|
|
|
|
204
|
|
|
if ($project->isMultiDomain()) { |
205
|
|
|
$params['tag-all'] = $domain; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->client->import()->import($project->getApiKey(), 'xliff', $data, $params); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
private function getProject($domain): LocoProject |
214
|
|
|
{ |
215
|
|
|
foreach ($this->projects as $project) { |
216
|
|
|
if ($project->hasDomain($domain)) { |
217
|
|
|
return $project; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
throw new StorageException(sprintf('Project for "%s" domain was not found.', $domain)); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: