|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BabyMarkt\DeepL; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* DeepL API client library |
|
9
|
|
|
* |
|
10
|
|
|
* @package BabyMarkt\DeepL |
|
11
|
|
|
*/ |
|
12
|
|
|
class DeepL |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* API URL: usage |
|
16
|
|
|
*/ |
|
17
|
|
|
const API_URL_RESOURCE_USAGE = 'usage'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* API URL: languages |
|
21
|
|
|
*/ |
|
22
|
|
|
const API_URL_RESOURCE_LANGUAGES = 'languages'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ClientInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $client; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com', ClientInterface $client = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->client = $client ?? new Client($authKey, $apiVersion, $host); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Call languages-Endpoint and return Json-response as an Array |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $type |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
* @throws DeepLException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function languages($type = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_LANGUAGES); |
|
45
|
|
|
$body = $this->client->buildQuery(array('type' => $type)); |
|
46
|
|
|
$languages = $this->client->request($url, $body); |
|
47
|
|
|
|
|
48
|
|
|
return $languages; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Translate the text string or array from source to destination language |
|
53
|
|
|
* For detailed info on Parameters see README.md |
|
54
|
|
|
* |
|
55
|
|
|
* @param string|string[] $text |
|
56
|
|
|
* @param string $sourceLang |
|
57
|
|
|
* @param string $targetLang |
|
58
|
|
|
* @param string $tagHandling |
|
59
|
|
|
* @param array|null $ignoreTags |
|
60
|
|
|
* @param string $formality |
|
61
|
|
|
* @param null $splitSentences |
|
|
|
|
|
|
62
|
|
|
* @param null $preserveFormatting |
|
|
|
|
|
|
63
|
|
|
* @param array|null $nonSplittingTags |
|
64
|
|
|
* @param null $outlineDetection |
|
|
|
|
|
|
65
|
|
|
* @param array|null $splittingTags |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
* @throws DeepLException |
|
69
|
|
|
* |
|
70
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList) |
|
71
|
|
|
*/ |
|
72
|
|
|
public function translate( |
|
73
|
|
|
$text, |
|
74
|
|
|
$sourceLang = 'de', |
|
75
|
|
|
$targetLang = 'en', |
|
76
|
|
|
$tagHandling = null, |
|
77
|
|
|
array $ignoreTags = null, |
|
78
|
|
|
$formality = 'default', |
|
79
|
|
|
$splitSentences = null, |
|
80
|
|
|
$preserveFormatting = null, |
|
81
|
|
|
array $nonSplittingTags = null, |
|
82
|
|
|
$outlineDetection = null, |
|
83
|
|
|
array $splittingTags = null |
|
84
|
|
|
) { |
|
85
|
31 |
|
if (is_array($tagHandling)) { |
|
|
|
|
|
|
86
|
|
|
throw new InvalidArgumentException('$tagHandling must be of type String in V2 of DeepLLibrary'); |
|
87
|
31 |
|
} |
|
88
|
31 |
|
$paramsArray = array( |
|
89
|
31 |
|
'text' => $text, |
|
90
|
31 |
|
'source_lang' => $sourceLang, |
|
91
|
|
|
'target_lang' => $targetLang, |
|
92
|
31 |
|
'splitting_tags' => $splittingTags, |
|
93
|
31 |
|
'non_splitting_tags' => $nonSplittingTags, |
|
94
|
|
|
'ignore_tags' => $ignoreTags, |
|
95
|
|
|
'tag_handling' => $tagHandling, |
|
96
|
|
|
'formality' => $formality, |
|
97
|
|
|
'split_sentences' => $splitSentences, |
|
98
|
31 |
|
'preserve_formatting' => $preserveFormatting, |
|
99
|
|
|
'outline_detection' => $outlineDetection, |
|
100
|
31 |
|
); |
|
101
|
31 |
|
|
|
102
|
31 |
|
$paramsArray = $this->removeEmptyParams($paramsArray); |
|
103
|
31 |
|
$url = $this->client->buildBaseUrl(); |
|
104
|
|
|
$body = $this->client->buildQuery($paramsArray); |
|
105
|
|
|
|
|
106
|
|
|
// request the DeepL API |
|
107
|
|
|
$translationsArray = $this->client->request($url, $body); |
|
108
|
|
|
|
|
109
|
|
|
return $translationsArray['translations']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
4 |
|
* Calls the usage-Endpoint and return Json-response as an array |
|
114
|
|
|
* |
|
115
|
4 |
|
* @return array |
|
116
|
4 |
|
* @throws DeepLException |
|
117
|
4 |
|
*/ |
|
118
|
|
|
public function usage() |
|
119
|
3 |
|
{ |
|
120
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_USAGE); |
|
121
|
|
|
$usage = $this->client->request($url); |
|
122
|
|
|
|
|
123
|
|
|
return $usage; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param array $paramsArray |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
private function removeEmptyParams($paramsArray) |
|
132
|
|
|
{ |
|
133
|
|
|
foreach ($paramsArray as $key => $value) { |
|
134
|
|
|
if (true === empty($value)) { |
|
135
|
|
|
unset($paramsArray[$key]); |
|
136
|
|
|
} |
|
137
|
|
|
// Special Workaround for outline_detection which will be unset above |
|
138
|
|
|
// DeepL assumes outline_detection=1 if it is not send |
|
139
|
|
|
// in order to deactivate it, we need to send outline_detection=0 to the api |
|
140
|
|
|
if ('outline_detection' === $key) { |
|
141
|
|
|
if (1 === $value) { |
|
142
|
|
|
unset($paramsArray[$key]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (0 === $value) { |
|
146
|
|
|
$paramsArray[$key] = 0; |
|
147
|
|
|
} |
|
148
|
1 |
|
} |
|
149
|
|
|
} |
|
150
|
1 |
|
|
|
151
|
1 |
|
return $paramsArray; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|