scn / deepl-api-connector
Unofficial PHP Client for the API of deepl.com
Installs: 225 986
Dependents: 8
Suggesters: 0
Security: 0
Stars: 15
Watchers: 9
Forks: 12
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- php-http/discovery: ^1.13
- php-http/multipart-stream-builder: ^1.1
- psr/http-factory: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- guzzlehttp/guzzle: ^7.0
- nyholm/psr7: ^1.4
- phpstan/phpstan: ^1.4
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-strict-rules: ^1.1
- phpunit/phpunit: ^9
README
- Information about Deepl: https://www.deepl.com
- Deepl API Documentation: https://www.deepl.com/api.html
Requirements
- php (See the compatibility table below for supported php versions)
- Implementations of PSR17 (Http-Factories) (Available packages) and PSR18 (Http-Client) (Available packages)
- A deepl free/pro api key
Compatibility
Install
Via Composer
$ composer require scn/deepl-api-connector
Usage
Api client creation
The DeeplClientFactory
supports auto-detection of installed psr17/psr18 implementations.
Just call the create
method and you are ready to go
require_once __DIR__ . '/vendor/autoload.php'; use \Scn\DeeplApiConnector\DeeplClientFactory; $deepl = DeeplClientFactory::create('your-api-key');
Optionally, you can provide already created instances of HttpClient, StreamFactory and RequestFactory as params to the create method.
require_once __DIR__ . '/vendor/autoload.php'; use \Scn\DeeplApiConnector\DeeplClientFactory; $deepl = DeeplClientFactory::create( 'your-api-key', $existingHttpClientInstance, $existingStreamFactoryInstance, $existingRequestFactoryInstance, );
If a custom HTTP client implementation is to be used, this can also be done via the DeeplClientFactory::create method. The Client must support PSR18.
Get Usage of API Key
require_once __DIR__ . '/vendor/autoload.php'; $deepl = \Scn\DeeplApiConnector\DeeplClientFactory::create('your-api-key'); try { $usageObject = $deepl->getUsage(); }
Get Translation
require_once __DIR__ . '/vendor/autoload.php'; $deepl = \Scn\DeeplApiConnector\DeeplClientFactory::create('your-api-key'); try { $translation = new \Scn\DeeplApiConnector\Model\TranslationConfig( 'My little Test', \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_DE ..., ..., ); $translationObject = $deepl->getTranslation($translation); }
require_once __DIR__ . '/vendor/autoload.php'; $deepl = \Scn\DeeplApiConnector\DeeplClientFactory::create('your-api-key'); try { $translation = new \Scn\DeeplApiConnector\Model\TranslationConfig( 'My little Test', \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_DE ); $translationObject = $deepl->translate('some text', \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_DE); }
Optional you also can translate a batch of texts as once, see example/translate_batch.php
Add File to Translation Queue
require_once __DIR__ . '/vendor/autoload.php'; $deepl = \Scn\DeeplApiConnector\DeeplClientFactory::create('your-api-key'); try { $fileTranslation = new \Scn\DeeplApiConnector\Model\FileTranslationConfig( file_get_contents('test.txt'), 'test.txt', \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_EN, \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_DE ); $fileSubmission = $deepl->translateFile($fileTranslation); $fileSubmission->getDocumentId() }
Check File Translation Status
All translation states are available in FileStatusEnum
require_once __DIR__ . '/vendor/autoload.php'; $deepl = \Scn\DeeplApiConnector\DeeplClientFactory::create('your-api-key'); try { $fileTranslation = new \Scn\DeeplApiConnector\Model\FileTranslationConfig( file_get_contents('test.txt'), 'test.txt', \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_EN, \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_DE ); $fileSubmission = $deepl->translateFile($fileTranslation); $translationStatus = $deepl->getFileTranslationStatus($fileSubmission); }
Get Translated File Content
require_once __DIR__ . '/vendor/autoload.php'; $deepl = \Scn\DeeplApiConnector\DeeplClientFactory::create('your-api-key'); try { $fileTranslation = new \Scn\DeeplApiConnector\Model\FileTranslationConfig( file_get_contents('test.txt'), 'test.txt', \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_EN, \Scn\DeeplApiConnector\Enum\LanguageEnum::LANGUAGE_DE ); $fileSubmission = $deepl->translateFile($fileTranslation); $file = $deepl->getFileTranslation($fileSubmission); echo $file->getContent(); }
Retrieve supported languages
See example/retrieve_supported_languages.php
Working with glossaries
Testing
$ composer test
Credits
License
The MIT License (MIT). Please see License File for more information.