-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranslate.php
71 lines (56 loc) · 2.06 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
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
<?php
require('vendor/autoload.php');
require('workflows.php');
use ChrisKonnertz\DeepLy\DeepLy;
class Translate {
private $targetLanguage;
private $sourceLanguage;
function __construct($targetLanguage) {
$this->deepLy = new DeepLy();
$this->targetLanguage = $targetLanguage;
}
function getSourceLanguage() {
$tokens = explode(' ', $this->input);
if ($tokens[0]
&& strlen($tokens[0]) > 0
&& $this->deepLy->supportsLangCode(strtoupper($tokens[0]))
) {
$lang = strtoupper($tokens[0]);
array_shift($tokens);
$this->input = implode(' ', $tokens);
return strtoupper($lang);
}
return DeepLy::LANG_AUTO;
}
function getTranslations($input) {
$this->input = $input;
$w = new Workflows("xiphe.alfed.deepl");
try {
$sourceLanguage = $this->getSourceLanguage();
$translations = $this->deepLy->proposeTranslations($this->input, $this->targetLanguage, $sourceLanguage);
$bag = $this->deepLy->getTranslationBag();
$detectedSourceLanguage = $bag->getSourceLanguage();
$targetLanguage = $bag->getTargetLanguage();
$subtext = $this->input;
if ($targetLanguage !== $this->targetLanguage) {
throw new Exception('Failed to detect source language.');
}
if ($detectedSourceLanguage && strlen($detectedSourceLanguage) > 0) {
$subtext .= ' (' . $detectedSourceLanguage . ')';
}
foreach ($translations as $translation) {
$w->result(
time() . $translation,
$translation,
$translation,
$subtext,
strtolower($targetLanguage) . '.png',
"yes"
);
}
} catch (\Exception $exception) {
$w->result(time(), "", $exception->getMessage(), $input, "icon.png", "no");
}
return $w->toxml();
}
}