Help with Google Translate and IE. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 1060
Joined: 30 Nov 2015, 21:19

Help with Google Translate and IE.

23 Jan 2018, 18:01

I got the advanced parts of the code from Benny-D here https://autohotkey.com/boards/viewtopic.php?t=21105.

The code is meant to translate some code text from English, to Spanish, then back to English again. Fairly often, no translation is returned though. I'm guessing that this is because the Google Translate server is busy, or something similar(?) I tried changing the Sleep 50 to Sleep 500, but that doesn't seem to fix it. Are there other tweaks I should try? Other ideas?

Code: Select all

#NoEnv
#SingleInstance force
;########## Source: https://autohotkey.com/boards/viewtopic.php?t=21105

^+t::  
;phrase := Clipboard
phrase := "Jonny continues to struggle in school.."
LangIn := "en"
LangOut := "es"
varSpan := % GoogleTranslate(phrase,LangIn,LangOut)
1stPhrase := phrase
;MsgBox, varSpan %varSpan%

phrase := varSpan
LangIn := "es"
LangOut := "en"
varEng := % GoogleTranslate(phrase,LangIn,LangOut)
;MsgBox, varEng %varEng%

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 500

Result := IE.document.all.result_box.innertext
IE.Quit
return Result
}

MsgBox, Started with: `t %1stPhrase%`n`nAs Spanish:`t %varSpan%`n`nBack to English: `t %varEng%
;Clipboard := varSpan
Last edited by kunkel321 on 25 Jan 2018, 14:32, edited 1 time in total.
ste(phen|ve) kunkel
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Help with Google Translate and IE.

23 Jan 2018, 19:40

Hello kunkel321.

The code you posted seems to be using IE COM to navigate to the webpage version of Google Translate and them recovering the translation from the html. I use that webpage in my browser regularly and it does fail to return an answer quite often. However, if you are going to write a script to get a Google translation, why not use their API instead? It will probably be more reliable.

The URL goes like the example below (this example is translating wonderful to spanish).
https://translate.googleapis.com/transl ... =wonderful

But do check the TOS of the translate API before you use it because it's probably not free for some uses (businesses and the like).
User avatar
kunkel321
Posts: 1060
Joined: 30 Nov 2015, 21:19

Re: Help with Google Translate and IE.

24 Jan 2018, 13:12

@Gio, Thanks for the reply. I'm afraid I have no idea how to use this URL. It doesn't matter though. I checked the Google TOS and there doesn't appear to be a 'free' option. It's a flat $20 per month, per one million characters. So that's a no go for me.

@Manlikezab12, the top code is essentially done. Unfortunately it's unreliable.
ste(phen|ve) kunkel
User avatar
rommmcek
Posts: 1475
Joined: 15 Aug 2014, 15:18

Re: Help with Google Translate and IE.

24 Jan 2018, 21:38

@kunkel321 I think it's a very robust script (except for picking up the text). To improve it try:

Code: Select all

while (!Result && A_Index < 50) {
	Result := IE.document.all.result_box.innertext
	sleep, 100
}
in place of Result := IE.document.all.result_box.innertext
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with Google Translate and IE.  Topic is solved

24 Jan 2018, 21:53

A related thread which might be of interest as well: Google Translate code slow and erroneous.

Hope this helps.
my scripts
User avatar
kunkel321
Posts: 1060
Joined: 30 Nov 2015, 21:19

Re: Help with Google Translate and IE.

25 Jan 2018, 14:05

Thanks everyone. Here is what I have now:

Code: Select all

#NoEnv
#SingleInstance force
;########## Source: https://autohotkey.com/boards/viewtopic.php?t=21105

^+t::  
phrase := Clipboard
LangIn := "en"
LangOut := "es"
varSpan := % GoogleTranslate(phrase,LangIn,LangOut)
1stPhrase := phrase

phrase := varSpan
LangIn := "es"
LangOut := "en"
varEng := % GoogleTranslate(phrase,LangIn,LangOut)

MsgBox, Started with: `t %1stPhrase%`n`nAs Spanish:`t %varSpan%`n`nBack to English: `t %varEng%
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)

SleepTime := StrLen(phrase) * 10
While IE.busy 
  Sleep, %SleepTime%

 while (!Result && A_Index < 50) {
	Result := IE.document.all.result_box.innertext
	sleep, 100
}

IE.Quit
return Result
}
@Rommmcek: Adding your little bit of code made it way more reliable. At first I left out

Code: Select all

SleepTime := StrLen(phrase) * 10
While IE.busy 
  Sleep, %SleepTime%
But it didn't always work. Then I put it back in. It works well, but with a full paragraph of text, it can take 30 seconds or more. I'm sure that's because of my StrLen(phrase) * 10 part though. I'll try a number smaller than 10, and see if it still works.

Questions for A_AhkUser: Would you recommend (based on that other post) taking the IE.Quit out of the function? It's already outside of the loops--yes? So I'm guessing it only get's used twice each time the script is run(?)

That JavaScript example on the other post is something else! I will use the other thread to ask about that though.

Thanks again everyone!
ste(phen|ve) kunkel
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with Google Translate and IE.

26 Jan 2018, 22:16

kunkel321 wrote: Questions for A_AhkUser: Would you recommend (based on that other post) taking the IE.Quit out of the function? It's already outside of the loops--yes? So I'm guessing it only get's used twice each time the script is run(?)
It is used twice each time you press your hotkey. Both IE := ComObjCreate("InternetExplorer.Application") and IE.Quit are in the body of GoogleTranslate in such a way that it depends on how frequently you actually call this function but, generally, a function is designed precisely for this purpose... Basically, in the code you provided, IE is created and destoyed each time the function is called. If you intend to call the function frequently (your ^+t subroutine does this already twice...) I guess yes you should follow the example in the other post. Otherwise, that is nothing more than open and close your browser each time to get a translation...
Incidentally, a similar logic applies to your base variable @GoogleTranslate - in such cases, consider using a static variable instead, for example. Btw, the code could be even more efficient by means of a webpage manipulation using javascript (and instead of navigating each time i.e. updating the url). When it comes to retrieve translation it is also very convenient to operate upon json formatted data as suggested by Gio.
kunkel321 wrote:That JavaScript example on the other post is something else!
Actually! I see you found what you was looking for with the incredible script of teadrinker and TBO that's what I had in view when I linked to the thread. Incidentally, from the little I understand it, it also operates upon json formatted data.
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DataLife, Google [Bot], pgeugene, Rohwedder and 119 guests