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

Commit 61d2a59

Browse files
Merge pull request #1 from weglot/master
Add XML tag handling support
2 parents 80a0fca + 214a355 commit 61d2a59

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/DeepL.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ class DeepL
3434
*/
3535
const API_URL_DESTINATION_LANG = 'target_lang=%s';
3636

37+
/**
38+
* API v1 URL: Parameter tag_handling
39+
*/
40+
const API_URL_TAG_HANDLING = 'tag_handling=%s';
41+
42+
/**
43+
* API v1 URL: Parameter ignore_tags
44+
*/
45+
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
46+
47+
3748
/**
3849
* DeepL HTTP error codes
3950
*
@@ -91,6 +102,13 @@ class DeepL
91102
*/
92103
protected $curl;
93104

105+
/**
106+
* DeepL ingored tags
107+
*
108+
* @var array
109+
*/
110+
protected $ignoreTags = array();
111+
94112
/**
95113
* DeepL constructor
96114
*
@@ -120,18 +138,19 @@ public function __destruct()
120138
* @param $text string|string[]
121139
* @param $sourceLanguage string
122140
* @param $destinationLanguage string
141+
* @param $tagHandling array
123142
*
124143
* @return string|string[]
125144
*
126145
* @throws DeepLException
127146
*/
128-
public function translate($text, $sourceLanguage = 'de', $destinationLanguage = 'en')
147+
public function translate($text, $sourceLanguage = 'de', $destinationLanguage = 'en', array $tagHandling = array())
129148
{
130149
// make sure we only accept supported languages
131150
$this->checkLanguages($sourceLanguage, $destinationLanguage);
132151

133152
// build the DeepL API request url
134-
$url = $this->buildUrl($sourceLanguage, $destinationLanguage);
153+
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling);
135154
$body = $this->buildBody($text);
136155

137156
// request the DeepL API
@@ -185,12 +204,20 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
185204
*
186205
* @return string
187206
*/
188-
protected function buildUrl($sourceLanguage, $destinationLanguage)
207+
protected function buildUrl($sourceLanguage, $destinationLanguage, $tagHandling = array())
189208
{
190209
$url = DeepL::API_URL . '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
191210

192211
$url .= '&' . sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage));
193212
$url .= '&' . sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage));
213+
if (!empty($tagHandling)) {
214+
$url .= '&' . sprintf(DeepL::API_URL_TAG_HANDLING, implode(',', $tagHandling));
215+
}
216+
if (!empty($this->ignoreTags)) {
217+
$url .= '&' . sprintf(DeepL::API_URL_IGNORE_TAGS, implode(',', $this->ignoreTags));
218+
}
219+
220+
194221

195222
return $url;
196223
}
@@ -259,4 +286,9 @@ protected function request($url, $body)
259286

260287
return $translationsArray;
261288
}
289+
290+
public function setIgnoreTags(array $ignoreTags)
291+
{
292+
$this->ignoreTags = $ignoreTags;
293+
}
262294
}

tests/DeepLTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,45 @@ public function testTranslateSuccess()
134134
$this->assertEquals('Hello World', $translatedText);
135135
}
136136

137+
/**
138+
* Test translate() with tag handling success
139+
*
140+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
141+
*/
142+
public function testTranslateTagHandlingSuccess()
143+
{
144+
return;
145+
146+
$authKey = 'INSERT YOUR AUTH KEY HERE';
147+
$englishText = '<strong>text to translate</strong>';
148+
149+
$deepl = new DeepL($authKey);
150+
151+
$translatedText = $deepl->translate($germanText, 'en', 'de', array('xml'));
152+
153+
$this->assertEquals('<strong>Text zu übersetzen</strong>', $translatedText);
154+
}
155+
156+
/**
157+
* Test translate() with tag ignored success
158+
*
159+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
160+
*/
161+
public function testTranslateIgnoreTagsSuccess()
162+
{
163+
return;
164+
165+
$authKey = 'INSERT YOUR AUTH KEY HERE';
166+
$englishText = '<strong>text to do not translate</strong><p>text to translate</p>';
167+
168+
$deepl = new DeepL($authKey);
169+
170+
$deepl->setIgnoreTags(array('strong'));
171+
$translatedText = $deepl->translate($germanText, 'en', 'de', array('xml'));
172+
173+
$this->assertEquals('<strong>text to do not translate</strong><p>Text zu übersetzen</p>', $translatedText);
174+
}
175+
137176
/**
138177
* Test translate()
139178
*

0 commit comments

Comments
 (0)