Skip to main content

DeepL API auto tranlation

Overview

With Kuroco, you can add custom function to the management screen. In this tutorial, we will use the DeepL API as an example to explain how to automatically translate newly added content and register it as content in the secondary languages.

By using the DeepL API, it is possible to translate only the text while maintaining the HTML tag structure. Additionally, we will use the terminology feature of DeepL to fine-tune the translation results.

What you'll learn

You will implement an automatic translation feature using the following steps:

Prerequisites

info

This feature is created based on the DeepL API documentation. Please refer to the official documentation - DeepL API for the specifications of the DeepL API.

Obtaining and Registering an Authentication Key

Applying for the DeepL API and obtaining an authentication key

First, access the DeepL API and register with DeepL.

If your translation volume is up to 500,000 characters per month, you can use the free version.

Image from Gyazo

After completing the registration, access the DeepL Pro account information page. The authentication key to be used with the DeepL API is displayed at the bottom of the account information page. Copy this key.

Image from Gyazo

Registering the Authentication Key in the Kuroco Management Screen

Access the Kuroco management screen and In the left sidebar menu, under "SETTINGS", click [Environment] -> [Secrets].

Image from Gyazo

Click on [Add].

Image from Gyazo

Enter the following information and click on [Add].

FieldValue
NameDEEPL_API_KEY
ValueThe authentication key to be used with the DeepL API

Image from Gyazo

You have now completed the preparation to use the DeepL API.

Setting up Multiple Languages

In the left sidebar menu, under "SETTINGS", click [Environment] -> [Localization].

Image from Gyazo

In this tutorial, we will set Japanese [ja] as the Primary language and English [en] as the Secondary languages. Make the following settings and click on [Update].

FieldValue
EnableCheck the box
Primary languageJapanese [ja]
Secondary languagesEnglish[en]

Image from Gyazo

You have now completed the setup for multiple languages.

Creating Content Definitions

Register content definitions for tuning the results of translation and for the content to be translated automatically. Click [Add] on the content definition list page to create the following two content definitions.

Glossary

By using a glossary, you can specify how terms are translated, allowing you to create consistent translations without the need for extensive editing afterward.
https://www.deepl.com/en/blog/translate-your-way-with-the-deepl-glossary

To manage DeepL's glossary in Kuroco's content, we will create a content definition for the glossary. In the content definition for glossary, register a repeating item for the pair of Japanese and English, and a text item to register the glossary ID of DeepL. It will be as follows.

IDParent fieldField NameField Settings
1Parent Field Name: Language pairs
Parent Field Identifier: glossary
Repetition: 30
Field Name: JA
Identifier: source_lang
Text
2Select JAField Name: EN
Identifier: target_lang
Text
3None selectedField Name: glossary_id
Identifier: glossary_id
Text

Image from Gyazo

Image from Gyazo

tip

Group settings cannot be made when adding content definitions, so please set the parent item and repetition count after adding each item.

tip

The default maximum value for the repetition count of items is 30, but it can be changed up to 99 in the management screen.

Also, since the glossary is used by updating a single content, register one original content in advance.

Click [Add] on the content list page.

Image from Gyazo

Register as follows for example. The language pair set here specifies how to translate terms when sending translation requests to DeepL.

For example, the word "記事" is usually translated as "Article", but with this, you can specify to translate it as "Content".

ItemValue
Slugglossary
TitleGlossary
Language pairs 1JA:記事
EN:Content
Language pairs 2JA:副言語
EN:Secondary languages
glossary_idBlank

Image from Gyazo

After entering the information, click [Add] to register the content.

Auto-Translated Content

For the content definition to be translated automatically, set it as a simple configuration with only Wysiwyg. It will be as follows.

IDParent fieldField NameField Settings
1No selectionField Name: WYSIWYG
Identifier: source_text
WYSIWYG

Image from Gyazo

Image from Gyazo

Creating an API

Create API for internal processing

It is recommended to separate APIs for endpoints that are only used inside Kuroco. So, first, create a new API for internal use. If it has already been added, you can proceed to the next step.

Creating APIs

Click [Add] on the API page of the Kuroco admin panel.

Image from Gyazo

Enter the following in the editor and click [Add].

Image from Gyazo

ItemSettings
TitleInternal
Version1.0
DescriptionAPI for internal use

The new API has been created.

Image from Gyazo

Security settings

Next, we set up the security. Click [Security].

Image from Gyazo

Select [Dynamic access token] and click [Save].

Image from Gyazo

After setting the security to [Dynamic access token], the message "Since this API is configured to use token, it is recommended to add an endpoint that implements Authentication::Login::token." will be shown. Please ignore it since the API is only for internal use.

Image from Gyazo

configure CORS

Next, we configure CORS. Click [Operation CORS].

Image from Gyazo

Click [Add origin] of CORS_ALLOW_ORIGINS and enter the following:

  • Admin panel URL

Click [Add Method] of CORS_ALLOW_METHODS and enter the followings:

  • GET
  • POST
  • OPTIONS

Confirm that [Allow Credentials] of CORS_ALLOW_CREDENTIALS is checked.

Image from Gyazo

If there are no problems, click [Save].

Creating Endpoints

Create three endpoints: "Endpoint to get glossary", "Endpoint to update glossary", and "Endpoint to update secondary language".

Click on [Add new endpoint] from the Internal API and create each endpoint.

Image from Gyazo

Endpoint to get glossary

FieldSetting
Pathget_glossary
CategoryContents
ModelTopics
Operationdetails
topics_group_idContent definition ID of the glossary (8)
ext_groupCheck the box

Image from Gyazo

Image from Gyazo

Endpoint to update glossary

FieldSetting
Pathupdate_glossary
CategoryContents
ModelTopics
Operationupdate
topics_group_idContent definition ID of the glossary (8)

Image from Gyazo

Image from Gyazo

Endpoint to update secondary language

FieldSetting
Pathupdate_content
CategoryContents
ModelTopics
Operationupdate
topics_group_idContent definition ID of the automatic translation content (9)

Image from Gyazo

Image from Gyazo

Creating Custom Functions

Once the content and endpoints are set up, we will write custom function to integrate with DeepL for translation.

In the left sidebar menu, click [Operation] -> [Custom function]

Image from Gyazo

Click on [Add] and create two custom functions: "Custom function to register/delete glossary" and "Custom function to translate content and register in secondary language".

Image from Gyazo

Custom function for registering and deleting glossaries

Since glossaries registered in DeepL cannot be updated, we need to delete and update them each time.

We will delete the existing glossary in DeepL and re-register the updated translation pairs triggered by the content update of the glossary created earlier.

Set as follows:

ItemValue
Titledeepl_manage_glossary
Identifierdeepl_manage_glossary
TriggerAfter updateing content / Glossary content definition ID (8)
ProcessingThe following content
deepl_manage_glossary
loading...
caution

Please replace the /rcms-api/3/get_glossary/ part with your own endpoint URL.

tip

If you use triggers in a way that might cause a loop, you should include the following code at the beginning of your custom processing to prevent the loop from occurring:

{if $smarty.server.HTTP_RCMS_X_API_REQUEST_CNT > 0}
{return}
{/if}

Although API loop prevention is already in place, the trigger will fire twice due to constraints set up, even without this code. For more details, refer to Why is the trigger called twice after content update?.

Image from Gyazo

Once you have entered the information, click [Add] to add the custom function.

Custom function for translating content and registering it as a secondary language

The custom function for translating content and registering it as a secondary language is triggered by adding content. Send the added content to DeepL and add the translated HTML to the secondary language.

info

By setting the tag_handling parameter to html and sending a request to DeepL, we perform translation considering HTML tags. For more information, refer to HTML Handling.

info

To update Kuroco's secondary language, specify the _doc_lang parameter in the query.

Set as follows:

ItemValue
Titledeepl_translate
Identifierdeepl_translate
TriggerAfter adding content / Content definition ID of automatic translation content (9)
ProcessingThe following content
deepl_translate
loading...

Image from Gyazo

To add the custom function, click on [Add].

Verify the operation

With the above steps, the setup for automatic translation is complete. Update or add content to verify the operation.

Glossary

First, update the content of the glossary. Add any language pairs and leave the glossary_id empty. After the update, the glossary_id will be automatically added, and a log confirming the addition of the glossary will be recorded in the custom log.

Image from Gyazo

Image from Gyazo

There is no need to manually edit the glossary_id for subsequent updates. It will be automatically updated when registering the glossary in DeepL.

tip

Since the glossary_id is not manually updated, it is effective to apply CSS to hide it by referring to Can I modify the display of the content editor screen? when actually operating.

Automatic Translation of Content

Next, verify the operation of automatic translation of content. Click on [Add] on the page of the automatic translation content list.

Image from Gyazo

Enter any Japanese content and click on [Add].

Image from Gyazo

After adding the content, you can see that the secondary language content is also added at the same time.

Image from Gyazo

Also, when checking the translation content, it can be seen that the glossary has translated the "記事" as "Content" and the "副言語" as "Secondary languages".

With the above, we conclude the explanation of the implementation method for the automatic translation feature using the DeepL API. The DeepL API also has the capability to directly translate PDF and Word files using its Translate Documents feature. Therefore, please refer to this document and give it a try.


Support

If you have any other questions, please contact us or check out Our Slack Community.