scn/deepl-api-connector

Unofficial PHP Client for the API of deepl.com

3.1.0 2022-09-20 11:48 UTC

This package is auto-updated.

Last update: 2022-09-20 11:51:35 UTC


README

Monthly Downloads License Build Status

Requirements

Compatibility

Connector-Version PHP-Version(s)
master (dev) TBD
3.x (features and bugfixes) 7.4, 8.0, 8.1
2.x (bugfixes only) 7.3, 7.4, 8.0, 8.1
1.x (EOL) 7.2, 7.3, 7.4

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);
        
    .......
    
    OR
    
    $translationObject = $deepl->translate('some text', \Scn\DeeplApiConnector\Model\TranslationConfigInterface::LANGUAGE_DE);
}

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

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);
    
    if ($translationStatus->getStatus() === \Scn\DeeplApiConnector\Enum\FileStatusEnum::FILE_TRANSLATION_DONE)
    {
    ....
    }
}

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();
    .....
}

Testing

$ composer test

Credits

License

The MIT License (MIT). Please see License File for more information.