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 and without an API key. Please switch to DeepLy 2 if you have an API key.
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!"
An interactive PHP demo script is included. It is located at
demos/demo_translate.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.
DeepLy has a method that uses the DeepL API to detect the language of a text:
$languageCode = $deepLy->detectLanguage('Hello world!');
This will return 'EN'. The language of the text has to be one of the supported languages or the result will be incorrect.
If you do not need the code of the language but its name, you may call the $deepLy->getLangName($langCode)
method.
The API in general can handle and completely translate texts that contain parts with different languages,
if the language switch is not within a sentence. The detectLanguage()
method will however
only return the code of one language. It will throw an exception if it is unable to auto-detect the language.
This will rarely happen, it is more likely that the API will return a "false positive": It will rather detect the wrong
language than no language at all.
An interactive PHP demo script is included. It is located at
demos/demo_detect.php
.
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.
DeepL says they will add more languages in the future, such as Chinese and Russian.
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 this 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();
There are several demo scripts included in the demos
folder:
demo_detect.php
: Demonstrates language detection. Write a text and the API will tell you which language it thinks it is.demo_translate.php
: Demonstrates language translation. Write a text and the API will try to translate it to a language of your choice.demo_split.php
: Demonstrates sentence detection. Write a text and the API will split it into sentences.demo_ping.php
: Demonstrates DeepLy'sping()
method by pinging the API.
There is a request limit. The threshold of this limit is unknown.
The "core" of this library consists of these classes:
DeepLy
- main classHttpClient\CurlHttpClient
- HTTP client classProtocol\JsonRpcProtocol
- JSON RPC is the protocol used by the DeepL APIResponseBag\AbstractBag
- base wrapper class for the responses of the DeepL APIResponseBag\SentencesBag
- concrete class for API responses to "split text" requestsResponseBag\TranslationBag
- concrete class for API responses to "translate" requests
There are also some exception classes, interfaces, an alternative HTTP client implementation that uses Guzzle and classes for the Laravel integration.
DeepL.com has officially released their API. They offer a premium service that includes access to this API. However, it is still possible (at your own risk) to access the API without using the premium service.
Premium API access requires authentication with an API key. This will be supported by Deeply 2.
This is not an official package. It is 100% open source and non-commercial. The API of DeepL.com can be accessed without an API key but this might change in the future.
DeepL is a product of 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.
-
Texts have to be UTF8-encoded.
-
If you are looking for a real-world example application that uses DeepLy, you may take a look at Translation Factory.
-
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.