Text translation using DeepL API

by | Oct 20, 2023

Quite often, websites have several languages. In this article, I will describe how to translate text using the DeepL API for automatic text translation.

Here we will describe a method that doesn’t involve the use of plugins or third-party libraries. Clear code using cURL PHP. This method is suitable for both WordPress developers and clear PHP sites.

How to translate text with DeepL API?

You need DeepL account with Free or Pro plan.

You can see the difference between the plans in the following screenshot. Prices and tariffs are current at the time of publication.

You can check the current plans and the list of countries where the DeepL API works by following the link.

Create a translation function

Let’s create a function that sends a request to DeepL and returns a translated text in the selected language.

Let this be a deepLWG function that accepts the parameters text and lang.

Text – is the actual text you want to translate.
Lang – is the language you want to translate the text into.

				
					function deepLWG($text, $lang){
    $key = 'deepl-api-key';
    $string = "auth_key=$key&text=$text&target_lang=$lang";
    $ch = curl_init();
    $apiLink = 'https://api.deepl.com/v2/translate';
    // $apiLink = 'https://api-free.deepl.com/v2/translate'; //if you use free account
    curl_setopt($ch, CURLOPT_URL, $apiLink);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $string);

    $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);
    return json_decode($result)->translations[0]->text ? json_decode($result)->translations[0]->text : $text;
}
				
			

$key – is your key to the DeepL API. You receive it after registering your account. Depending on whether you have a Pro or Free plan, you specify the appropriate url. This is for the Pro version:

				
					$apiLink = 'https://api.deepl.com/v2/translate';
				
			

For the Free version, you need to use this url:

				
					$apiLink = 'https://api-free.deepl.com/v2/translate';
				
			

The last line of the function:

				
					return json_decode($result)->translations[0]->text ? json_decode($result)->translations[0]->text : $text;
				
			

Checks, using a ternary operator, whether we received the translated text in response. If so, the function returns it, and if not, it returns the previously sent untranslated text.

If we call a function. For example:

				
					$dpl = deepLWG('Привіт Світ!', 'EN');
				
			

In return, we will get Hello World!

Newsletter

Sign up for latest news, tips and learnings from ConversionLab.

Copyright 2020 ConversionLab

Sign up to our newsletter

Get a free guide to improving conversion rates

By submitting this form you agree to our Privacy Policy.
By submitting this form you agree to our Privacy Policy.
By continuing to use this site you consent to the use of cookies in accordance with our Privacy Policy.