Getting translations from DeepL online translator

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Getting translations from DeepL online translator

27 May 2018, 20:18

Hi everyone!

I am a translator. I have found that DeepL is an online translator that translates much much better than the crap translation my agency provides me with to perform a Machine Translation Edit. I work with a Translation Tool called Trados.

So I had the following idea. It would be great to have a script to do the following.
1. Copy source text (text to translate).
2.- Paste it onto the Source Language box in deepl.com/translate.
3.- Collect the translated text from the target language box.
4.- Paste that text in the relevant target location in my translation tool.
5.- Advance to the next line/segment of text. Do so a certain number of steps.

My script does precisely that, but in a crappy and newbie way. it just uses SendInputs, MouseMove, Clicks and Winactivates (Trados<->Firefox) to do the job. It does it, but barely. Since my scripting skills are close to zero, I have filled the script with Sleeps to help minimize any random behaviour, copy pasting not working, and the like. It is slow, I have to keep the monitors on since the mouse is moving back and forth, and it looks awfully slow, but that is more or less my scripting skills. Out of shame, I am NOT posting it... it's THAT lame.

However, I found a guy who did a similar script for Google Translate, but worked differently in the sense that, when the script opened Internet Explorer to load the Google Translate page, it did it in a hidden from view way, so it was much better looking. And apparently, instead of taking the mouse to a certain point in the website page, clicking, sending Control A and then Control X, it apparently had a way of "reading" the contents of the target language box directly, with some kind of DOM object. I have not an inkling of what DOM objects do or are, I just search for them in the Internet. But I believe they are things such as all obscure functions starting with IE.

If anyone has any inkling of what I am talking about and could adapt this code to work with DeepL, I would be extremely helpful. I am not posting my own script (the one that goes back and forth with the mouse) because it's too pitiful, but here's is the script of the guy that works with Google Translate.

Code: Select all

#NoEnv  ;~ Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ;~ Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ;~ Ensures a consistent starting directory.
SetTitleMatchMode, 3
LangIn ="en"
LangOut = "es"





;~ [Ctrl]+[F12] shows the little user interface for changing source and target language
^F12::
Gui, 1:Show, Autosize
Return


;~ [F12] runs Google Translate for selected text or makes the tooltip with the translation disappear if it is visible
F12::

CurrentCB = %Clipboard%
Clipboard =
SendInput, ^c
ClipWait, 5
If ErrorLevel
{
    MsgBox, 48, GoogleTranslateSelection, No text highlighted or problem copying text to clipboard.
    Return
}
Source = %Clipboard%
StringLen, SourceLength, Source
SourceLength := SourceLength * 5
ToolTip, Translating... please wait ☺., % A_CaretX-SourceLength, % A_CaretY+50
Target =
Target := GoogleTranslate(Source,en,es)
ToolTip, %Target%, % A_CaretX-SourceLength, % A_CaretY+50
Clipboard = %Target%

GoogleTranslate(Source,LangIn,LangOut)
{
    StringReplace, Source, Source, %A_Space%, `%20, All
    Base := "translate.google.com/#"
    Path := Base . "en" . "/" . "es" . "/" . Source
    IE := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Google Translate and retrieve translation
    IE.Navigate(Path)
    While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy
            Sleep 50
    Result := IE.document.all.result_box.innertext
    IE.Quit
    Return Result
}
Does anyone know how to adapt this to DeepL? I believe the key lies in the

Code: Select all

Result := IE.document.all.result_box.innertext
line, but... I have no idea.

The script is not exactly what I need, but I think what I need would be easily adapted. What I need is to be able to input the source text and obtain the target text from http://www.deepl.com/translator. This script does it from Google Translate... and I am not able to do the change
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Getting translations from DeepL online translator

10 Sep 2018, 13:29

So there is a few things that would need to be changed in order to get this working properly. The first part is the Base variable. The new variable will be this

Code: Select all

Base := "https://www.deepl.com/en/translator#"
Now we can adjust the Path so represent the languages you want.

Code: Select all

Path := Base . LangIn . "/" . LangOut . "/" . Source
Now for the result we want to adjust the formula a bit to get the text since the box has no id we can use (note that we must use the [1] since multiple elements exist with the tag "textarea" so this makes sure we target the correct one)

Code: Select all

Result := IE.document.getElementsByTagName("textarea")[1].value
Your Final code would look like this

Code: Select all

MsgBox % DeeplTranslate("This is text to translate to german from english", "en", "de")

DeeplTranslate(Source,LangIn,LangOut)
{
    StringReplace, Source, Source, %A_Space%, `%20, All  ;you don't need this, but you can keep it in
    Base := "https://www.deepl.com/en/translator#"
    Path := Base . LangIn . "/" . LangOut . "/" . Source
    IE := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Google Translate and retrieve translation
    IE.Navigate(Path)
    While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy
        Sleep 50
	While (IE.document.getElementsByTagName("textarea")[1].value = "")  ;Since the conversion takes a second we want to wait till the value is filled otherwise the return will always be nothing
		Sleep 50
    Result := IE.document.getElementsByTagName("textarea")[1].value
    IE.Quit
    Return Result
}
Antonio

Re: Getting translations from DeepL online translator

10 Sep 2018, 18:05

This is the code I've tried for Italian following your instructions (using ALT+D as the shortcut):

Code: Select all


!d::

MsgBox % DeeplTranslate("This is text to translate to italian from english", "en", "it")

DeeplTranslate(Source,LangIn,LangOut)
{
    StringReplace, Source, Source, %A_Space%, `%20, All  ;you don't need this, but you can keep it in
    Base := "https://www.deepl.com/en/translator#"
    Path := Base . LangIn . "en" . LangOut . "it" . Source
    IE := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Google Translate and retrieve translation
    IE.Navigate(Path)
    While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy
        Sleep 50
	While (IE.document.getElementsByTagName("textarea")[1].value = "")  ;Since the conversion takes a second we want to wait till the value is filled otherwise the return will always be nothing
		Sleep 50
    Result := IE.document.getElementsByTagName("textarea")[1].value
    IE.Quit
    Return Result
}
Unfortunately, it is working. I get this error:

"Dies ist ein Text, der aus dem Englischen ins Deutsche übersetzt werden soll."

Why I get this wrong? Thanks!
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Getting translations from DeepL online translator

10 Sep 2018, 20:08

The reason why is because you changed the path variable. Make the en and it back to / and it will work. Calling the function is where you will choose your language
Antonio

Re: Getting translations from DeepL online translator

11 Sep 2018, 03:37

Ok, I've changed it back to /. Now I get another popup in Italian, but it doesn't seem to work. Sorry if I am being annoying, but I am not a programmer :D .

Basically, this is what I'd like the script to do:

I select some text, press a key combination and get the translation from Deepl. I would only need to past it where I want. Do you think that would be possible? Thanks!
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Getting translations from DeepL online translator

11 Sep 2018, 03:59

Nixcalo wrote:I have found that DeepL is an online translator that translates much much better than the crap translation my agency provides me with to perform a Machine Translation Edit.
A very good example of what an artificial neural network can do.
The function provided by antonio works very well for me. 8-)
thank's for sharing :thumbup:
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Getting translations from DeepL online translator

11 Sep 2018, 07:12

Thats not too hard

Code: Select all

!d::

Send ^c
Clipboard := DeeplTranslate(Clipboard, "en", "it")

DeeplTranslate(Source,LangIn,LangOut)
{
    StringReplace, Source, Source, %A_Space%, `%20, All  ;you don't need this, but you can keep it in
    Base := "https://www.deepl.com/en/translator#"
    Path := Base . LangIn . "/" . LangOut . "/" . Source
    IE := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Google Translate and retrieve translation
    IE.Navigate(Path)
    While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy
        Sleep 50
	While (IE.document.getElementsByTagName("textarea")[1].value = "")  ;Since the conversion takes a second we want to wait till the value is filled otherwise the return will always be nothing
		Sleep 50
    Result := IE.document.getElementsByTagName("textarea")[1].value
    IE.Quit
    Return Result
}
This will copy any highlighted text (as long as that is the active window) and sends it thought Deepl. Before you paste what is back into your clipboard make sure to give it a second since the function needs time to open the deepl website and wait for the translation.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Getting translations from DeepL online translator

11 Sep 2018, 11:54

You dont need to provide lang in for deepL.
It's probably better at recognizing the input language than the user is.
Sadly its only available for so few languages.
The way I see it is the best translation tool available.
Also you should probably use a class for this:

Code: Select all

class DeepLTranslator {
	
	static formatURL := "https://www.deepl.com/en/translator#{2:s}/{3:s}/{1:s}"

	__New() {
		this.IE := ComObjCreate("InternetExplorer.Application") 
	}

	__Delete() {
		this.IE.Quit
	}
	translate(sourceText, languageOut, languageIn := "auto") {
		sourceURL := this.uriEncode(sourceText)
		url := Format(This.formatURL, sourceURL, languageIn, languageOut)
		this.navigate(url)
		return this.translation() 
	}
	
	navigate(url) {
		this.IE.Navigate(url)
		While (this.IE.readyState != 4 || this.IE.document.readyState != "complete" || this.IE.busy)
			Sleep 50
	}
	
	translation() {
		While ((result := this.IE.document.getElementsByTagName("textarea")[1].value) = "")
			Sleep 50
		return result
	}
	
	uriEncode(sourceText) {
		return StrReplace(sourceText, " ", "%20")
	}
}
And then use it like this:

Code: Select all

translator := new DeepLTranslator()
!d::
Send ^c
Clipboard := translator.translate(Clipboard, "en", "it")
That will keep you from creating a new IE instance every time you want to translate something.
Edit: fixed 2 typos
Recommends AHK Studio
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Getting translations from DeepL online translator

11 Sep 2018, 11:59

Lol, I am so bad with classes :P, but also you could set the translate(sourceText, languageIn="auto", languageOut) as the default as auto is the value that it sets that way you don't have to give it (also deepl will change the first language to whatever it thinks it is if its not the language you determined).
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Getting translations from DeepL online translator

11 Sep 2018, 12:01

MannyKSoSo wrote:Lol, I am so bad with classes :P, but also you could set the translate(sourceText, languageIn="auto", languageOut) as the default as auto is the value that it sets that way you don't have to give it (also deepl will change the first language to whatever it thinks it is if its not the language you determined).
Thanks I have edited that in
Recommends AHK Studio
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Getting translations from DeepL online translator

11 Sep 2018, 12:08

I use this linguee-api (Linguee is being developed by DeepL GmbH, actually) in a script of mine. This is the simple and more or less dependant from the original script javascript I use to parse the JSON response. This API has the advantage (just as Linguee) to also provide translation memories (words in their respective context with sources, from official texts published by UNESCO, ONU etc.).
my scripts
mora145
Posts: 57
Joined: 25 Jun 2022, 15:31

Re: Getting translations from DeepL online translator

01 Aug 2022, 22:58

I am developing something specifically for what you want using AutoHotKey. I am also a translator, and I use DeepL to work with MemoQ.
Contact me.
sokrakes
Posts: 36
Joined: 07 Aug 2019, 01:40

Re: Getting translations from DeepL online translator

27 Sep 2022, 10:31

Hi,
i had deepl installed and was translating it this "primitive" way, switching application windows.

Code: Select all

MouseGetPos, xpos, ypos 
WinActivate, ahk_exe DeepL.exe
MouseMove, 140, 210
Send {LButton}
send ^a
send {Delete}
send ^v
sleep 3000
MouseMove, 1530, 1070
Send {LButton}
send !{Tab}
sleep 50
send ^v
MouseMove, %xpos%, %ypos%
[Mod edit: [code][/code] tags added.]

I'm looking for a more intelligent solution to translating the contents of the clipboard.
I can't separate the above, it works for you, can you direct me?
sokrakes
Posts: 36
Joined: 07 Aug 2019, 01:40

Re: Getting translations from DeepL online translator

27 Sep 2022, 12:55

Now I simplified it a bit

Code: Select all

send ^c
MouseGetPos, xpos, ypos 
send {CtrlDown}{c}{c}{CtrlUp}
sleep 3000
MouseMove, 3733, 870
Send {LButton}
send !{F4}
MouseMove, %xpos%, %ypos%
Last edited by gregster on 27 Sep 2022, 13:05, edited 1 time in total.
Reason: Thanks for using code tags! But please only put them around code.
sokrakes
Posts: 36
Joined: 07 Aug 2019, 01:40

Re: Getting translations from DeepL online translator

28 Sep 2022, 02:56

mora145 wrote:
01 Aug 2022, 22:58
I am developing something specifically for what you want using AutoHotKey. I am also a translator, and I use DeepL to work with MemoQ.
Contact me.
May I ask? ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat and 155 guests