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\Loader\ArrayLoader; |
18
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
19
|
|
|
use Translation\Common\Exception\StorageException; |
20
|
|
|
use Translation\Common\Model\Message; |
21
|
|
|
use Translation\Common\Storage; |
22
|
|
|
use Translation\Common\TransferableStorage; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Localize.biz. |
26
|
|
|
* |
27
|
|
|
* @author Tobias Nyholm <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Loco implements Storage, TransferableStorage |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var LocoClient |
33
|
|
|
*/ |
34
|
|
|
private $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $domainToProjectId = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param LocoClient $client |
43
|
|
|
* @param array $domainToProjectId |
44
|
|
|
*/ |
45
|
|
|
public function __construct(LocoClient $client, array $domainToProjectId) |
46
|
|
|
{ |
47
|
|
|
$this->client = $client; |
48
|
|
|
$this->domainToProjectId = $domainToProjectId; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function get($locale, $domain, $key) |
55
|
|
|
{ |
56
|
|
|
$projectKey = $this->getApiKey($domain); |
57
|
|
|
$translation = $this->client->translations()->get($projectKey, $key, $locale)->getTranslation(); |
|
|
|
|
58
|
|
|
$meta = []; |
59
|
|
|
|
60
|
|
|
return new Message($key, $domain, $locale, $translation, $meta); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function create(Message $message) |
67
|
|
|
{ |
68
|
|
|
$projectKey = $this->getApiKey($message->getDomain()); |
69
|
|
|
$isNewAsset = true; |
70
|
|
|
try { |
71
|
|
|
// Create asset first |
72
|
|
|
$this->client->asset()->create($projectKey, $message->getKey()); |
73
|
|
|
$this->client->translations()->create($projectKey, $message->getKey(), $message->getLocale(), $message->getTranslation()); |
74
|
|
|
} catch (AssetConflictException $e) { |
75
|
|
|
// This is okey |
76
|
|
|
$isNewAsset = false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($isNewAsset) { |
80
|
|
|
$this->client->translations()->create( |
81
|
|
|
$projectKey, |
82
|
|
|
$message->getKey(), |
83
|
|
|
$message->getLocale(), |
84
|
|
|
$message->getTranslation() |
85
|
|
|
); |
86
|
|
|
} else { |
87
|
|
|
try { |
88
|
|
|
$this->client->translations()->get( |
89
|
|
|
$projectKey, |
90
|
|
|
$message->getKey(), |
91
|
|
|
$message->getLocale() |
92
|
|
|
); |
93
|
|
|
} catch (NotFoundException $e) { |
94
|
|
|
// Create only if not found. |
95
|
|
|
$this->client->translations()->create( |
96
|
|
|
$projectKey, |
97
|
|
|
$message->getKey(), |
98
|
|
|
$message->getLocale(), |
99
|
|
|
$message->getTranslation() |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function update(Message $message) |
109
|
|
|
{ |
110
|
|
|
$projectKey = $this->getApiKey($message->getDomain()); |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$this->client->translations()->create($projectKey, $message->getKey(), $message->getLocale(), $message->getTranslation()); |
114
|
|
|
} catch (NotFoundException $e) { |
115
|
|
|
$this->create($message); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function delete($locale, $domain, $key) |
123
|
|
|
{ |
124
|
|
|
$projectKey = $this->getApiKey($domain); |
125
|
|
|
$this->client->translations()->delete($projectKey, $key, $locale); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function export(MessageCatalogueInterface $catalogue) |
132
|
|
|
{ |
133
|
|
|
$locale = $catalogue->getLocale(); |
134
|
|
|
$loader = new ArrayLoader(); |
135
|
|
|
foreach ($this->domainToProjectId as $domain => $projectKey) { |
136
|
|
|
try { |
137
|
|
|
$data = $this->client->export()->locale( |
|
|
|
|
138
|
|
|
$projectKey, |
139
|
|
|
$locale, |
140
|
|
|
'json', |
141
|
|
|
['format' => 'symfony'] |
142
|
|
|
); |
143
|
|
|
$array = json_decode($data, true); |
144
|
|
|
$catalogue->addCatalogue( |
145
|
|
|
$loader->load($array, $locale, $domain) |
146
|
|
|
); |
147
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function import(MessageCatalogueInterface $catalogue) |
156
|
|
|
{ |
157
|
|
|
$locale = $catalogue->getLocale(); |
158
|
|
|
foreach ($this->domainToProjectId as $domain => $projectKey) { |
159
|
|
|
$data = json_encode($catalogue->all($domain)); |
160
|
|
|
$this->client->import()->import($projectKey, 'json', $data, ['locale' => $locale, 'async' => 1]); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $domain |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
private function getApiKey($domain) |
170
|
|
|
{ |
171
|
|
|
if (isset($this->domainToProjectId[$domain])) { |
172
|
|
|
return $this->domainToProjectId[$domain]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
throw new StorageException(sprintf('Api key for domain "%s" has not been configured.', $domain)); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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: