Using the Deepl API

Using the Deepl API

Written by SailorEudes on Aug 19th, 2020 Views Report Post

Deepl Good morning, and welcome to an exciting and new tutorial. This tutorial is also available in its French and original version here.

Today, I'm back to share with you a script I created, on which I spent 1 hour, and which turned out to be non-existent on the web.

Introduction

Yes, today, you will learn to integrate the Deepl API in PHP/JSON on your website, which will allow you to offer your users the possibility to translate your website cleanly and in the respect of the art.

This tutorial follows another tutorial that I had presented in the past on HabboDev, and which remains accessible in French version (for the moment) here.

We can now begin!

Presentation

Deepl, what's that?

DeepL is an online machine translation service from DeepL GmbH, which was launched on 28 August 2017 by the Linguee team. The service allows the translation of eleven languages in 110 language-to-language combinations (German, English, French, Dutch, Polish, Russian, Italian, Spanish, Portuguese, Japanese, Simplified Chinese). This service uses convolutional neural networks built on the Linguee database. At the time of publication, DeepL was reportedly outperforming its competitors in blind tests, including Google Translation, Microsoft Translation and Facebook. It would also be more accurate and more nuanced for the same speed as its competitors. Since December 2018, translations into Russian and Portuguese are also available. On 19 March 2020, translations into Simplified Chinese and Japanese were added. Its principle is based on an indexing robot for collecting translations already existing on websites. In August 2018, the 20 million user mark is crossed. As of 12 May 2019, 21.8% of traffic comes from Germany, 17.4% from France, 10% from Spain, 7.9% from Switzerland and 4.1% from Poland.

Source: Wikipedia

Documentation A complete and official documentation is available at this link: https://www.deepl.com/docs-api/

Integration

To be able to integrate the Deepl API into your website, you will need a minimum of knowledge in the fields of tables, curl and/or httprequest.

Moreover, the bad news is that you will need a "Developer" account in order to have access to an authentication key that will allow you to use the API on your website. Unfortunately, the subscription costs 5€/month then 20,00 € for 1 000 000 translated characters (you have some margin).

Then, the good news is that Deepl offers 1 month FREE to all new users, which will allow you to at least test the API, train yourself, maybe even use it afterwards for potential customers (if you are freelance) or simply for the future company you will work for.

Tariff link: Deepl Plans

Finally, you are not obliged to use this API, as there are many other ways to offer a translation system to your future Internet users, but you should only take the positive out of this tutorial, as, in addition to taking you out of your comfort zone, it will teach you the basics of the code that will be important for you to continue your studies and/or your learning in development.

CURL, and TABLES are fundamentals in PHP.

A person claiming to be a "Developer" and not knowing how to use them is a person who is doomed to failure in this area.

We can begin!

Step I - Choose a method You will already have to make a choice between the CURL method and the HTTPREQUEST method to be able to consume your API.

Curl comes with PHP, HTTPRequest is a separate PECL extension. This makes it much more likely that CURL will be installed on your target platform, which is about the deciding factor for most projects, and for this tutorial.

Step II - Convert CURL and/or HTTPREQUEST to PHP

If you are more experienced, I leave you the possibility to do it your own way, here is the information to consume for 1 word to be translated:

- Curl Method

curl https://api.deepl.com/v2/translate \ 
-d auth_key=[yourAuthKey] \ 
-d "text=Hello, world"  \ 
-d "target_lang=FR"

- HTPPREQUEST method

POST /v2/translate?auth_key=[yourAuthKey]> HTTP/1.0
Host: api.deepl.com
User-Agent: YourApp
Accept: */*
Content-Length: 54
Content-Type: application/x-www-form-urlencoded
auth_key=[yourAuthKey]&text=Hello, world&target_lang=FR

If you are less experienced, we will start with the CURL method and convert it to PHP :

- Curl Method

curl https://api.deepl.com/v2/translate \ 
-d auth_key=[yourAuthKey] \ 
-d "text=Hello, world"  \ 
-d "target_lang=FR"

You're going to use an already existing converter thanks to curl-to-PHP, and paste the above code in the box provided (it's magic, and rather fast ;)).

You should get this result:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.deepl.com/v2/translate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "auth_key=[yourAuthKey]&text=Hello, world&target_lang=FR");

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Step III - Setting up the PHP code If you have a good understanding of English, and a minimum level of competence in this field, you should already have found the parameters to be modified in the code. If not, I'll enlighten you:

Replace : [yourAuthKey]

By: Your authentication key which you can view here after you have registered, logged in and subscribed to the "Developer" subscription.

Then:

Replace: Hello, world

By: A word you wish to translate

Then:

Replace: FR

By: The ALPHA 2 code of the language you want to translate your word into

There you go, you've finished setting your code!

Step IV - Convert JSON to GROSS

And now, you'll have to display your translated word, and for that, you'll have to play with JSON and PHP :

<?php

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.deepl.com/v2/translate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "auth_key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx&text=Hello, world&target_lang=FR");

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

$translatedWords = json_decode($result, true); // Decode the word
$result = $translatedWords['translations'][0]['text']; // Search the word

echo $result; // Display the word

There you go, your site is well connected to Deepl!

Screen

Screen 1

Download

And finally, if you were lazy enough to follow the tutorial and you prefer live, here is the link to download the code.

Sailor, this subject is finally coming to an end!

I hope you'll find it useful, and that you'll offer DevDojo users an improved version of this script to make it last in time!

Click to access an alternative of this script.

In the meantime, I wish you a good continuation, and a good courage for the future, and may the sea be with you !

Comments (0)