Repository navigation
Expand file tree
/
Copy pathscript.php
More file actions
253 lines (208 loc) · 7.69 KB
/
Copy pathscript.php
File metadata and controls
253 lines (208 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$config = json_decode(file_get_contents("config.json"), true);
$username = $config['traduora']['username'];
$password = $config['traduora']['password'];
$projects = $config['traduora']['projects'];
$url = $config['traduora']['url'];
$deeplApiKey = $config['deepl']['apiKey'];
$sourceLangDeepl = $config['sourceLang']['deepl'];
$sourceLang = $config['sourceLang']['traduora'];
sendMessage("");
sendMessage("Traduora-Deepl-Translator 1.0");
sendMessage("");
$managedLang = $config['langs'];
define("url", $url);
define("deepl", $deeplApiKey);
define("sourceLangDeepl", $sourceLangDeepl);
define("sourceLang", $sourceLang);
$postField = ["grant_type" => "password", "username" => $username, "password" => $password];
$ch = curl_init();
curl_setopt_array($ch, [CURLOPT_URL => url . "/api/v1/auth/token", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode($postField), CURLOPT_HTTPHEADER => ["content-type: application/json"]]);
$auth = curl_exec($ch);
$response = json_decode($auth, true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(is_null($response)) throw new Exception("auth request failed (null)");
if (isset($auth['error'])) throw new Exception("auth request failed");
define("accessToken", $response['access_token']);
function traduoraRequest($path, $method, $data = [], $timeout = 10): array
{
$ch = curl_init();
curl_setopt_array($ch, [CURLOPT_URL => url . "/api/v1/" . $path, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_FOLLOWLOCATION => true, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => ["Authorization: Bearer " . accessToken, "content-type: application/json"]]);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
if ($response === false) throw new Exception("traduora request failed");
if (isset($response['error'])) {
throw new Exception("traduora request failed: " . $response['error']['message'] . " (" . $response['error']['code'] . ")");
}
if(is_null($response)) throw new Exception("traduora request failed: " . $response."(null)");
return $response;
}
function deeplRequest($sourceLang, $outputLang, $message): string
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-free.deepl.com/v2/translate',
CURLOPT_POSTFIELDS => [
'auth_key' => deepl,
'text' => $message,
'target_lang' => $outputLang,
'source_lang' => $sourceLang,
'ignore_tags' => 'keep',
'preserve_formatting' => true,
'tag_handling' => 'xml'
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST'
]);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
if ($response === false) {
throw new Exception("deepl request failed, ".$info["http_code"]);
}
$array = json_decode($response, true);
if (is_null($array) or $array === false) {
throw new Exception("deepl request failed, ".$info["http_code"]);
}
return $array['translations'][0]['text'] ?? throw new Exception("deepl request failed, ".$info["http_code"]);
}
/**
* @throws Exception
*/
function getTerms($project, $lang): array
{
$terms = [];
$r = traduoraRequest("projects/" . $project . "/translations/" . $lang, "GET");
foreach ($r['data'] as $term) {
$terms[$term['termId']] = $term['value'];
}
return $terms;
}
/**
* @throws Exception
*/
function getLangs($project): array
{
$r = traduoraRequest("projects/" . $project . "/translations/", "GET");
$langs = [];
foreach ($r['data'] as $lang) {
$langs[$lang['locale']['code']] = $lang['locale']['language'];
}
return $langs;
}
foreach ($projects as $project => $name) {
sendMessage("get project ".$name);
try {
sendMessage("get terms in ".sourceLang." in ".$name);
$terms = getTerms($project, sourceLang);
} catch (Exception $e) {
sendMessage("error in get Projects terms in ".$name.": ".$e->getMessage());
continue;
}
try {
sendMessage("get langs in ".$name);
$langs = getLangs($project);
} catch (Exception $e) {
sendMessage("error in get Projects langs in ".$name.": ".$e->getMessage());
continue;
}
foreach ($managedLang as $lang => $deeplLang) {
try {
if (!isset($langs[$lang])) {
sendMessage("create lang ".$lang." in ".$name);
traduoraRequest("projects/" . $project . "/translations", "POST", ["code" => $lang], 100);
sleep(3);
}
sendMessage("get terms in ".$lang." in ".$name);
$termsLang = getTerms($project, $lang);
$countTerms = count($terms);
$currentTerm = 0;
foreach ($terms as $key => $value) {
$currentTerm++;
if (empty($termsLang[$key]) and !empty($value)) {
usleep(250000);
try {
sendMessage("translate " . $key . " in " . $lang . " in " . $name." (".$currentTerm."/".$countTerms.")");
$termsLang[$key] = removeMessagePlaceholder(deeplRequest(sourceLangDeepl, $deeplLang, addMessagePlaceholder($value)));
traduoraRequest("projects/" . $project . "/translations/" . $lang, "PATCH", ["termId" => $key, "value" => $termsLang[$key]]);
} catch (Exception $e) {
sendMessage("error in lang " . $lang . " for term " . $key . " with value " . $value." error: ".$e->getMessage(). " (".$currentTerm."/".$countTerms.")");
}
} else {
if(!hasMessagePlaceholder($termsLang[$key],$value)){
sendMessage("patch translation (no placeholder found) " . $key . " in " . $lang . " in " . $name." (".$currentTerm."/".$countTerms.")");
echo "old:".$termsLang[$key].PHP_EOL;
$termsLang[$key] = removeMessagePlaceholder(deeplRequest(sourceLangDeepl, $deeplLang, addMessagePlaceholder($value)));
echo "new:".$termsLang[$key].PHP_EOL;
traduoraRequest("projects/" . $project . "/translations/" . $lang, "PATCH", ["termId" => $key, "value" => $termsLang[$key]]);
} else {
sendMessage("skip " . $key . " in " . $lang . " in " . $name . " (" . $currentTerm . "/" . $countTerms . ")");
}
}
}
} catch (Exception $e) {
sendMessage("error in lang " . $lang . ":" . $e->getMessage());
}
}
}
function sendMessage(string $message): void
{
echo "[".date("d.m.Y H:i:s")."] ".$message.PHP_EOL;
}
function hasMessagePlaceholder(string $message, string $original): bool
{
$allPlaceholders = [];
$split = explode(" ", $original);
foreach ($split as $word){
if (str_starts_with($word, "@")){
$allPlaceholders[] = $word;
}
}
if(count($allPlaceholders) === 0){
return true;
}
foreach ($allPlaceholders as $allPlaceholder) {
if (!str_contains($message, $allPlaceholder)) {
return false;
}
}
return true;
}
function addMessagePlaceholder(string $message): string
{
$split = explode(" ", $message);
$newMessage = "";
foreach ($split as $word){
if (str_starts_with($word, "@")){
$newMessage .= " <keep>".$word."</keep>";
} else {
$newMessage .= " ".$word;
}
}
return removeFirstEmptySpace($newMessage);
}
function removeFirstEmptySpace(string $message): string
{
$new = $message;
if (str_starts_with($message, " ")){
$new = substr($message, 1);
}
if (str_starts_with($new, " ")){
return removeFirstEmptySpace($new);
}
return $new;
}
function removeMessagePlaceholder(string $message): string
{
$message = str_replace("</keep>", "", str_replace("<keep>", "", $message));
return removeFirstEmptySpace($message);
}