Text translation using DeepL API

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!

Comments

Share Your Thoughts

Join the conversation!

Scroll to Top

Share Your Thoughts

Thank you for your comment!

Your email address will not be published. Required fields are marked *

Pro

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Standard

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Basic

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Contact us

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Fill out the form below, and we’ll get back to you within 24 hours