-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDeepLTranslatorEscaped.php
More file actions
54 lines (45 loc) · 1.29 KB
/
DeepLTranslatorEscaped.php
File metadata and controls
54 lines (45 loc) · 1.29 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
<?php
namespace potrans\translator;
use DeepL\DeepLException;
use Gettext\Translation;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* There are two global variable $input and $output
*
* Usage:
*
* ./bin/potrans deepl ./tests/example-cs_CZ.po ~/Downloads --apikey=123456 --translator=src/translator/DeepLTranslatorEscaped.php --no-cache -vvv
*
* @var InputInterface $input
* @var OutputInterface $output
* @see \potrans\commands\DeepLTranslatorCommand for more information
*/
class DeepLTranslatorEscaped extends TranslatorAbstract {
private \DeepL\Translator $translator;
public function __construct(\DeepL\Translator $translator) {
$this->translator = $translator;
}
/**
* @throws DeepLException
*/
public function getTranslation(Translation $sentence): string {
$response = $this->translator->translateText(
preg_replace('/\$([^$]+)\$/i', '<keep>$1</keep>', $sentence->getOriginal()),
$this->from,
$this->to,
[
'tag_handling' => 'xml',
'ignore_tags' => 'keep',
]
);
return preg_replace('/<\/?keep>/i', '$', $response->text);
}
}
$apikey = (string) $input->getOption('apikey');
/**
* Return custom translator instance
*/
return new DeepLTranslatorEscaped(
new \DeepL\Translator($apikey),
);