Giter Site home page Giter Site logo

deeply's Introduction

DeepLy

Build Status GitHub license Version

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 an interactive PHP demo script included. It is located at demos/demo_interactive.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.

Text Length Limit

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.

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)

Guzzle

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);

Framework Integration

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

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.

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! Give these implementations a try if you are coding in Node.js or Python.

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.

deeply's People

Contributors

chriskonnertz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.