Sunday, August 5, 2018

Autohotkey and Google Translate

Run Translation Program.ahk. A Messagebox will appear on every initial start. Just click ok to get rid of it. Now everytime you copy something a new Messagebox will apear with the translated text. 
To close the program: First close all Messageboxes then press Control+j. This can be changed to whatever if you know your way around Autohotkey. Check Translate Program.ahk
Changing the language: Open Translate.ahk with any text edito of your choice and change "LangIn" and "LangOut" correspondingly. "LangIn" is the language of the original text and "LangOut" the language it is meant to be translated to.
You need to know the language codes. The Text is translated with Google Translator so expect some bad grammar and broken sentences, but for getting a basic understading of what is being said. Source: https://github.com/Pjer4/Translation-Program
 
Translation Program.ahk 

OnClipBoardChange:
Loop
{
Run Translate.ahk
return
}
^j:: Exitapp
Numpad1::

 Translate.ahk
#SingleInstance force
phrase := clipboard
LangIn := "de"
LangOut := "en"
SetTimer, WinMoveMsgBox, 50
MsgBox,4096, Translation, % GoogleTranslate(phrase,LangIn,LangOut)
ID:=WinExist("Translation")
return

WinMoveMsgBox:
ID:=WinExist("Translation")
WinGetPos, Xc, Yc, Width, Height, ahk_id %ID%
x=A_Screenwidth-WidthSetTimer, WinMoveMsgBox, OFF
ID:=WinExist("Translation")
WinMove, ahk_id %ID%, , %x%, 400
return

GoogleTranslate(phrase,LangIn,LangOut)
{
base := "https://translate.google.com.tw/?hl=en&tab=wT#"
path := base . LangIn . "/" . LangOut . "/" . phrase
IE := ComObjCreate("InternetExplorer.Application")
;~ IE.Visible := true
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
}
return

GuiClose:

ExitApp