News & Columns
DeepL Using Translation from Python
1st Tagged String Translation

In this article, we will introduce how to use the DeepL translation API in the first and second parts, and introduce string translation and file translation.
*Basic knowledge of Python and Linux is assumed.
Please also see the manual of the original family.
https://www.deepl.com/en/docs-api
Python is using 3.9.
The API specifications are current as of July 2022. Please note.
To use the API, register a user and enter the access key (auth_key)
It is necessary to get.
In this article[authKey]Please replace it with your own access key.
The document ID and document key strings that appear later[documentID] [documentKey]It is noted.
Translating Tagged Strings
The first is the translation of the strings. Translate Japanese with XML tags (HTML tags) to English (US).
The code example in the manual shows the HTTP Request format and how to use it in the Linux
It describes two forms of curl, which is a standard command tool.
Let's take a look at curl that you can easily try.
Japanese String "Example: <br /><span style="font-size:1.2em"> hello, world </span>"
If you want to translate to English (US), it would look like this:
$src='Example:<br/><span style="font-size:1.2em">Hi, world</span>'
$ auth_key=[authKey]
$ curl https://api.deepl.com/v2/translate \
$ -d auth_key=${auth_key} \
$ -d text=${src} \
$ -d target_lang=en-us \
$ -d tag_handling=xml
This URL is for the paid version. The URL of the free version isManualPlease refer to.
xml tags, so the request usestag_handlingIt is also specified.
I didn't specify the original language, but in this case it will be auto-detected.
In this article, the results of the execution will be omitted. Please read on while actually checking it out.
Writing this in Python would look like this using requests.post():
(If you don't have requests, please install it)
import requests
import json
def get_key():
return open(‘key.txt’).read().rstrip()
def translate_xml(src):
”’
Translating XML tagged strings
Writing curl commands in python
”’
url = ‘https://api.deepl.com/v2/translate’
headers = dict()
headers[‘Content-Type’] = ‘application/x-www-form-urlencoded’
data = dict()
data[‘auth_key’] = get_key()
data[‘text’] = src
data[‘target_lang’] = ‘en-us’
data[‘tag_handling’] = ‘xml’
res = requests.post(url, headers=headers, data=data)
res_text = res.text
res_data = json.loads(res_text)
tgt = res_data[‘translations’][0][‘text’]
return tgt
src = 'Example:<br /><span style="font-size:1.2em">Hello, world</span>'
tgt = translate_xml(src)
print(tgt)
The access key string should be avoided writing in the source code.
I read and use what I have stored in a text file key.txt .
res_text is the result of receiving it from DeepL's server, which is a string in json format.
json.loads() to dict and then retrieve the translation result.
One of the purposes of this article is to add the curl command in the manual to
It is to show an example to express in Python.
If you compare the two, you will get a feeling.
That's all for this article:
- I used DeepL's API to translate the strings.
Next time, I'll use DeepL's API to translate the string file. Please look forward to it!
Thank you for reading.