10
10
class DeepL
11
11
{
12
12
/**
13
- * API BASE URL
14
- */
13
+ * API BASE URL
14
+ */
15
15
const API_URL_BASE = '%s://%s/v%s ' ;
16
16
17
17
/**
@@ -27,7 +27,7 @@ class DeepL
27
27
/**
28
28
* API URL: languages
29
29
*/
30
- const API_URL_RESOURCE_LANGUAGES = 'languages ' ;
30
+ const API_URL_RESOURCE_LANGUAGES = 'languages ' ;
31
31
32
32
/**
33
33
* API URL: Parameter auth_key
@@ -64,25 +64,50 @@ class DeepL
64
64
*/
65
65
const API_URL_FORMALITY = 'formality=%s ' ;
66
66
67
+ /**
68
+ * API URL: Parameter split_sentences
69
+ */
70
+ const API_URL_SPLIT_SENTENCES = 'split_sentences=%s ' ;
71
+
72
+ /**
73
+ * API URL: Parameter preserve_formatting
74
+ */
75
+ const API_URL_PRESERVER_FORMATING = 'preserve_formatting=%s ' ;
76
+
77
+ /**
78
+ * API URL: Parameter non_splitting_tags
79
+ */
80
+ const API_URL_NON_SPLITTING_TAGS = 'non_splitting_tags=%s ' ;
81
+
82
+ /**
83
+ * API URL: Parameter outline_detection
84
+ */
85
+ const API_URL_OUTLINE_DETECTION = 'outline_detection=%s ' ;
86
+
87
+ /**
88
+ * API URL: Parameter splitting_tags
89
+ */
90
+ const API_URL_SPLITTING_TAGS = 'splitting_tags=%s ' ;
91
+
67
92
/**
68
93
* DeepL HTTP error codes
69
94
*
70
95
* @var array
71
96
*/
72
- protected $ errorCodes = array (
97
+ protected $ errorCodes = [
73
98
400 => 'Wrong request, please check error message and your parameters. ' ,
74
99
403 => 'Authorization failed. Please supply a valid auth_key parameter. ' ,
75
100
413 => 'Request Entity Too Large. The request size exceeds the current limit. ' ,
76
101
429 => 'Too many requests. Please wait and send your request once again. ' ,
77
- 456 => 'Quota exceeded. The character limit has been reached. '
78
- ) ;
102
+ 456 => 'Quota exceeded. The character limit has been reached. ' ,
103
+ ] ;
79
104
80
105
/**
81
106
* Supported translation source languages
82
107
*
83
108
* @var array
84
109
*/
85
- protected $ sourceLanguages = array (
110
+ protected $ sourceLanguages = [
86
111
'EN ' ,
87
112
'DE ' ,
88
113
'FR ' ,
@@ -93,15 +118,15 @@ class DeepL
93
118
'PL ' ,
94
119
'RU ' ,
95
120
'JA ' ,
96
- 'ZH '
97
- ) ;
121
+ 'ZH ' ,
122
+ ] ;
98
123
99
124
/**
100
125
* Supported translation destination languages
101
126
*
102
127
* @var array
103
128
*/
104
- protected $ destinationLanguages = array (
129
+ protected $ destinationLanguages = [
105
130
'EN ' ,
106
131
'DE ' ,
107
132
'FR ' ,
@@ -114,8 +139,8 @@ class DeepL
114
139
'PL ' ,
115
140
'RU ' ,
116
141
'JA ' ,
117
- 'ZH '
118
- ) ;
142
+ 'ZH ' ,
143
+ ] ;
119
144
120
145
/**
121
146
* @var integer
@@ -177,18 +202,31 @@ public function __destruct()
177
202
* @throws DeepLException
178
203
*/
179
204
public function translate (
180
- $ text ,
181
- $ sourceLanguage = 'de ' ,
182
- $ destinationLanguage = 'en ' ,
183
- array $ tagHandling = array (),
184
- array $ ignoreTags = array (),
185
- $ formality = "default "
205
+ string $ text ,
206
+ string $ sourceLanguage = 'de ' ,
207
+ string $ destinationLanguage = 'en ' ,
208
+ string $ tagHandling = null ,
209
+ array $ ignoreTags = null ,
210
+ string $ formality = "default " ,
211
+ string $ resource = 'translate ' ,
212
+ string $ splitSentences = null ,
213
+ bool $ preserveFormatting = null ,
214
+ array $ nonSplittingTags = null ,
215
+ bool $ outlineDetection = null ,
216
+ array $ splittingTags = null
186
217
) {
187
218
// make sure we only accept supported languages
188
219
$ this ->checkLanguages ($ sourceLanguage , $ destinationLanguage );
189
220
190
221
// build the DeepL API request url
191
- $ url = $ this ->buildUrl ($ sourceLanguage , $ destinationLanguage , $ tagHandling , $ ignoreTags , $ formality );
222
+ $ url = $ this ->buildUrl (
223
+ $ sourceLanguage ,
224
+ $ destinationLanguage ,
225
+ $ tagHandling ,
226
+ $ ignoreTags ,
227
+ $ formality ,
228
+ self ::API_URL_RESOURCE_TRANSLATE
229
+ );
192
230
$ body = $ this ->buildBody ($ text );
193
231
194
232
// request the DeepL API
@@ -204,16 +242,6 @@ public function translate(
204
242
return $ translationsArray ['translations ' ];
205
243
}
206
244
207
- public function usage ()
208
- {
209
- $ result = [];
210
- $ body = '' ;
211
- $ url = $ this ->buildUrl (null , null , [], [], '' , 'usage ' );
212
- $ result = $ this ->request ($ url , $ body );
213
-
214
- return $ result ;
215
- }
216
-
217
245
/**
218
246
* Check if the given languages are supported
219
247
*
@@ -224,19 +252,19 @@ public function usage()
224
252
*
225
253
* @throws DeepLException
226
254
*/
227
- protected function checkLanguages ($ sourceLanguage , $ destinationLanguage )
255
+ protected function checkLanguages (string $ sourceLanguage , string $ destinationLanguage )
228
256
{
229
257
$ sourceLanguage = strtoupper ($ sourceLanguage );
230
258
231
- if (! in_array ($ sourceLanguage , $ this ->sourceLanguages )) {
259
+ if (false === in_array ($ sourceLanguage , $ this ->sourceLanguages )) {
232
260
throw new DeepLException (
233
261
sprintf ('The language "%s" is not supported as source language. ' , $ sourceLanguage )
234
262
);
235
263
}
236
264
237
265
$ destinationLanguage = strtoupper ($ destinationLanguage );
238
266
239
- if (! in_array ($ destinationLanguage , $ this ->destinationLanguages )) {
267
+ if (false === in_array ($ destinationLanguage , $ this ->destinationLanguages )) {
240
268
throw new DeepLException (
241
269
sprintf ('The language "%s" is not supported as destination language. ' , $ destinationLanguage )
242
270
);
@@ -258,66 +286,63 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
258
286
* @return string
259
287
*/
260
288
protected function buildUrl (
261
- $ sourceLanguage = null ,
262
- $ destinationLanguage = null ,
263
- $ tagHandling = null ,
264
- $ ignoreTags = null ,
265
- $ formality ="default " ,
266
- $ resource ='translate '
267
- )
268
- {
269
- $ url = sprintf (DeepL::API_URL_BASE , 'https ' , $ this ->host , $ this ->apiVersion );
289
+ string $ sourceLanguage = null ,
290
+ string $ destinationLanguage = null ,
291
+ string $ tagHandling = null ,
292
+ array $ ignoreTags = null ,
293
+ string $ formality = 'default ' ,
294
+ string $ resource = 'translate ' ,
295
+ string $ splitSentences = null ,
296
+ bool $ preserveFormatting = null ,
297
+ array $ nonSplittingTags = null ,
298
+ bool $ outlineDetection = null ,
299
+ array $ splittingTags = null
300
+ ) {
301
+ $ url = sprintf (self ::API_URL_BASE , 'https ' , $ this ->host , $ this ->apiVersion );
270
302
$ url .= sprintf ('/%s ' , $ resource );
271
- $ url .= '? ' . sprintf (DeepL ::API_URL_AUTH_KEY , $ this ->authKey );
303
+ $ url .= '? ' . sprintf (self ::API_URL_AUTH_KEY , $ this ->authKey );
272
304
273
- if (! empty ($ sourceLanguage )) {
274
- $ url .= '& ' .sprintf (DeepL ::API_URL_SOURCE_LANG , strtolower ($ sourceLanguage ));
305
+ if (false === empty ($ sourceLanguage )) {
306
+ $ url .= '& ' .sprintf (self ::API_URL_SOURCE_LANG , strtolower ($ sourceLanguage ));
275
307
}
276
308
277
- if (! empty ($ destinationLanguage )) {
278
- $ url .= '& ' .sprintf (DeepL ::API_URL_DESTINATION_LANG , strtolower ($ destinationLanguage ));
309
+ if (false === empty ($ destinationLanguage )) {
310
+ $ url .= '& ' .sprintf (self ::API_URL_DESTINATION_LANG , strtolower ($ destinationLanguage ));
279
311
}
280
312
281
- if (! empty ($ tagHandling )) {
282
- $ url .= '& ' . sprintf (DeepL ::API_URL_TAG_HANDLING , implode ( ' , ' , $ tagHandling) );
313
+ if (false === empty ($ tagHandling )) {
314
+ $ url .= '& ' . sprintf (self ::API_URL_TAG_HANDLING , $ tagHandling );
283
315
}
284
316
285
- if (! empty ($ ignoreTags )) {
286
- $ url .= '& ' . sprintf (DeepL ::API_URL_IGNORE_TAGS , implode (', ' , $ ignoreTags ));
317
+ if (false === empty ($ ignoreTags )) {
318
+ $ url .= '& ' . sprintf (self ::API_URL_IGNORE_TAGS , implode (', ' , $ ignoreTags ));
287
319
}
288
320
289
- if (! empty ($ formality )) {
290
- $ url .= '& ' . sprintf (DeepL ::API_URL_FORMALITY , $ formality );
321
+ if (false === empty ($ formality )) {
322
+ $ url .= '& ' . sprintf (self ::API_URL_FORMALITY , $ formality );
291
323
}
292
324
293
- return $ url ;
294
- }
325
+ if (false === empty ($ splitSentences )) {
326
+ $ url .= '& ' .sprintf (self ::API_URL_SPLIT_SENTENCES , $ splitSentences );
327
+ }
295
328
296
- /**
297
- * Build the body for the DeepL API request
298
- *
299
- * @param string|string[] $text
300
- *
301
- * @return string
302
- */
303
- protected function buildBody ($ text )
304
- {
305
- $ body = '' ;
306
- $ first = true ;
329
+ if (false === empty ($ preserveFormatting )) {
330
+ $ url .= '& ' .sprintf (self ::API_URL_PRESERVER_FORMATING , $ preserveFormatting );
331
+ }
307
332
308
- if (! is_array ( $ text )) {
309
- $ text = ( array ) $ text ;
333
+ if (false === empty ( $ nonSplittingTags )) {
334
+ $ url .= ' & ' . sprintf ( self :: API_URL_NON_SPLITTING_TAGS , implode ( ' , ' , $ nonSplittingTags )) ;
310
335
}
311
336
312
- foreach ($ text as $ textElement ) {
313
- $ body .= ($ first ? '' : '& ' ) . sprintf (DeepL::API_URL_TEXT , rawurlencode ($ textElement ));
337
+ if (false === empty ($ outlineDetection )) {
338
+ $ url .= '& ' .sprintf (self ::API_URL_OUTLINE_DETECTION , $ outlineDetection );
339
+ }
314
340
315
- if ($ first ) {
316
- $ first = false ;
317
- }
341
+ if (false === empty ($ splittingTags )) {
342
+ $ url .= '& ' .sprintf (self ::API_URL_SPLITTING_TAGS , implode (', ' , $ splittingTags ));
318
343
}
319
344
320
- return $ body ;
345
+ return $ url ;
321
346
}
322
347
323
348
/**
@@ -331,9 +356,10 @@ protected function buildBody($text)
331
356
*/
332
357
protected function request ($ url , $ body )
333
358
{
359
+ curl_setopt ($ this ->curl , CURLOPT_POST , true );
334
360
curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
335
361
curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , $ body );
336
- curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , array ( 'Content-Type: application/x-www-form-urlencoded ' ) );
362
+ curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , [ 'Content-Type: application/x-www-form-urlencoded ' ] );
337
363
338
364
$ response = curl_exec ($ this ->curl );
339
365
@@ -355,4 +381,60 @@ protected function request($url, $body)
355
381
356
382
return $ translationsArray ;
357
383
}
384
+
385
+ /**
386
+ * Build the body for the DeepL API request
387
+ *
388
+ * @param string|string[] $text
389
+ *
390
+ * @return string
391
+ */
392
+ protected function buildBody ($ text )
393
+ {
394
+ $ body = '' ;
395
+ $ first = true ;
396
+
397
+ if (!is_array ($ text )) {
398
+ $ text = (array )$ text ;
399
+ }
400
+
401
+ foreach ($ text as $ textElement ) {
402
+ $ body .= ($ first ? '' : '& ' ).sprintf (self ::API_URL_TEXT , rawurlencode ($ textElement ));
403
+
404
+ if ($ first ) {
405
+ $ first = false ;
406
+ }
407
+ }
408
+
409
+ return $ body ;
410
+ }
411
+
412
+ /**
413
+ * @return array
414
+ * @throws DeepLException
415
+ */
416
+ public function usage ()
417
+ {
418
+ $ result = [];
419
+ $ body = '' ;
420
+ $ url = $ this ->buildUrl (null , null , null , null , '' , self ::API_URL_RESOURCE_USAGE );
421
+ $ result = $ this ->request ($ url , $ body );
422
+
423
+ return $ result ;
424
+ }
425
+
426
+
427
+ /**
428
+ * @return array
429
+ * @throws DeepLException
430
+ */
431
+ public function languages ()
432
+ {
433
+ $ result = [];
434
+ $ body = '' ;
435
+ $ url = $ this ->buildUrl (null , null , [], [], '' , self ::API_URL_RESOURCE_LANGUAGES );
436
+ $ result = $ this ->request ($ url , $ body );
437
+
438
+ return $ result ;
439
+ }
358
440
}
0 commit comments