Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9122a3e

Browse files
Merge pull request #4 from Baby-Markt/feature/v2-support-clean
add deepl v2 api support and fix some little errors
2 parents f093d96 + e7e669f commit 9122a3e

File tree

2 files changed

+133
-64
lines changed

2 files changed

+133
-64
lines changed

src/DeepL.php

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,42 @@ class DeepL
1212
/**
1313
* API v1 URL
1414
*/
15-
const API_URL = 'https://api.deepl.com/v1/translate';
15+
const API_URL_V1 = 'https://api.deepl.com/v1/translate';
1616

1717
/**
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
1924
*/
2025
const API_URL_AUTH_KEY = 'auth_key=%s';
2126

2227
/**
23-
* API v1 URL: Parameter text
28+
* API URL: Parameter text
2429
*/
2530
const API_URL_TEXT = 'text=%s';
2631

2732
/**
28-
* API v1 URL: Parameter source_lang
33+
* API URL: Parameter source_lang
2934
*/
3035
const API_URL_SOURCE_LANG = 'source_lang=%s';
3136

3237
/**
33-
* API v1 URL: Parameter target_lang
38+
* API URL: Parameter target_lang
3439
*/
3540
const API_URL_DESTINATION_LANG = 'target_lang=%s';
3641

3742
/**
38-
* API v1 URL: Parameter tag_handling
43+
* API URL: Parameter tag_handling
3944
*/
40-
const API_URL_TAG_HANDLING = 'tag_handling=%s';
45+
const API_URL_TAG_HANDLING = 'tag_handling=%s';
4146

4247
/**
43-
* API v1 URL: Parameter ignore_tags
48+
* API URL: Parameter ignore_tags
4449
*/
45-
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
46-
50+
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
4751

4852
/**
4953
* DeepL HTTP error codes
@@ -92,6 +96,11 @@ class DeepL
9296
'RU'
9397
);
9498

99+
/**
100+
* @var integer
101+
*/
102+
protected $apiVersion;
103+
95104
/**
96105
* DeepL API Auth Key (DeepL Pro access required)
97106
*
@@ -106,22 +115,17 @@ class DeepL
106115
*/
107116
protected $curl;
108117

109-
/**
110-
* DeepL ingored tags
111-
*
112-
* @var array
113-
*/
114-
protected $ignoreTags = array();
115-
116118
/**
117119
* DeepL constructor
118120
*
119-
* @param $authKey string
121+
* @param string $authKey
122+
* @param integer $apiVersion
120123
*/
121-
public function __construct($authKey)
124+
public function __construct($authKey, $apiVersion = 1)
122125
{
123-
$this->authKey = $authKey;
124-
$this->curl = curl_init();
126+
$this->authKey = $authKey;
127+
$this->apiVersion = $apiVersion;
128+
$this->curl = curl_init();
125129

126130
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
127131
}
@@ -139,22 +143,29 @@ public function __destruct()
139143
/**
140144
* Translate the text string or array from source to destination language
141145
*
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
146151
*
147152
* @return string|string[]
148153
*
149154
* @throws DeepLException
150155
*/
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+
)
152163
{
153164
// make sure we only accept supported languages
154165
$this->checkLanguages($sourceLanguage, $destinationLanguage);
155166

156167
// build the DeepL API request url
157-
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling);
168+
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling, $ignoreTags);
158169
$body = $this->buildBody($text);
159170

160171
// request the DeepL API
@@ -175,8 +186,8 @@ public function translate($text, $sourceLanguage = 'de', $destinationLanguage =
175186
/**
176187
* Check if the given languages are supported
177188
*
178-
* @param $sourceLanguage string
179-
* @param $destinationLanguage string
189+
* @param string $sourceLanguage
190+
* @param string $destinationLanguage
180191
*
181192
* @return boolean
182193
*
@@ -202,34 +213,51 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
202213
/**
203214
* Build the URL for the DeepL API request
204215
*
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
208220
*
209221
* @return string
210222
*/
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+
)
212229
{
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+
}
214241

242+
$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
215243
$url .= '&' . sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage));
216244
$url .= '&' . sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage));
245+
217246
if (!empty($tagHandling)) {
218247
$url .= '&' . sprintf(DeepL::API_URL_TAG_HANDLING, implode(',', $tagHandling));
219248
}
249+
220250
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));
222252
}
223253

224-
225-
226254
return $url;
227255
}
228256

229257
/**
230258
* Build the body for the DeepL API request
231259
*
232-
* @param $text
260+
* @param string|string[] $text
233261
*
234262
* @return string
235263
*/
@@ -257,7 +285,7 @@ protected function buildBody($text)
257285
/**
258286
* Make a request to the given URL
259287
*
260-
* @param $url string
288+
* @param string $url
261289
*
262290
* @return array
263291
*
@@ -290,9 +318,4 @@ protected function request($url, $body)
290318

291319
return $translationsArray;
292320
}
293-
294-
public function setIgnoreTags(array $ignoreTags)
295-
{
296-
$this->ignoreTags = $ignoreTags;
297-
}
298321
}

tests/DeepLTest.php

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@
99
*/
1010
class DeepLTest extends \PHPUnit_Framework_TestCase
1111
{
12+
/**
13+
* DeepL Auth Key (Set if you want to test all methods)
14+
*
15+
* @var string
16+
*/
17+
private $authKey = '';
18+
1219
/**
1320
* Get protected method
1421
*
1522
* @param $className
1623
* @param $methodName
1724
*
25+
* @throws \ReflectionException
26+
*
1827
* @return \ReflectionMethod
1928
*/
2029
protected static function getMethod($className, $methodName)
@@ -110,6 +119,8 @@ public function testBuildBody()
110119
*/
111120
public function testTranslate()
112121
{
122+
return;
123+
113124
$mock = $this->getMockBuilder('\BabyMarkt\DeepL\DeepL');
114125

115126
// TODO: test if translate methods calls correct methods
@@ -122,16 +133,39 @@ public function testTranslate()
122133
*/
123134
public function testTranslateSuccess()
124135
{
125-
return;
136+
if (!$this->authKey) {
137+
return;
138+
}
126139

127-
$authKey = 'INSERT YOUR AUTH KEY HERE';
128-
$germanText = 'Hallo Welt';
140+
$deepl = new DeepL($this->authKey);
129141

130-
$deepl = new DeepL($authKey);
142+
$germanText = 'Hallo Welt';
143+
$expectedText = 'Hello World';
144+
145+
$translatedText = $deepl->translate($germanText);
146+
147+
$this->assertEquals($expectedText, $translatedText);
148+
}
149+
150+
/**
151+
* Test translate() success with v2 API
152+
*
153+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
154+
*/
155+
public function testTranslateV2Success()
156+
{
157+
if (!$this->authKey) {
158+
return;
159+
}
160+
161+
$deepl = new DeepL($this->authKey, 2);
162+
163+
$germanText = 'Hallo Welt';
164+
$expectedText = 'Hello World';
131165

132166
$translatedText = $deepl->translate($germanText);
133167

134-
$this->assertEquals('Hello World', $translatedText);
168+
$this->assertEquals($expectedText, $translatedText);
135169
}
136170

137171
/**
@@ -141,16 +175,23 @@ public function testTranslateSuccess()
141175
*/
142176
public function testTranslateTagHandlingSuccess()
143177
{
144-
return;
178+
if (!$this->authKey) {
179+
return;
180+
}
145181

146-
$authKey = 'INSERT YOUR AUTH KEY HERE';
147-
$englishText = '<strong>text to translate</strong>';
182+
$deepl = new DeepL($this->authKey);
148183

149-
$deepl = new DeepL($authKey);
184+
$englishText = '<strong>text to translate</strong>';
185+
$expectedText = '<strong>zu übersetzender Text</strong>';
150186

151-
$translatedText = $deepl->translate($germanText, 'en', 'de', array('xml'));
187+
$translatedText = $deepl->translate(
188+
$englishText,
189+
'en',
190+
'de',
191+
array('xml')
192+
);
152193

153-
$this->assertEquals('<strong>Text zu übersetzen</strong>', $translatedText);
194+
$this->assertEquals($expectedText, $translatedText);
154195
}
155196

156197
/**
@@ -160,23 +201,28 @@ public function testTranslateTagHandlingSuccess()
160201
*/
161202
public function testTranslateIgnoreTagsSuccess()
162203
{
163-
return;
204+
if (!$this->authKey) {
205+
return;
206+
}
164207

165-
$authKey = 'INSERT YOUR AUTH KEY HERE';
166-
$englishText = '<strong>text to do not translate</strong><p>text to translate</p>';
208+
$deepl = new DeepL($this->authKey);
167209

168-
$deepl = new DeepL($authKey);
210+
$englishText = '<strong>text to do not translate</strong><p>text to translate</p>';
211+
$expectedText = '<strong>Text, der nicht übersetzt werden soll</strong><p>zu übersetzender Text</p>';
169212

170-
$deepl->setIgnoreTags(array('strong'));
171-
$translatedText = $deepl->translate($germanText, 'en', 'de', array('xml'));
213+
$translatedText = $deepl->translate(
214+
$englishText,
215+
'en',
216+
'de',
217+
array('xml'),
218+
array('strong')
219+
);
172220

173-
$this->assertEquals('<strong>text to do not translate</strong><p>Text zu übersetzen</p>', $translatedText);
221+
$this->assertEquals($expectedText, $translatedText);
174222
}
175223

176224
/**
177225
* Test translate()
178-
*
179-
* TEST REQUIRES VALID DEEPL AUTH KEY!!
180226
*/
181227
public function testTranslateException()
182228
{

0 commit comments

Comments
 (0)