@@ -12,38 +12,42 @@ class DeepL
12
12
/**
13
13
* API v1 URL
14
14
*/
15
- const API_URL = 'https://api.deepl.com/v1/translate ' ;
15
+ const API_URL_V1 = 'https://api.deepl.com/v1/translate ' ;
16
16
17
17
/**
18
- * API v1 URL: Parameter auth_key
18
+ * API v2 URL
19
+ */
20
+ const API_URL_V2 = 'https://api.deepl.com/v2/translate ' ;
21
+
22
+ /**
23
+ * API URL: Parameter auth_key
19
24
*/
20
25
const API_URL_AUTH_KEY = 'auth_key=%s ' ;
21
26
22
27
/**
23
- * API v1 URL: Parameter text
28
+ * API URL: Parameter text
24
29
*/
25
30
const API_URL_TEXT = 'text=%s ' ;
26
31
27
32
/**
28
- * API v1 URL: Parameter source_lang
33
+ * API URL: Parameter source_lang
29
34
*/
30
35
const API_URL_SOURCE_LANG = 'source_lang=%s ' ;
31
36
32
37
/**
33
- * API v1 URL: Parameter target_lang
38
+ * API URL: Parameter target_lang
34
39
*/
35
40
const API_URL_DESTINATION_LANG = 'target_lang=%s ' ;
36
41
37
42
/**
38
- * API v1 URL: Parameter tag_handling
43
+ * API URL: Parameter tag_handling
39
44
*/
40
- const API_URL_TAG_HANDLING = 'tag_handling=%s ' ;
45
+ const API_URL_TAG_HANDLING = 'tag_handling=%s ' ;
41
46
42
47
/**
43
- * API v1 URL: Parameter ignore_tags
48
+ * API URL: Parameter ignore_tags
44
49
*/
45
- const API_URL_IGNORE_TAGS = 'ignore_tags=%s ' ;
46
-
50
+ const API_URL_IGNORE_TAGS = 'ignore_tags=%s ' ;
47
51
48
52
/**
49
53
* DeepL HTTP error codes
@@ -92,6 +96,11 @@ class DeepL
92
96
'RU '
93
97
);
94
98
99
+ /**
100
+ * @var integer
101
+ */
102
+ protected $ apiVersion ;
103
+
95
104
/**
96
105
* DeepL API Auth Key (DeepL Pro access required)
97
106
*
@@ -106,22 +115,17 @@ class DeepL
106
115
*/
107
116
protected $ curl ;
108
117
109
- /**
110
- * DeepL ingored tags
111
- *
112
- * @var array
113
- */
114
- protected $ ignoreTags = array ();
115
-
116
118
/**
117
119
* DeepL constructor
118
120
*
119
- * @param $authKey string
121
+ * @param string $authKey
122
+ * @param integer $apiVersion
120
123
*/
121
- public function __construct ($ authKey )
124
+ public function __construct ($ authKey, $ apiVersion = 1 )
122
125
{
123
- $ this ->authKey = $ authKey ;
124
- $ this ->curl = curl_init ();
126
+ $ this ->authKey = $ authKey ;
127
+ $ this ->apiVersion = $ apiVersion ;
128
+ $ this ->curl = curl_init ();
125
129
126
130
curl_setopt ($ this ->curl , CURLOPT_RETURNTRANSFER , 1 );
127
131
}
@@ -139,22 +143,29 @@ public function __destruct()
139
143
/**
140
144
* Translate the text string or array from source to destination language
141
145
*
142
- * @param $text string|string[]
143
- * @param $sourceLanguage string
144
- * @param $destinationLanguage string
145
- * @param $tagHandling array
146
+ * @param string|string[] $text
147
+ * @param string $sourceLanguage
148
+ * @param string $destinationLanguage
149
+ * @param array $tagHandling
150
+ * @param array $ignoreTags
146
151
*
147
152
* @return string|string[]
148
153
*
149
154
* @throws DeepLException
150
155
*/
151
- public function translate ($ text , $ sourceLanguage = 'de ' , $ destinationLanguage = 'en ' , array $ tagHandling = array ())
156
+ public function translate (
157
+ $ text ,
158
+ $ sourceLanguage = 'de ' ,
159
+ $ destinationLanguage = 'en ' ,
160
+ array $ tagHandling = array (),
161
+ array $ ignoreTags = array ()
162
+ )
152
163
{
153
164
// make sure we only accept supported languages
154
165
$ this ->checkLanguages ($ sourceLanguage , $ destinationLanguage );
155
166
156
167
// build the DeepL API request url
157
- $ url = $ this ->buildUrl ($ sourceLanguage , $ destinationLanguage , $ tagHandling );
168
+ $ url = $ this ->buildUrl ($ sourceLanguage , $ destinationLanguage , $ tagHandling, $ ignoreTags );
158
169
$ body = $ this ->buildBody ($ text );
159
170
160
171
// request the DeepL API
@@ -175,8 +186,8 @@ public function translate($text, $sourceLanguage = 'de', $destinationLanguage =
175
186
/**
176
187
* Check if the given languages are supported
177
188
*
178
- * @param $sourceLanguage string
179
- * @param $destinationLanguage string
189
+ * @param string $sourceLanguage
190
+ * @param string $destinationLanguage
180
191
*
181
192
* @return boolean
182
193
*
@@ -202,34 +213,51 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
202
213
/**
203
214
* Build the URL for the DeepL API request
204
215
*
205
- * @param $text string
206
- * @param $sourceLanguage string
207
- * @param $destinationLanguage string
216
+ * @param string $sourceLanguage
217
+ * @param string $destinationLanguage
218
+ * @param array $tagHandling
219
+ * @param array $ignoreTags
208
220
*
209
221
* @return string
210
222
*/
211
- protected function buildUrl ($ sourceLanguage , $ destinationLanguage , $ tagHandling = array ())
223
+ protected function buildUrl (
224
+ $ sourceLanguage ,
225
+ $ destinationLanguage ,
226
+ array $ tagHandling = array (),
227
+ array $ ignoreTags = array ()
228
+ )
212
229
{
213
- $ url = DeepL::API_URL . '? ' . sprintf (DeepL::API_URL_AUTH_KEY , $ this ->authKey );
230
+ // select correct api url
231
+ switch ($ this ->apiVersion ) {
232
+ case 1 :
233
+ $ url = DeepL::API_URL_V1 ;
234
+ break ;
235
+ case 2 :
236
+ $ url = DeepL::API_URL_V2 ;
237
+ break ;
238
+ default :
239
+ $ url = DeepL::API_URL_V1 ;
240
+ }
214
241
242
+ $ url .= '? ' . sprintf (DeepL::API_URL_AUTH_KEY , $ this ->authKey );
215
243
$ url .= '& ' . sprintf (DeepL::API_URL_SOURCE_LANG , strtolower ($ sourceLanguage ));
216
244
$ url .= '& ' . sprintf (DeepL::API_URL_DESTINATION_LANG , strtolower ($ destinationLanguage ));
245
+
217
246
if (!empty ($ tagHandling )) {
218
247
$ url .= '& ' . sprintf (DeepL::API_URL_TAG_HANDLING , implode (', ' , $ tagHandling ));
219
248
}
249
+
220
250
if (!empty ($ this ->ignoreTags )) {
221
- $ url .= '& ' . sprintf (DeepL::API_URL_IGNORE_TAGS , implode (', ' , $ this -> ignoreTags ));
251
+ $ url .= '& ' . sprintf (DeepL::API_URL_IGNORE_TAGS , implode (', ' , $ ignoreTags ));
222
252
}
223
253
224
-
225
-
226
254
return $ url ;
227
255
}
228
256
229
257
/**
230
258
* Build the body for the DeepL API request
231
259
*
232
- * @param $text
260
+ * @param string|string[] $text
233
261
*
234
262
* @return string
235
263
*/
@@ -257,7 +285,7 @@ protected function buildBody($text)
257
285
/**
258
286
* Make a request to the given URL
259
287
*
260
- * @param $url string
288
+ * @param string $url
261
289
*
262
290
* @return array
263
291
*
@@ -290,9 +318,4 @@ protected function request($url, $body)
290
318
291
319
return $ translationsArray ;
292
320
}
293
-
294
- public function setIgnoreTags (array $ ignoreTags )
295
- {
296
- $ this ->ignoreTags = $ ignoreTags ;
297
- }
298
321
}
0 commit comments