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

Commit 3c75c72

Browse files
committed
- Update README.md
- add source & target option to languages-function - add Testscases
1 parent 840d828 commit 3c75c72

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,26 @@ foreach ($translations as $translation) {
5151
}
5252
```
5353
### Supported languages
54-
* `de` - German
55-
* `en` - English
56-
* `fr` - French
57-
* `it` - Italian
58-
* `ja` - Japanese
59-
* `es` - Spanish
60-
* `nl` - Dutch
61-
* `pl` - Polish
62-
* `pt` - Portuguese
63-
* `pt-pt` - Portuguese variant
64-
* `pt-br` - Portuguese variant
65-
* `ru` - Russian
66-
* `zh` - Chinese
54+
In Version 2 we removed the internal List of supported Languages.
55+
Instead, you can now get an array with the supported Languages directly form DeepL:
6756

57+
```php
58+
$languagesArray = $deepl->languages();
59+
60+
foreach ($languagesArray as $language) {
61+
echo $language['name'];
62+
echo $language['language'];
63+
}
64+
```
65+
66+
### Monitoring usage
67+
You can now check ow much you translate, as well as the limits set.
68+
```php
69+
$usageArray = $deepl->usage();
70+
71+
echo 'You have used '.$usageArray['character_count'].' of '.$usageArray['character_limit'].' in in the current billing period.';
72+
73+
```
6874
## Testing
6975

7076
Run PHP_CodeSniffer Tests:
@@ -93,7 +99,7 @@ composer test:all
9399

94100
## Credits
95101

96-
- [Arkadius Jonczek][link-author]
102+
- [babymarkt.de GmbH][link-author]
97103
- [All Contributors][link-contributors]
98104

99105
## License
@@ -112,5 +118,5 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
112118
[link-scrutinizer]: https://scrutinizer-ci.com/g/Baby-Markt/deepl-php-lib/code-structure
113119
[link-code-quality]: https://scrutinizer-ci.com/g/Baby-Markt/deepl-php-lib
114120
[link-downloads]: https://packagist.org/packages/babymarkt/deepl-php-lib
115-
[link-author]: https://github.com/arkadiusjonczek
121+
[link-author]: https://github.com/Baby-Markt
116122
[link-contributors]: ../../contributors

src/DeepL.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,20 @@ public function __destruct()
8282
}
8383

8484
/**
85-
* Calls the usage-Endpoint and return Json-response as an array
85+
* Call languages-Endpoint and return Json-response as an Array
86+
*
87+
* @param string $type
8688
*
8789
* @return array
8890
* @throws DeepLException
8991
*/
90-
public function usage()
92+
public function languages($type = null)
9193
{
92-
$url = $this->buildBaseUrl(self::API_URL_RESOURCE_USAGE);
93-
$usage = $this->request($url);
94+
$url = $this->buildBaseUrl(self::API_URL_RESOURCE_LANGUAGES);
95+
$body = $this->buildQuery(array('type' => $type));
96+
$languages = $this->request($url, $body);
9497

95-
return $usage;
98+
return $languages;
9699
}
97100

98101
/**
@@ -156,19 +159,20 @@ public function translate(
156159
}
157160

158161
/**
159-
* Call languages-Endpoint and return Json-response as an Array
162+
* Calls the usage-Endpoint and return Json-response as an array
160163
*
161164
* @return array
162165
* @throws DeepLException
163166
*/
164-
public function languages()
167+
public function usage()
165168
{
166-
$url = $this->buildBaseUrl(self::API_URL_RESOURCE_LANGUAGES);
167-
$languages = $this->request($url);
169+
$url = $this->buildBaseUrl(self::API_URL_RESOURCE_USAGE);
170+
$usage = $this->request($url);
168171

169-
return $languages;
172+
return $usage;
170173
}
171174

175+
172176
/**
173177
* Creates the Base-Url which all of the 3 API-resources have in common.
174178
*
@@ -197,7 +201,7 @@ protected function buildBaseUrl($resource = 'translate')
197201
*/
198202
protected function buildQuery($paramsArray)
199203
{
200-
if (true === is_array($paramsArray['text'])) {
204+
if (isset($paramsArray['text']) && true === is_array($paramsArray['text'])) {
201205
$text = $paramsArray['text'];
202206
unset($paramsArray['text']);
203207
$textString = '';

tests/integration/DeepLApiTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,58 @@ public function testLanguages()
193193
}
194194
}
195195

196+
/**
197+
* Test languages() can return the source-languages
198+
*/
199+
public function testLanguagesSource()
200+
{
201+
if (self::$authKey === false) {
202+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
203+
}
204+
205+
$deepl = new DeepL(self::$authKey);
206+
$response = $deepl->languages('source');
207+
208+
foreach ($response as $language) {
209+
$this->assertArrayHasKey('language', $language);
210+
$this->assertArrayHasKey('name', $language);
211+
}
212+
}
213+
214+
/**
215+
* Test languages() can return the targe-languages
216+
*/
217+
public function testLanguagesTarget()
218+
{
219+
if (self::$authKey === false) {
220+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
221+
}
222+
223+
$deepl = new DeepL(self::$authKey);
224+
$response = $deepl->languages('target');
225+
226+
foreach ($response as $language) {
227+
$this->assertArrayHasKey('language', $language);
228+
$this->assertArrayHasKey('name', $language);
229+
}
230+
}
231+
232+
/**
233+
* Test languages() will fail with wrong Parameter
234+
*/
235+
public function testLanguagesFail()
236+
{
237+
if (self::$authKey === false) {
238+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
239+
}
240+
241+
$deepl = new DeepL(self::$authKey);
242+
243+
$this->setExpectedException('\BabyMarkt\DeepL\DeepLException');
244+
245+
$deepl->languages('fail');
246+
}
247+
196248
/**
197249
* Test translate() with all Params
198250
*/

0 commit comments

Comments
 (0)