|
10 | 10 | class DeepL
|
11 | 11 | {
|
12 | 12 | /**
|
13 |
| - * API v1 URL |
| 13 | + * API BASE URL |
| 14 | + */ |
| 15 | + const API_URL_BASE = '%s://%s/v%s'; |
| 16 | + |
| 17 | + /** |
| 18 | + * API URL: translate |
| 19 | + */ |
| 20 | + const API_URL_RESOURCE_TRANSLATE = 'translate'; |
| 21 | + |
| 22 | + /** |
| 23 | + * API URL: usage |
14 | 24 | */
|
15 |
| - const API_URL_V1 = 'https://api.deepl.com/v1/translate'; |
| 25 | + const API_URL_RESOURCE_USAGE = 'usage'; |
16 | 26 |
|
17 | 27 | /**
|
18 |
| - * API v2 URL |
| 28 | + * API URL: languages |
19 | 29 | */
|
20 |
| - const API_URL_V2 = 'https://api.deepl.com/v2/translate'; |
| 30 | + const API_URL_RESOURCE_LANGUAGES = 'languages '; |
21 | 31 |
|
22 | 32 | /**
|
23 | 33 | * API URL: Parameter auth_key
|
@@ -132,10 +142,11 @@ class DeepL
|
132 | 142 | * @param string $authKey
|
133 | 143 | * @param integer $apiVersion
|
134 | 144 | */
|
135 |
| - public function __construct($authKey, $apiVersion = 2) |
| 145 | + public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com') |
136 | 146 | {
|
137 | 147 | $this->authKey = $authKey;
|
138 | 148 | $this->apiVersion = $apiVersion;
|
| 149 | + $this->host = $host; |
139 | 150 | $this->curl = curl_init();
|
140 | 151 |
|
141 | 152 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
|
@@ -193,6 +204,16 @@ public function translate(
|
193 | 204 | return $translationsArray['translations'];
|
194 | 205 | }
|
195 | 206 |
|
| 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 | + |
196 | 217 | /**
|
197 | 218 | * Check if the given languages are supported
|
198 | 219 | *
|
@@ -232,31 +253,30 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
|
232 | 253 | * @param array $tagHandling
|
233 | 254 | * @param array $ignoreTags
|
234 | 255 | * @param string $formality
|
| 256 | + * @param string $resource |
235 | 257 | *
|
236 | 258 | * @return string
|
237 | 259 | */
|
238 | 260 | protected function buildUrl(
|
239 |
| - $sourceLanguage, |
240 |
| - $destinationLanguage, |
241 |
| - array $tagHandling = array(), |
242 |
| - array $ignoreTags = array(), |
243 |
| - $formality = "default" |
244 |
| - ) { |
245 |
| - // select correct api url |
246 |
| - switch ($this->apiVersion) { |
247 |
| - case 1: |
248 |
| - $url = DeepL::API_URL_V1; |
249 |
| - break; |
250 |
| - case 2: |
251 |
| - $url = DeepL::API_URL_V2; |
252 |
| - break; |
253 |
| - default: |
254 |
| - $url = DeepL::API_URL_V2; |
| 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); |
| 270 | + $url .= sprintf('/%s', $resource); |
| 271 | + $url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey); |
| 272 | + |
| 273 | + if (!empty($sourceLanguage)) { |
| 274 | + $url .= '&'.sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage)); |
255 | 275 | }
|
256 | 276 |
|
257 |
| - $url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey); |
258 |
| - $url .= '&' . sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage)); |
259 |
| - $url .= '&' . sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage)); |
| 277 | + if (!empty($destinationLanguage)) { |
| 278 | + $url .= '&'.sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage)); |
| 279 | + } |
260 | 280 |
|
261 | 281 | if (!empty($tagHandling)) {
|
262 | 282 | $url .= '&' . sprintf(DeepL::API_URL_TAG_HANDLING, implode(',', $tagHandling));
|
|
0 commit comments