This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.php
46 lines (41 loc) · 1.51 KB
/
translate.php
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
<?php
$fileToTranslate = json_decode(file_get_contents(__DIR__.'/fileToTranslate.json'), true);
$langToTranslate = "RU";
$deepl_apiKey = '4e1e7179-4363-60ab-6c0e-343c617374ae:fx';
$curl = curl_init();
$translation = [];
$translationCount = count($fileToTranslate);
echo PHP_EOL.'START SIMPLE DEEPL TRANSLATION BY THESYSTEMS'.PHP_EOL;
$i = 0;
foreach ($fileToTranslate as $key => $value) {
$i++;
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-free.deepl.com/v2/translate?auth_key='.$deepl_apiKey.'&text='.urlencode($value).'&target_lang=' . $langToTranslate,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST'
]);
$response = curl_exec($curl);
curl_close($curl);
$response = curl_exec($curl);
if ($response === false) {
$translation[$key] = $value;
echo $i."/".$translationCount. " (Error Request)".PHP_EOL;
continue;
}
$array = json_decode($response, true);
if (is_null($array) or $array === false) {
$translation[$key] = $value;
echo $i."/".$translationCount. " (Error JSON)".PHP_EOL;
continue;
}
$translation[$key] = $array['translations'][0]['text'];
echo $i."/".$translationCount. " - ".round(($i/$translationCount*100), 1)."% (Done)".PHP_EOL;
}
echo PHP_EOL."DONE".PHP_EOL;
unlink(__DIR__.'/translatesFile.json');
$file = fopen(__DIR__.'/translatesFile.json', "w");
fwrite($file, json_encode($translation, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
fclose($file);