Python 3 script for Deepl Translator Pro

Among the MT (machine translation) providers Deepl is the new kid on the block, but it has been making waves ever since it appeared. Frankly speaking, as soon as I found out how good the Deepl MT is, I actually abandoned Google Translate. If I am to pay for a service, I'll pay for the one which delivers better results.

Before Deepl announced the paid Pro service, there was (and still is) the free Python module called pydeepl. It used to work until recently, but the ‘backdoor’ making it possible to programmatically get free translations in the era of the paid service has recently been plugged. (For the record, I subscribed to the paid service as soon as it appeared).

There also is a Deepl Translator plugin for SDL Trados Studio. It is quite simple and almost no settings, except for the API key, can be entered. My Python script for pydeepl was more sophisticated, for example I added a replacement table for cases where I did not like Deepl’s word choice.

Today I read up a bit on the best way to create a Python script for Deepl Pro and here we are – I wrote one.

How do you make Trados Studio talk to a Python script and get a response from it? It needs an intermediate layer called AHK (or AutoHotkey). AHK scripts are ‘macros’ remotely similar to those in Word or Excel, but they work everywhere in Windows.

So, if you are interested and want to make it work, you need to download and install the (free) AutoHotkey software. Then download the scripts (link at the end of this post), and modify them. There are a few modifications and installations that you need to do, but you only need to do it once for most of them, until you reinstall your system or buy a new computer. And don't you complain, you're getting a freebie here, so put in some effort on your side to set it up. :-)

I use two AHK scripts, one that gets translation for the whole segment in Studio (It will also work in MemoQ, if you modify  the Copy Source to target shortcut to Ctrl+Ins in MemoQ), called translate.ahk, and the other gets translation for the selected text – translatesel.ahk. When you save these on your disk, you need to modify the path to the python script to point to the exact location where it is saved, otherwise nothing will work. The code of the AHK script is very simple.

^!h::
Send ^{Ins}
Send ^a
Send ^c
RunWait D:\Documents\TM\translate.py,, Hide
Send ^v
Return


In both scripts you need to modify the highlighted part, keeping everything else unchanged. The Windows Notepad will do for this task, but you can also try the free Notepad++. When these scripts are modified, and AHK installed, you need to right-click each script, and select the 'Run script' command. The script does not actually run, until you press Right Alt + H for translate.ahk and Right Alt + I for translatesel.ahk, respectively. Just remember to select some text before you run translatesel.ahk. And this latter script will also work in any other text editing software, not just Studio. Just select the text you need to machine-translate, and press the shortcut. When you restart Windows, you need to run these scripts again for them to work.

You will also need to install Python, because it does not come bundled with Windows. My script works with Python 3, and the latest release at the time of writing is Python 3.7. If you have 64-bit Windows, choose a 64-bit Python installer. When installing Python, remember to tick the option that adds the Python path to the Windows PATH environment variable, or the python script will not run.

When Python is installed, you will also need to install one extra module called pyperclip, because the AHK script connects Studio and Python using the Windows clipboard. To do that, press the Windows key + S, type cmd and press enter. The Windows command line will display, where you need to type:

pip install pyperclip

and press Enter. By the way, pip is Python's installer for its libraries or modules.

In the python script itself, called translate.py, you also need to modify at least one thing, namely your Deepl Pro API key. You will not find my key there, you should be so lucky ;-) The part that you need to modify is highlighted below:

import pyperclip
import requests
import json

source = pyperclip.paste()
payload = {'auth_key': '00000000-0000-0000-0000-000000000000', 'text': source, 'target_lang': 'PL'}

You can also modify the target langue that you want, using the abbreviation for one of the languages supported by Deepl.

Further on in the script there is this ‘zamiana' replacement table, where you can modify stuff between straight single quotes, remembering to keep the straight single quotes, or else you'll break the script (NB, extra straight single quotes will also break it! ;-)). The rule is that to the left of the colon is the stuff that you want to find, and to the right – the stuff that you want to replace it with, like this:

'through the skin' : 'by the skin'

And that's about it. No guarantees whatsoever, use at your own risk. You have been warned. ;-)

And here are the download links:

1. AutoHotkey: https://autohotkey.com/download/
2. Python: https://www.python.org/downloads/windows/ (choose the executable installer for the 32-bit or 64-bit version of Windows, depending on which Windows architecture you have)
3. The scripts: http://englishintopolish.eu/translate_scripts.zip










Comments

Popular posts from this blog