diff --git a/.travis.yml b/.travis.yml index e42501b..7dd1847 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ matrix: - php: 7.3 - php: 7.4 - php: 8.0 + - php: 8.1 - php: nightly allow_failures: - php: nightly diff --git a/CHANGELOG.md b/CHANGELOG.md index 684f1a7..3e1c7a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [4.0.0] - 2022-**-05 +### Added +- marked Package as abandoned +- PHP 8.1 compatibility +- glossary_id to translate-method +- ability to run Integration-Tests against custom host if needed (Free/Pro) +### Changed +- migrated phpunit.xml.dist +- fixed some Tests and general Maintenance such as Typos in README.md +- **Breaking Change!** standard DEEPL_HOST to free again +- **Breaking Change!** default source Langauge form DE to empty so DeepL will decide +### Removed + ## [3.0.1] - 2021-07-09 ### Added ### Changed diff --git a/LICENSE.md b/LICENSE.md index 7c0b357..0dbdf02 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # The MIT License (MIT) -Copyright (c) 2019 babymarkt.de GmbH +Copyright (c) 2018-2022 babymarkt.de GmbH > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 90dba14..56df683 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,20 @@ # deepl-php-lib +*** +## Deprecated Library ! +Hello everyone, + +in the last couple of months we didn't give much attention to this projekt. DeepL on the other hand has developed client libraries for Python, .NET and Node.js (see: [API-Doc][link-deepl-client-libraries]). + +Since there is a PHP-Library in the works [Github][link-deepl-php-lib-github] and our track record with maintaining our library hasn't been as good as it should have been, we decided to abandon this library and switch internally to the official one. + +To make clear that we won't continue to work on this repository, we will archive it. If anyone from the community wants to continue our work we can put a line in the README to point to the new maintained repository. + +Otherwise, we recommend you to check out the new library coming from [DeepL][link-deepl-php-lib-github]. + +Thank you to everyone who contributed to this project over the years. + +*** [![Latest Version on Packagist][ico-version]][link-packagist] [![Software License][ico-license]](LICENSE.md) [![Build Status][ico-travis]][link-travis] @@ -39,7 +54,7 @@ Use the DeepL API Pro: use \BabyMarkt\DeepL\DeepL; $authKey = ''; -$deepl = new DeepL($authKey,2,'api.deepl.com'); +$deepl = new DeepL($authKey,2,'api-free.deepl.com'); ``` ### Translate @@ -69,7 +84,7 @@ foreach ($translations as $translation) { | param | Description | |---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | $text | Text to be translated. Only UTF8-encoded plain text is supported. The parameter may be specified as an Array and translations are returned in the same order as they are requested. Each of the array values may contain multiple sentences. Up to 50 texts can be sent for translation in one request. | -| $sourceLang | Language of the text to be translated.
default: de | +| $sourceLang | Language of the text to be translated.
default: "" (DeepL will auto-detect) | | $targetLang | The language into which the text should be translated.
default: en | | $tagHandling | Sets which kind of tags should be handled. Options currently available: "xml" | | $ignoreTags | Array of XML tags that indicate text not to be translated.
default: null | @@ -123,21 +138,21 @@ Create a glossary $glossary = $deepl->createGlossary('MyGlossary', ['Hallo' => 'Hello'], 'de', 'en'); ``` -| param | Description | -|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| $name | Glossary name -| $entries | Array of entries -| $sourceLanguage | The source language into which the glossary rule apply | -| $targetLanguage | The target language into which the glossary rule apply | +| param | Description | +|-----------------|--------------------------------------------------------| +| $name | Glossary name | +| $entries | Array of entries | +| $sourceLanguage | The source language into which the glossary rule apply | +| $targetLanguage | The target language into which the glossary rule apply | Delete a glossary ```php $glossary = $deepl->deleteGlossary($glossaryId); ``` -| param | Description | -|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| $glossaryId | Glossary uuid (set by DeepL when glossary is created) +| param | Description | +|-------------|-------------------------------------------------------| +| $glossaryId | Glossary uuid (set by DeepL when glossary is created) | List glossaries ```php @@ -157,9 +172,9 @@ $glossaryInformation = $deepl->glossaryInformation($glossaryId); var_dump($glossaryInformation); ``` -| param | Description | -|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| $glossaryId | Glossary uuid (set by DeepL when glossary is created) +| param | Description | +|-------------|-------------------------------------------------------| +| $glossaryId | Glossary uuid (set by DeepL when glossary is created) | Get glossary entries ```php @@ -169,9 +184,9 @@ foreach ($entries as $sourceLangText => $targetLangText) { } ``` -| param | Description | -|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| $glossaryId | Glossary uuid (set by DeepL when glossary is created) +| param | Description | +|-------------|-------------------------------------------------------| +| $glossaryId | Glossary uuid (set by DeepL when glossary is created) | ### Configuring cURL requests If you need to use a proxy, you can configure the underlying curl client to use one. You can also specify a timeout to avoid waiting for several minutes if Deepl is unreachable @@ -231,3 +246,5 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio [link-author]: https://github.com/Baby-Markt [link-contributors]: ../../contributors [link-deepl]: https://www.deepl.com/docs-api/introduction/ +[link-deepl-client-libraries]:https://www.deepl.com/en/docs-api/accessing-the-api/client-libraries/ +[link-deepl-php-lib-github]:https://github.com/DeepLcom/deepl-php diff --git a/composer.json b/composer.json index 9b0857a..fd3678d 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,8 @@ { + "abandoned": "deeplcom/deepl-php", "name": "babymarkt/deepl-php-lib", "type": "library", - "description": "DeepL API Client Library supporting PHP >= 7.3 && PHP < 8.1", + "description": "DeepL API Client Library supporting PHP >= 7.3 && PHP <= 8.1", "keywords": [ "babymarkt", "deepl", @@ -19,7 +20,7 @@ } ], "require": { - "php": ">=7.3 <8.1", + "php": ">=7.3 <8.2", "ext-json": "*", "ext-curl": "*" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b1e3381..9c1a135 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,17 @@ - - - - - tests/ - - - - - - src/ - - - - - - - \ No newline at end of file + + + + src/ + + + + + + + + tests/ + + + + diff --git a/src/Client.php b/src/Client.php index 18045df..3c918db 100644 --- a/src/Client.php +++ b/src/Client.php @@ -71,7 +71,7 @@ final class Client implements ClientInterface * @param integer $apiVersion * @param string $host */ - public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com') + public function __construct($authKey, $apiVersion = 2, $host = 'api-free.deepl.com') { $this->authKey = $authKey; $this->apiVersion = $apiVersion; @@ -215,7 +215,7 @@ public function buildQuery($paramsArray) } } - $body = http_build_query($paramsArray, null, '&'); + $body = http_build_query($paramsArray, '', '&'); if (isset($textString)) { $body = $textString . '&' . $body; diff --git a/src/DeepL.php b/src/DeepL.php index 212dd57..5eefe63 100644 --- a/src/DeepL.php +++ b/src/DeepL.php @@ -6,8 +6,6 @@ /** * DeepL API client library - * - * @package BabyMarkt\DeepL */ class DeepL { @@ -26,7 +24,7 @@ class DeepL */ private $client; - public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com', ClientInterface $client = null) + public function __construct($authKey, $apiVersion = 2, $host = 'api-free.deepl.com', ClientInterface $client = null) { $this->client = $client ?? new Client($authKey, $apiVersion, $host); } @@ -64,6 +62,7 @@ public function languages($type = null) * @param array|null $nonSplittingTags * @param null $outlineDetection * @param array|null $splittingTags + * @param string|null $glossaryId * * @return array * @@ -73,7 +72,7 @@ public function languages($type = null) */ public function translate( $text, - $sourceLang = 'de', + $sourceLang = '', $targetLang = 'en', $tagHandling = null, array $ignoreTags = null, @@ -82,7 +81,8 @@ public function translate( $preserveFormatting = null, array $nonSplittingTags = null, $outlineDetection = null, - array $splittingTags = null + array $splittingTags = null, + string $glossaryId = null ) { if (is_array($tagHandling)) { throw new InvalidArgumentException('$tagHandling must be of type String in V2 of DeepLLibrary'); @@ -99,6 +99,7 @@ public function translate( 'split_sentences' => $splitSentences, 'preserve_formatting' => $preserveFormatting, 'outline_detection' => $outlineDetection, + 'glossary_id' => $glossaryId ); $paramsArray = $this->removeEmptyParams($paramsArray); diff --git a/src/Glossary.php b/src/Glossary.php index b419ae6..d3e8b07 100644 --- a/src/Glossary.php +++ b/src/Glossary.php @@ -26,7 +26,7 @@ class Glossary * @param string $host * @param ClientInterface|null $client */ - public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com', ClientInterface $client = null) + public function __construct($authKey, $apiVersion = 2, $host = 'api-free.deepl.com', ClientInterface $client = null) { $this->client = $client ?? new Client($authKey, $apiVersion, $host); } diff --git a/tests/integration/DeepLApiTest.php b/tests/integration/DeepLApiTest.php index dffdc6b..166b5fe 100644 --- a/tests/integration/DeepLApiTest.php +++ b/tests/integration/DeepLApiTest.php @@ -22,6 +22,13 @@ class DeepLApiTest extends TestCase */ protected static $authKey = false; + /** + * DeepL Auth Key. + * + * @var string + */ + protected static $apiHost; + /** * Proxy URL * @var bool|string @@ -42,6 +49,7 @@ public static function setUpBeforeClass(): void parent::setUpBeforeClass(); $authKey = getenv('DEEPL_AUTH_KEY'); + $apiHost = getenv('DEEPL_HOST') ?: 'api-free.deepl.com'; $proxy = getenv('HTTP_PROXY'); $proxyCredentials = getenv('HTTP_PROXY_CREDENTIALS'); @@ -49,8 +57,9 @@ public static function setUpBeforeClass(): void return; } - self::$authKey = $authKey; - self::$proxy = $proxy; + self::$authKey = $authKey; + self::$apiHost = $apiHost; + self::$proxy = $proxy; self::$proxyCredentials = $proxyCredentials; } @@ -82,14 +91,16 @@ public function testTranslateSuccess() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); - $germanText = 'Hallo Welt'; - $expectedText = 'Hello world'; + $germanText = 'Hallo Welt, dies ist ein deutscher Text.'; + $expectedText = 'Hello world, this is a German text.'; + $expectedLanguage = 'DE'; $translatedText = $deepl->translate($germanText); self::assertEquals($expectedText, $translatedText[0]['text']); + self::assertEquals($expectedLanguage, $translatedText[0]['detected_source_language']); } /** @@ -101,14 +112,16 @@ public function testTranslateV1Success() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey, 1); + $deepl = new DeepL(self::$authKey, 1, self::$apiHost); - $germanText = 'Hallo Welt'; - $expectedText = 'Hello world'; + $germanText = 'Hallo Welt'; + $expectedText = 'Hallo Welt'; + $expectedLanguage = 'IT'; $translatedText = $deepl->translate($germanText); self::assertEquals($expectedText, $translatedText[0]['text']); + self::assertEquals($expectedLanguage, $translatedText[0]['detected_source_language']); } /** @@ -120,7 +133,7 @@ public function testTranslateWrongVersion() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } $germanText = 'Hallo Welt'; - $deepl = new DeepL(self::$authKey, 3); + $deepl = new DeepL(self::$authKey, 3, self::$apiHost); $this->expectException('\BabyMarkt\DeepL\DeepLException'); @@ -136,7 +149,7 @@ public function testTranslateTagHandlingSuccess() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $englishText = 'text to translate'; $expectedText = 'zu übersetzender Text'; @@ -160,7 +173,7 @@ public function testTranslateIgnoreTagsSuccess() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $englishText = 'text to do not translate

text to translate

'; $expectedText = 'text to do not translate

zu übersetzender Text

'; @@ -185,7 +198,7 @@ public function testUsage() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $response = $deepl->usage(); self::assertArrayHasKey('character_count', $response); @@ -201,7 +214,7 @@ public function testLanguages() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $response = $deepl->languages(); foreach ($response as $language) { @@ -219,7 +232,7 @@ public function testLanguagesSource() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $response = $deepl->languages('source'); foreach ($response as $language) { @@ -237,7 +250,7 @@ public function testLanguagesTarget() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $response = $deepl->languages('target'); foreach ($response as $language) { @@ -255,7 +268,7 @@ public function testLanguagesFail() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $this->expectException('\BabyMarkt\DeepL\DeepLException'); @@ -271,7 +284,7 @@ public function testTranslateWithAllParams() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $englishText = 'text to do not translate

please translate this text

'; $expectedText = 'text to do not translate

bitte übersetzen Sie diesen Text

'; @@ -303,13 +316,15 @@ public function testWithProxy() } if (self::$proxy === false) { - // The test would succeed with $proxy === false but it wouln't mean anything. + // The test would succeed with $proxy === false but it wouldn't mean anything. $this->markTestSkipped('Proxy is not configured.'); } - $deepl = new DeepL(self::$authKey); - $deepl->setProxy(self::$proxy); - $deepl->setProxyCredentials(self::$proxyCredentials); + $client = new Client(self::$authKey, 2, self::$apiHost); + $client->setProxy(self::$proxy); + $client->setProxyCredentials(self::$proxyCredentials); + + $deepl = new DeepL(self::$authKey, 2, self::$apiHost, $client); $englishText = 'please translate this text'; $expectedText = 'Bitte übersetzen Sie diesen Text'; @@ -345,7 +360,7 @@ public function testTranslateFormality() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $englishText = 'text to do not translate

please translate this text

'; $expectedText = 'Nicht zu übersetzender Text

Bitte übersetze diesen Text

'; @@ -370,10 +385,10 @@ public function testTranslateWithHTML() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $textToTranslate = array( 'Hello WorldThis should stay the same', - 'Another Text
new line

this is a paragraph

' + 'Another Text
new line.

This is a paragraph

' ); $expectedArray = array( @@ -383,7 +398,7 @@ public function testTranslateWithHTML() ), array( 'detected_source_language' => "EN", - 'text' => "Eine weitere
neue Textzeile

, die ein Absatz ist


", + 'text' => "Eine weitere
neue Textzeile.

Dies ist ein Absatz


", ), ); @@ -414,7 +429,7 @@ public function testTranslateWithNotSupportedSourceLanguage() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $this->expectException('\BabyMarkt\DeepL\DeepLException'); $deepl->translate('some txt', 'dk', 'de'); @@ -429,7 +444,7 @@ public function testTranslateWithNotSupportedDestinationLanguage() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new DeepL(self::$authKey); + $deepl = new DeepL(self::$authKey, 2, self::$apiHost); $this->expectException('\BabyMarkt\DeepL\DeepLException'); $deepl->translate('some txt', 'en', 'dk'); diff --git a/tests/integration/GlossaryTest.php b/tests/integration/GlossaryTest.php index dc98871..86b1329 100644 --- a/tests/integration/GlossaryTest.php +++ b/tests/integration/GlossaryTest.php @@ -15,6 +15,13 @@ class GlossaryTest extends TestCase */ protected static $authKey = false; + /** + * DeepL Auth Key. + * + * @var string + */ + protected static $apiHost; + /** * Setup DeepL Auth Key. */ @@ -23,17 +30,19 @@ public static function setUpBeforeClass(): void parent::setUpBeforeClass(); $authKey = getenv('DEEPL_AUTH_KEY'); + $apiHost = getenv('DEEPL_HOST') ?: 'api-free.deepl.com'; if ($authKey === false) { - return; + self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } self::$authKey = $authKey; + self::$apiHost = $apiHost; } public static function tearDownAfterClass(): void { - $glossaryAPI = new Glossary(self::$authKey); + $glossaryAPI = new Glossary(self::$authKey, 2, self::$apiHost); $glossariesResponse = $glossaryAPI->listGlossaries(); @@ -62,7 +71,7 @@ public function testCreateGlossary() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new Glossary(self::$authKey); + $deepl = new Glossary(self::$authKey, 2, self::$apiHost); $entries = ['Hallo' => 'Hello']; $glossary = $deepl->createGlossary('test', $entries, 'de', 'en'); @@ -80,7 +89,7 @@ public function testListGlossaries() self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new Glossary(self::$authKey); + $deepl = new Glossary(self::$authKey, 2, self::$apiHost); $glossaries = $deepl->listGlossaries(); self::assertNotEmpty($glossaries['glossaries']); @@ -97,7 +106,7 @@ public function testGlossaryInformation($glossaryId) self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new Glossary(self::$authKey); + $deepl = new Glossary(self::$authKey, 2, self::$apiHost); $information = $deepl->glossaryInformation($glossaryId); self::assertArrayHasKey('glossary_id', $information); @@ -114,7 +123,7 @@ public function testGlossaryEntries($glossaryId) self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new Glossary(self::$authKey); + $deepl = new Glossary(self::$authKey, 2, self::$apiHost); $entries = $deepl->glossaryEntries($glossaryId); self::assertEquals($entries, ['Hallo' => 'Hello']); @@ -131,7 +140,7 @@ public function testDeleteGlossary($glossaryId) self::markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); } - $deepl = new Glossary(self::$authKey); + $deepl = new Glossary(self::$authKey, 2, self::$apiHost); $response = $deepl->deleteGlossary($glossaryId); self::assertNull($response); diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 2d0a0e6..0ce35f4 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -203,7 +203,7 @@ public function testBuildBaseUrlHost() public function testBuildBaseUrl() { $authKey = '123456'; - $expectedUrl = 'https://api.deepl.com/v2/translate'; + $expectedUrl = 'https://api-free.deepl.com/v2/translate'; $testSubject = new Client($authKey); $result = $testSubject->buildBaseUrl();