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
  • Hi Raphaël,

    Could you modify this script to work with the DeepL translator page? I have been fiddling to do it, but I don't know anything about IE commands and I don't know how to modify your Result := IE.document.all.result_box.innertext line so it works with DeepL.

    I have tried the usual things arising from a limited knowledge of scripting and a zeroish knowledge of IE, COM, DOM or whatever they mean. The crux of the matter is that I don't know how to collect the translated text from the DeepL page as you did for Google Translate...

    You would give me a joy!

    Ignacio

Reply
  • Hi Raphaël,

    Could you modify this script to work with the DeepL translator page? I have been fiddling to do it, but I don't know anything about IE commands and I don't know how to modify your Result := IE.document.all.result_box.innertext line so it works with DeepL.

    I have tried the usual things arising from a limited knowledge of scripting and a zeroish knowledge of IE, COM, DOM or whatever they mean. The crux of the matter is that I don't know how to collect the translated text from the DeepL page as you did for Google Translate...

    You would give me a joy!

    Ignacio

Children
No Data