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

This article is the first and second part of a series of articles on how to use theDeepLtranslation API, covering string translation and file translation .
*A rudimentary knowledge of Python and Linux is assumed.
Please also refer to the manual of the original website.
https://www.deepl.com/en/docs-api
We use Python 3.9.
API specifications are current as of July 2022. Please be aware of this.
To use the API, you need to register as a user and obtain an access key ( auth_key) from
.
In this article, we will use [authKey] and you should replace it with your own access key.
The document ID and document key strings that appear later will also be denoted as [documentID] [documentKey] will be denoted as
Translation of tagged strings
The first step is to translate strings: translate Japanese with xml tags (html tags) into English (US).
The code examples in the manual are given in two formats: the HTTP Request format and curl, which is the standard command tool for Linux
.
We will look at the curl one, which is easier to try, as a reference.
If you want to translate the Japanese string " Example: <br /><span style="font-size:1.2em">Hello, world</span>"
into English (US), it will look like this:
$ src='Example: <br /><span style="font-size:1.2em">Hello, 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. For the url of the free version, please refer to Manual for the free version.
Since we are dealing with xml tags, the request also specifies tag_handling is also specified.
The source language is not specified, but in this case it will be detected automatically.
The execution result is omitted in this article. Please read on while actually checking it.
If you write this in Python, you can use requests.post() as follows:
(If you do not have requests, please install it)
import requests
import json
def get_key():
return open('key.txt').read().rstrip()
def translate_xml(src):
"'
translate xml tagged string
write curl command 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 text:<br /><span style="font-size:1.2em">Hello, world</span>'
tgt = translate_xml(src)
print(tgt)
Avoid writing the access key string in the source code
I read and use the text file key.txt stored in .
res_text is the result received from the server at DeepL and is a string in json format.
json.loads() is used to dict the string once and then retrieve the translated result.
One of the purposes of this article is to show an example of the manual curl command in
Python.
You can compare the two to get a feel for them.
That is all for this article:
We used the DeepL API to translate strings.
In the next article, we will translate string files using the API of DeepL. Please look forward to it!
Thank you for reading.