Translate via DeepL

Currently, DeepL (https://www.deepl.com/translate) is hot in the translators world.

Could anyone please create a macro to translate the content of the current clipboard and replace it with a translation in one of the available languages?

It would also be very useful to have any available alternative translations displayed (possibly via another keyboard shortcut).

Via the computer magazine C’t (https://www.heise.de/ct/hotline/DeepL-Uebersetzer-auf-der-Kommandozeile-3825206.html?wt_mc=print.ct.2017.20.162#zsdb-article-links) I’ve found this Python script:

But I don’t have the knowledge to get it working on my Mac :(.

Thanks in advance!

2 Likes

Hi @CafeTran_Training, I'm not an expert in scripts, so I created this macro. Maybe it'll help you a little.
You have to select the languages with the cursor, because you cannot use Enter etc. here. Everything else is done by the macro.

11)Übersetzen.kmmacros (34,6 KB)

Greetings from Germany :nerd_face:

2 Likes

Assuming you have never fiddled around with python on your Mac, I recommend you this:

Install Python3

Easiest way:

  1. Install the Homebrew package manager according to the instructions.

  2. Now open a new Terminal window and install python3 with the help of Homebrew:

    brew install python3
    
  3. Wait for the install to finish.

Install the required python modules

Since you have installed python3 in a separate environment, do not follow the instructions on the Github page.

Instead, open a new Terminal window and just run this:

python3 -m pip install requests

This was the only module I had to install.

Save the script from the Github page

anywhere on your disk as translate.py

Run the script from within KM

in a Execute Shell Script action:

export LANG=en_US.UTF-8
/usr/local/bin/python3 '<path/to/the/script>/translate.py' -l DE "$(pbpaste)"

Of course, replace <path/to/the/script> with the actual path to your script file.

Here is an example macro:

[demo] DeepL via Python.kmmacros (1.7 KB)

This will feed the text on your clipboard to the script and you'll get the output in a KM window.

The -l option in the command line is for the target language. (Obviously using the ISO 639-1 codes in uppercase.)

4 Likes

Thank you very much, Tom!

I’ve posted this in the CafeTran Freshdesk: https://cafetran.freshdesk.com/support/discussions/topics/6000051005

And at Proz.com:
https://www.proz.com/forum/cafetran_support/318845-using_the_temporary_api_to_translate_source_segments_in_cafetran.html

Since you have linked to this thread on two other forums, I assume that my above instructions worked fine for you. :relieved:

On the CafeTran forum you say that “you need the uber-useful Keyboard Maestro to execute the macros.” Just for clarification:

The Python script itself runs perfectly fine also without KM. You can run it with other launchers (Alfred, LaunchBar, etc.) or from within an AppleScript, etc., or of course simply from the Terminal.

In the Terminal, to get the result to the clipboard just pipe it to pbcopy. If your system’s LANG variable is correctly set to UTF-8 (the language doesn’t matter) then you don’t need the first line.

So, this…

/usr/local/bin/python3 '<path/to/the/script>/translate.py' -l DE "$(pbpaste)" | pbcopy

would be a standalone command line for the Terminal that gets the input from the clipboard and pastes the output to the clipboard.


BTW, I’ve never heard of that CafeTran thingy before, but it looks very interesting. I’m also working as translator (not my main job though) and in the past I used OmegaT or, ahem, no CAT tool at all. I will definitely give CafeTran a serious try next time!


PS:

I’ve just seen that the forum links you’ve posted on Proz and Freshdesk go to post#2 in this thread. I’m not sure if this was your intention, since you also uploaded the ‘DeepL via Python’ macro file (which is from post #3).

Yes, Tom, your instructions helped me to get the Python script running. Thanks again!

The piping to the clipboard is also very nice. Must look into Alfred some day …

About CafeTran: I’ve compared it with the other currently available CAT (computer-assisted translation) tools for the Mac platform thoroughly. It’s an advanced, high-performance app, with a very responsive developer. If you have any questions about CafeTran, I’d be very happy to help you!

1 Like

Hi @CafeTran_Training ,

4 days ago Translate Shell has added support for DeepL.

I have uploaded a GUI macro for Translate Shell in the Macros category.

Unfortunately, with DeepL via Translate Shell there are encoding issues on the Mac. I’ll try to find a workaround, when I find some time. Or better: let’s hope the author finds some solution…

Edit /PS:

Found sort of a workaround. See the macro post.

Hi Tom,

Is there a way to get these nifty alternative translations from DeepL in Safari or Chrome to a variable?

Hey @ALYB,

It looks like all the elements are available in the DOM (document object model), so it should be possible to reliably parse the page with JavaScript.

I don’t have time to do that at present, but here’s a quick and dirty way.

Run the following AppleScript from an Execute an AppleScript action:

tell application "Safari" to tell front document to return its text

This will return all the text from the front page in Safari.

-Chris

Hi Alyb,

I guess it will be easier to use the Translate Shell script, which supports DeepL. Read the docs on Github, it has stuff like dictionary mode and alternate translations (not sure if also for DeepL, but I think so).

If Translate Shell does what you want, then it’s just a matter of grabbing the shell output, which will probably be more straightforward than getting it from the web page. (The script is using the API of the transllation engine.)

I’ve already written a KM macro for Translate Shell. It does not cover the alternate translations and other modes of Translate Shell, but I’m sure you can copy some elements from the macro.

You also find some (hopefully) useful information in my macro post, e.g. how to install Translate Shell and a workaround for a bug (if the bug is still there).

See here: https://www.proz.com/forum/cafetran_support/324095-harvest_alternative_translations_from_deepl.html

Get Primary and Alternative Translations from DeepL.kmmacros (1.9 KB)

How can I send the clipboard content to a JavaScript for Chrome? (To replace My Own Text with the content of the system clipboard.)

According to this Wiki article you can use KM variables in the JS action.

So, setting a variable to the Clipboard token %SystemClipboard% and then using that variable as shown in the Wiki article should work.

Hint: If you posted a downloadable minimal example (+ the information what you are trying to achieve on exactly which web page URL) we could test it for you.

I'm using this:


Get Primary and Alternative Translations from DeepL.kmmacros (2.2 KB)

And these:


Send Clipboard Content to DeepL.kmmacros (4.0 KB)
Open DeepL in Chrome in compact mode.kmmacros (3.7 KB)

1 Like

I’ve tried your “Send Clipboard Content to DeepL” macro in Safari (with the proper Safari actions) and it doesn’t work. But it doesn’t work either if I replace the KM variable with a fix string (const strClip = "test string";).

Does it work for you in Chrome?

Yes, Tom. Flawless.

Well, I installed Chrome, and it works in Chrome. And it also works perfectly fine with the KM variable (const strClip = document.kmvar.clipText;)

So, ATM I don’t know why it doesn’t work for you with the Clipboard token / KM variable. My Chrome version is 65.0.3325.181

Which KM version are you using?

If you are using KM7 or earlier try it with %CurrentClipboard% (instead of %SystemClipboard%).

It is possible that %SystemClipboard% was introduced with KM8.

I’m using the latest versions of Chrome and Keyboard Maestro.

Weird.

I just downloaded your macro again, to make sure I haven’t made any modification. And yes, it works out of the box here.

(To be clear: I’m speaking of your “Send Clipboard” macro, not any of the others.)