Highlight text and get result from Google Translate in tooltip

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, 3

ToolTipVisible = 0
;~ Interface for selecting source and target language
Gui, 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 default
Gui, 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 default
Gui, 1:Add, Checkbox, x10 y+20 Checked vPaste2CB, Paste translation to clipoard when closing tooltip
Gui, 1:Add, Checkbox, x10 y+15 Checked vHideToolTip, Hide tooltip on mouseclick
Gui, 1:Add, Button, x30 y+20 w80 vB1, &Exit
Gui, 1:Add, Button, x+30 w80 Default vB2, &OK
Gui, 1:Show, Autosize
Return

ButtonExit:
GuiEscape:
GuiClose:
ExitApp

ButtonOK:
Gui, 1:Submit
Return

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

;~ A mouse click makes the tooltip disappear
~RButton::
~MButton::
~LButton::
If HideToolTip = 0
    Return
If 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 visible
F12::
If ToolTipVisible = 1
{
    ToolTip
    ToolTipVisible = 0
    Return
}
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,LangIn,LangOut)
ToolTip, %Target%, % A_CaretX-SourceLength, % A_CaretY+50
ToolTipVisible = 1
If Paste2CB = 1
    Clipboard = %Target%
Else
    Clipboard = %CurrentCB%
Return

GoogleTranslate(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
Parents Reply
  • Good morning Nora!

    Thanks a lot for your detailed feedback. I fear there is only little that can be done to improve the script further concerning reliability. The reason for this is that it "silently" (or rather invisibly in the background) calls Internet Explorer and then accesses the URL "translate.google.com/#auto/es/your source text here" and directly reads the translation from the source code of IE. I'll have to dig deeper into that since I have re-used pieces of already existing code and written parts of the script myself.

    Achieving a more reliable script would mean passing by the official Google Translate API and getting an API key, a solution into which I haven't yet looked.

    What I take away from the feedback so far received is that simple scripts usually work for most if not all users, most often even over different Widows versions. As soon as a script gets more complex and/or has external dependencies, things get trickier. Unfortunately, it is very difficult for amateur scripters like me to test all scenarios and environments, but I'll try my best to troubleshoot whenever possible.

    Greetings from Brussels,
    Raphaël

Children