Tailwind Logo

Using the API to perform machine translation - DeepL

Related Technology

Published: 2024-08-30

Recently, it has become possible to use AI engines to translate as well. It is also possible to translate through an API rather than through an app or chat format. In this article, we will check how a translation service works using the DeepL API.

DeepL translation

DeepL, the machine translation service we use on this blog, allows you to translate using its API. Let's check out the procedure for translating via the API using this service.

The DeepL API allows you to translate up to 500,000 characters per month for free. You will need to register your credit card details, etc., but you will not be charged unless you change your plan yourself.

Create an account

To create an account, first go to the website and click on the “Start free trial” button in the top right corner to go to the sign-up page. There are three services to choose from, but for this time, please select the DeepL API. The URL when writing this blog is as follows.

deepl01.png

By clicking on DeepL API Free, you can apply for the API service.

To create an account, you will need to register your address and credit card details, but please note that the credit card details are only used to enable you to switch to the paid version and are also used for identity verification, so please do not worry.

After you have actually completed the account creation process, you can check your API key from the account screen and obtain it. Your account is now ready.

deepl02.png

Get the Postman Collection

Postman is useful for checking how things work via the API. And there is also a page in the DeepL developer documentation that explains how to use Postman.

If you click the Run in Postman button on this page, the screen will change to the following.

deepl03.png

This time, click the Fork Collection button instead of the Copy button, and log in with your Postman account. Once you have logged in, you can specify the name of the collection. In this case, we set it up as follows.

deepl04.png

Once you have completed this, you will be able to access it from Postman as follows.

deepl05.png

Run a translation from Postman

Base Settings

In this article, we will perform basic translation using Postman. First, when setting the API key, select the DeepL API in the root, and check the Variables section. The first time you do this, it will look like the following.

deepl06.png

As this is the free version, the URL for the API integration destination is different, and you will need to select https://api-free.deepl.com/v2. Also, please obtain an API key from the DeepL management screen and make the necessary settings. Your preparations are now complete.

Run the sample

Once you have completed the above settings, try running a sample translation. If the values are correct, the sample will return the results as follows.

deepl07.png

Change translation request

For the sample translation, we used application/x-www-form-urlencoded, and we obtained the translation results by setting values for the items in Postman. When we check the Translate text page, there is a sample that passes the data in JSON format to obtain the results, so we will create a sample in this format.

First, copy the existing Request Translation sample using Duplicate to create a Request Translation JSON. Leave the Header fields blank, and set the following JSON in the Body.

JSON
{
  "text": [
    "Hello, world!"
  ],
  "target_lang": "JA"
}

Even with the data format of the Body being JSON, I was able to get the results when I ran it in this situation.

deepl08.png

Translate HTML data

What happens when you try to translate data that contains HTML tags rather than text data? As sample data, we will pass the HTML sample data from Sitecore in Json format. We specify the original language and target language.

JSON
{
    "text": [
        "<p style=\"line-height: 22px;\">From a single connected platform that also integrates with other customer-facing platforms, to a single view of the customer in a big data marketing repository, to completely eliminating much of the complexity that has previously held marketers back, the latest version of Sitecore makes customer experience highly achievable. Learn how the latest version of Sitecore gives marketers the complete data, integrated tools, and automation capabilities to engage customers throughout an iterative lifecycle &ndash; the technology foundation absolutely necessary to win customers for life.</p><p>For further information, please go to the <a href=\"https://doc.sitecore.net/\" target=\"_blank\" title=\"Sitecore Documentation site\">Sitecore Documentation site</a></p>"
    ],
    "source_lang": "EN",
    "target_lang": "JA"
}

The result will be returned as follows.

JSON
{
    "translations": [
        {
            "detected_source_language": "EN",
            "text": "<p style=\"line-height: 22px;\">他の顧客対応プラットフォームとも統合できる単一の接続プラットフォームから、ビッグデータ マーケティング リポジトリにおける顧客の単一ビューまで、これまでマーケティング担当者の足かせとなっていた複雑性の多くを完全に排除したSitecoreの最新バージョンは、カスタマーエクスペリエンスを実現します。</p><p>詳細については、<a href=\"https://doc.sitecore.net/\" target=\"_blank\" title=\"Sitecoreドキュメンテーションサイト\">Sitecoreドキュメンテーションサイト</a></p>をご覧ください。"
        }
    ]
}

The above results can be used as they are, but since DeepL provides the parameter tag_handling, which can be used to handle the translation as HTML, we will try using this.

JSON
{
  "text": [
    "<p style=\"line-height: 22px;\">From a single connected platform that also integrates with other customer-facing platforms, to a single view of the customer in a big data marketing repository, to completely eliminating much of the complexity that has previously held marketers back, the latest version of Sitecore makes customer experience highly achievable. Learn how the latest version of Sitecore gives marketers the complete data, integrated tools, and automation capabilities to engage customers throughout an iterative lifecycle &ndash; the technology foundation absolutely necessary to win customers for life.</p><p>For further information, please go to the <a href=\"https://doc.sitecore.net/\" target=\"_blank\" title=\"Sitecore Documentation site\">Sitecore Documentation site</a></p>"
  ],
  "tag_handling": "html",
  "source_lang": "EN",
  "target_lang": "JA"
}

The results came back in a slightly different format as follows. The sentences within the tags are not translated and are maintained as they are.

JSON
{
    "translations": [
        {
            "detected_source_language": "EN",
            "text": "<p style=\"line-height: 22px;\">他の顧客対応プラットフォームとも統合できる単一の接続プラットフォームから、ビッグデータ マーケティング リポジトリにおける顧客の単一ビューまで、これまでマーケティング担当者の足かせとなっていた複雑性の多くを完全に排除したSitecoreの最新バージョンは、カスタマーエクスペリエンスを高度に実現します。Sitecoreの最新バージョンにより、マーケティング担当者が反復的なライフサイクルを通じて顧客をエンゲージするための完全なデータ、統合ツール、自動化機能、つまり生涯顧客を獲得するために絶対に必要なテクノロジー基盤がどのように提供されるかをご覧ください。</p><p>詳細については、<a href=\"https://doc.sitecore.net/\" target=\"_blank\" title=\"Sitecore Documentation site\">Sitecoreドキュメントサイトを</a>ご覧ください。</p>"
        }
    ]
}

In some cases, it is better to treat it as HTML because the tag values may change.

Summary

This time, we passed JSON data to DeepL to check how the HTML translation works. We were able to confirm that the translation was correct even with tags, so we will start working on a demo that uses this to link with a CMS.

Tags