LuisHumanoide / DeepLy

PHP client for the DeepL.com translation API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DeepLy

Build Status GitHub license Build Status

DeepL.com is a great, new translation service. It provides better translations compared to other popular translation engines. DeepLy is a PHP package that implements a client to interact with DeepL via their undocumented API.

Installation

Through Composer:

composer require chriskonnertz/deeply

From then on you may run composer update to get the latest version of this library.

It is possible to use this library without using Composer but then it is necessary to register an autoloader function.

This library requires PHP 5.6 or higher and the cURL extension.

Usage Example

$deepLy = new ChrisKonnertz\DeepLy\DeepLy();

$translatedText = $deepLy->translate('Hello world!', 'DE', 'EN');
    
echo $translatedText; // Prints "Hallo Welt!"

There is a PHP demo script included. It is located at dev/demo.php.

Sophisticated Example

use ChrisKonnertz\DeepLy\DeepLy;

$deepLy = new DeepLy();

try {
    $translatedText = $deepLy->translate('Hello world!', DeepLy::LANG_EN, DeepLy::LANG_AUTO);
    
    echo $translatedText; // Prints "Hallo Welt!"
} catch (\Exception $exception) {
    echo $exception->getMessage();
}

Always wrap calls of the translate method in a try-catch-block, because they might throw an exception if the arguments are invalid or the API call fails. Instead of using hardcoded strings as language arguments better use the language code constants of the DeepLy class. The class also offers methods such as getLangCodes($withAuto = true) and supportsLangCode($langCode).

You may use the proposeTranslations method if you want to get alternative translations for a text. This method cannot operate on more than one sentence at once.

Supported Languages

DeepL(y) supports these languages:

Code Language
auto Auto detect
DE German
EN English
FR French
ES Spanish
IT Italian
NL Dutch
PL Polish

Note that auto detection only is possible for the source language.

HTTP Client

Per default DeepLy uses a minimalistic HTTP client based on cURL. If you want to use a different HTTP client, such as Guzzle, create a class that implements the HttpClient\HttpClientInterface and makes use of the methods of the alternative HTTP client. Then use $deepLy->setHttpClient($yourHttpClient) to inject it.

Note: If you experience issues with the integrated cURL client that could be solved by setting the CURLOPT_SSL_VERIFYPEER to false, first read this: snippets.webaware.com.au/../

If it does not help try: $deepLy->getHttpClient()->setSslVerifyPeer(false)

Text Length Limit

According to the DeepL.com website, the length of the text that has to be translated is limited to 5000 characters.

Current State

I do not know if or when DeepL.com will officially release their API but I expect them to do it at some point. Meanwhile you may use this PHP client on your own risk. Also note that their API responds quite slow. This might be intentional. Nevertheless the API is reliable. I had not a single issue amongst hundreds of API calls.

I tried to rush towards a first release. It works and I hope that I do not have to change the main method translate() in a way that makes it incompatible with the current release. However, I cannot guarantee that. More commits might come soon. There still is space for improvements.

Disclaimer

This is not an official package. It is 100% open source and non-commercial. The API of DeepL.com is free as well but this might change in the future.

DeepL is a product from DeepL GmbH. More info: deepl.com/publisher.html

This package has been heavily inspired by node-deepls and deeplator. Thank you for your great work!

General Notes

  • The code of this library is formatted according to the code style defined by the PSR-2 standard.

  • Status of this repository: Maintained. Create an issue and you will get a response, usually within 48 hours.

About

PHP client for the DeepL.com translation API

License:MIT License


Languages

Language:PHP 100.0%