-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathwait-for-translation-complete.php
More file actions
63 lines (48 loc) · 1.92 KB
/
wait-for-translation-complete.php
File metadata and controls
63 lines (48 loc) · 1.92 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
<?php
require_once '../../vendor/autoload.php';
$has_error = false;
$output = [
'status' => 'error',
'msg' => '',
'documentStatus' => null,
'downloadUrl' => null
];
try {
$dotenv = \Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']);
$dotenv->load();
$apiKey = $_ENV['DEEPL_API_KEY'];
if (!$apiKey) {
throw new \Exception('No API key specified. You must create a ".env" file in the root of the project with DEEPL_API_KEY="YOUR_DEEPL_API_KEY"');
}
if (!isset($_POST['document-handle'])) {
throw new \Exception('No document handle specified.');
}
$dh = json_decode($_POST['document-handle'], true);
if (!$dh) {
throw new \Exception('Invalid document handle');
}
$document_handle = new \DeepL\DocumentHandle($dh['documentId'], $dh['documentKey']);
try {
$output['status'] = 'success';
$deepl_translator = new \DeepL\Translator($apiKey);
$document_status = $deepl_translator->waitUntilDocumentTranslationComplete($document_handle);
$output['documentStatus'] = $document_status;
if ($document_status->status == 'done') {
$ext = pathinfo($_POST['filename'], PATHINFO_EXTENSION);
$name = pathinfo($_POST['filename'], PATHINFO_FILENAME);
$newFilename = $name. '-' . $_POST['target-language'] . '-translated.' . $ext;
if (file_exists('../uploads/' . $newFilename)) {
unlink('../uploads/'. $newFilename);
}
$deepl_translator->downloadDocument($document_handle, '../uploads/' . $newFilename);
$output['downloadUrl'] = 'uploads/'. $newFilename;
}
} catch (\Exception $e) {
$output['status'] = 'error';
$output['msg'] = $e->getMessage();
}
} catch (\Exception $e) {
$output['status'] = 'error';
$output['msg'] = $e->getMessage();
}
echo json_encode($output);