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

Commit 5703499

Browse files
author
Sascha Thielemann
authored
Merge pull request #11 from tomschwiha/formality-patch
Add formality parameter
2 parents 53d5993 + 468105f commit 5703499

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/DeepL.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class DeepL
4949
*/
5050
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
5151

52+
/**
53+
* API URL: Parameter formality
54+
*/
55+
const API_URL_FORMALITY = 'formality=%s';
56+
5257
/**
5358
* DeepL HTTP error codes
5459
*
@@ -152,6 +157,7 @@ public function __destruct()
152157
* @param string $destinationLanguage
153158
* @param array $tagHandling
154159
* @param array $ignoreTags
160+
* @param string $formality
155161
*
156162
* @return string|string[]
157163
*
@@ -162,13 +168,14 @@ public function translate(
162168
$sourceLanguage = 'de',
163169
$destinationLanguage = 'en',
164170
array $tagHandling = array(),
165-
array $ignoreTags = array()
171+
array $ignoreTags = array(),
172+
$formality = "default"
166173
) {
167174
// make sure we only accept supported languages
168175
$this->checkLanguages($sourceLanguage, $destinationLanguage);
169176

170177
// build the DeepL API request url
171-
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling, $ignoreTags);
178+
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling, $ignoreTags, $formality);
172179
$body = $this->buildBody($text);
173180

174181
// request the DeepL API
@@ -222,14 +229,16 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
222229
* @param string $destinationLanguage
223230
* @param array $tagHandling
224231
* @param array $ignoreTags
232+
* @param string $formality
225233
*
226234
* @return string
227235
*/
228236
protected function buildUrl(
229237
$sourceLanguage,
230238
$destinationLanguage,
231239
array $tagHandling = array(),
232-
array $ignoreTags = array()
240+
array $ignoreTags = array(),
241+
$formality = "default"
233242
) {
234243
// select correct api url
235244
switch ($this->apiVersion) {
@@ -255,6 +264,10 @@ protected function buildUrl(
255264
$url .= '&' . sprintf(DeepL::API_URL_IGNORE_TAGS, implode(',', $ignoreTags));
256265
}
257266

267+
if (!empty($formality)) {
268+
$url .= '&' . sprintf(DeepL::API_URL_FORMALITY, $formality);
269+
}
270+
258271
return $url;
259272
}
260273

tests/DeepLTest.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ public function testCheckLanguagesDestinationLanguageException()
104104
*/
105105
public function testBuildUrl()
106106
{
107-
$expectedString = 'https://api.deepl.com/v1/translate?auth_key=123456&source_lang=de&target_lang=en';
108-
109107
$authKey = '123456';
108+
109+
$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
110+
'auth_key' => $authKey,
111+
'source_lang' => 'de',
112+
'target_lang' => 'en',
113+
'formality' => 'default'
114+
));
115+
110116
$deepl = new DeepL($authKey);
111117

112118
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
@@ -121,10 +127,16 @@ public function testBuildUrl()
121127
*/
122128
public function testBuildUrlWithTags()
123129
{
124-
$expectedString = 'https://api.deepl.com';
125-
$expectedString .= '/v1/translate?auth_key=123456&source_lang=de&target_lang=en&tag_handling=xml&ignore_tags=x';
126-
127130
$authKey = '123456';
131+
$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
132+
'auth_key' => $authKey,
133+
'source_lang' => 'de',
134+
'target_lang' => 'en',
135+
'tag_handling' => 'xml',
136+
'ignore_tags' => 'x',
137+
'formality' => 'default'
138+
));
139+
128140
$deepl = new DeepL($authKey);
129141

130142
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');

0 commit comments

Comments
 (0)