Overview
This script works everywhere you can select text: Trados Studio, MS Office, webpages, PDF files... you name it.
When you first run the script, you can set your source language (either auto if you want Google to recognize the language or a two letter language code) and target language. There are some predefined languages I used most, but one can directly write in the edit boxes. Alternatively, you can modify the code and add the languages you use most. Adding a double pipe character "||" after a language makes it the one selected by default. You can call up the language selection interface by pressing [Ctrl]+[F12].
Just select the text you want to have translate by Google Translate (maybe not an entire page, since it will be not too readable) and press [F12]. Ah tool tip shows Google is busy translating and then you get the translation in a tooltip next to the caret position:
↓
To make the tooltip with the translation disappear, just press [F12] again.
Code
Text in green are comments in the code for better understanding. The code can be pasted like this in an editor.
#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, 3ToolTipVisible = 0;~ Interface for selecting source and target languageGui, 1:Add, Text, x10 y15 vTb1, Source language:Gui, 1:Add, Combobox, x+20 yp-3 vLangIn, auto||en|de|fr|it|es|nl|ja|sk|tr|hu ;~ place double pipe behind language to be used as defaultGui, 1:Add, Text, x10 y+15 vTb2, Target language:Gui, 1:Add, Combobox, x+22 yp-3 vLangOut, en|de||fr|it|es|nl|ja|sk|tr|hu ;~ place double pipe behind language to be used as defaultGui, 1:Add, Checkbox, x10 y+20 Checked vPaste2CB, Paste translation to clipoard when closing tooltipGui, 1:Add, Checkbox, x10 y+15 Checked vHideToolTip, Hide tooltip on mouseclickGui, 1:Add, Button, x30 y+20 w80 vB1, &ExitGui, 1:Add, Button, x+30 w80 Default vB2, &OKGui, 1:Show, AutosizeReturnButtonExit:GuiEscape:GuiClose:ExitAppButtonOK:Gui, 1:SubmitReturn;~ [Ctrl]+[F12] shows the little user interface for changing source and target language^F12::Gui, 1:Show, AutosizeReturn;~ A mouse click makes the tooltip disappear~RButton::~MButton::~LButton::If HideToolTip = 0 ReturnIf ToolTipVisible = 1{ ToolTip ToolTipVisible = 0 Return}Return;~ [F12] runs Google Translate for selected text or makes the tooltip with the translation disappear if it is visibleF12::If ToolTipVisible = 1{ ToolTip ToolTipVisible = 0 Return}CurrentCB = %Clipboard%Clipboard =SendInput, ^cClipWait, 5If ErrorLevel{ MsgBox, 48, GoogleTranslateSelection, No text highlighted or problem copying text to clipboard. Return}Source = %Clipboard%StringLen, SourceLength, SourceSourceLength := SourceLength * 5ToolTip, Translating... please wait ☺., % A_CaretX-SourceLength, % A_CaretY+50Target =Target := GoogleTranslate(Source,LangIn,LangOut)ToolTip, %Target%, % A_CaretX-SourceLength, % A_CaretY+50ToolTipVisible = 1If Paste2CB = 1 Clipboard = %Target%Else Clipboard = %CurrentCB%ReturnGoogleTranslate(Source,LangIn,LangOut){ StringReplace, Source, Source, %A_Space%, `%20, All Base := "translate.google.com/#" 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 Result := IE.document.all.result_box.innertext IE.Quit Return Result}
If you have suggestions or ideas on how to improve this script, please don't hesitate to let me know!
Edit 1:
- Small adaptation to make script work in Windows 10
Edit 2:
- Simplification of code to avoid endless loops
- Replacement of spaces in source tags by "%20" because literal spaces in the IE address bar lead to the error encountered by Paul
- Option to save translation to clipboard
- Option to close tooltip with translation on mouseclick
Translate


