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.
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.
$deepLy = new ChrisKonnertz\DeepLy\DeepLy();
$translatedText = $deepLy->translate('Hello world!', 'DE', 'EN');
echo $translatedText; // Prints "Hallo Welt!"
There is an interactive PHP demo script included. It is located at
demos/demo_interactive.php
.
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.
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.
According to the DeepL.com website, the length of the text that has to be translated is limited to 5000 characters.
Per default DeepLy will throw an exception if the length limit is exceeded.
You may call $deepLy->setValidateTextLength(false)
to disable that validation.
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
tofalse
, first read this: snippets.webaware.com.au/../If it does not help try:
$deepLy->getHttpClient()->setSslVerifyPeer(false)
If you want to use Guzzle as the HTTP client: Support for Guzzle is available out-of-the-box. Make sure you have installed Guzzle (preferably via Composer), then copy this code and paste it right after you instantiate DeepLy:
$protocol = $deepLy->getProtocol();
$httpClient = new \ChrisKonnertz\DeepLy\HttpClient\GuzzleHttpClient($protocol);
$deepLy->setHttpClient($httpClient);
DeepLy comes with support for Laravel 5.x and since it also supports package auto-discovery it will be auto-detected in Laravel 5.5.
In Laravel 5.0-5.4 you manually have to register the service provider
ChrisKonnertz\DeepLy\Integrations\Laravel\DeepLyServiceProvider
in the "providers" array and the facade
ChrisKonnertz\DeepLy\Integrations\Laravel\DeepLyFacade
as an alias in the "aliases" array
in your config/app.php
config file.
You can then access DeepLy like this: $ping = \DeepLy::ping();
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.
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 will 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! Give these implementations a try if you are coding in Node.js or Python.