10-31-2015, 04:00 AM
Hi QMers:
This is only a little exercise, so feel free to change and improve everything.
Function QMTranslate
Macro Use QMTranslate
Function Translator
Macro SysMenuExample
Trigger #Rh2
This is only a little exercise, so feel free to change and improve everything.
Function QMTranslate
;/
function'str $text $langTo [$langFrom]
;Translates text using Google Translate.
;Returns translated text.
;text - text to translate.
;langTo - output language.
;langFrom - input language. English by default
str stext=text
stext.findreplace(" " "%20")
if empty(langFrom)
,langFrom="en"
str Url=F"https://translate.google.com/#{langFrom}/{langTo}/{stext}"
HtmlDoc d
d.SetOptions(2)
d.InitFromWeb(Url)
;FirefoxWait "" 1
str s=d.GetHtml
int i
str translated_text
ARRAY(str) a
if findrx(s "span class=''hps''>(.+?)</span" 0 1|4 a)>=0
,for i 0 a.len
,,translated_text+F"{a[1,i]} "
,translated_text.rtrim
else translated_text= "not found"
ret translated_text
Macro Use QMTranslate
Function Translator
;/
str selected_text.getsel
selected_text.findreplace(" " "%20")
int h
GetWorkArea 0 0 0 h
int w=win
RECT r
scan F"color:{GetSysColor(COLOR_HIGHLIGHT)}" w r 2|16
int y=r.top+100
if y+100>h
,y=r.top-100
OnScreenDisplay "Wait a second..." 10 r.left y "" 12 0xff0000 1|4|16 "my-tooltip" 0xc0ffff
str translated_text=QMTranslate(selected_text "es")
OnScreenDisplay translated_text 0 r.left y "" 12 0xff0000 1|4|16 "my-tooltip" 0xc0ffff
err+ OnScreenDisplay "Error"
Macro SysMenuExample
Trigger #Rh2