Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translator
#1
Hi QMers:

This is only a little exercise, so feel free to change and improve everything.

Function QMTranslate
Code:
Select All      Help
;/
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
Code:
Select All      Help
str s=
;this is a little example

out
out QMTranslate(s "es" "en")

Function Translator
Code:
Select All      Help
;/
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     Help - how to add the trigger to the macro
Code:
Select All      Help
;Adds items to the system menu of all windows
sel SysMenu("-[]Copy Videos[]xxx TRANSLATE xxx-[]On Top[]NOT On Top" 0 0 0 0 0 1)
,case 1: Videos
,case 2: Translator
,case 3: Zorder win HWND_TOPMOST;; ont win
,case 4: Zorder win HWND_NOTOPMOST;; ont- win
,case else ret


Attached Files Image(s)
   
Reply
#2
Why FirefoxWait? I removed it, now works faster.
Reply
#3
Update!

Function Translator
Code:
Select All      Help
;/
str selected_text.getsel

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=GoogleTranslator(selected_text "es")

OnScreenDisplay translated_text 0 r.left y "" 12 0xff0000 1|4|16 "my-tooltip" 0xc0ffff

err+ OnScreenDisplay "Error"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)