Re: Using Google Translate to automate text translation
Posted: 25 Jun 2020, 11:25
Envy, I am sure that there already is php wrapper.
You need just google it.
You need just google it.
Let's help each other out
https://www.autohotkey.com/boards/
Code: Select all
GoogleTranslate(str, from := "auto", to := "en")
Code: Select all
GoogleTranslate(str, from := "auto", to := "jp")
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
clipboard := ""
clipboard := clipboard
~LButton::
Loop, {
MouseGetPos, x, y
KeyWait, LButton
MouseGetPos, x0, y0
If (abs(x-x0) > 1 or abs(y-y0) > 1){
Send ^c
ClipWait
If (RegExmatch(clipboard, "[^\x00-\x7F]")) { ; WORKS
Tooltip, % GoogleTranslateEN(clipboard)
}
Else {
Tooltip, % GoogleTranslateJP(clipboard) ;DOES NOT WORK
}
}
}
Return
GoogleTranslateEN(str, from := "auto", to := "en") {
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
if !IsObject(oJSON[1]) {
Loop % oJSON[0].length
trans .= oJSON[0][A_Index - 1][0]
}
else {
MainTransText := oJSON[0][0][0]
Loop % oJSON[1].length {
trans .= "`n+"
obj := oJSON[1][A_Index-1][1]
Loop % obj.length {
txt := obj[A_Index - 1]
trans .= (MainTransText = txt ? "" : "`n" txt)
}
}
}
if !IsObject(oJSON[1])
MainTransText := trans := Trim(trans, ",+`n ")
else
trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
from := oJSON[2]
trans := Trim(trans, ",+`n ")
Return trans
}
GoogleTranslateJP(str, from := "auto", to := "ja") {
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
if !IsObject(oJSON[1]) {
Loop % oJSON[0].length
trans .= oJSON[0][A_Index - 1][0]
}
else {
MainTransText := oJSON[0][0][0]
Loop % oJSON[1].length {
trans .= "`n+"
obj := oJSON[1][A_Index-1][1]
Loop % obj.length {
txt := obj[A_Index - 1]
trans .= (MainTransText = txt ? "" : "`n" txt)
}
}
}
if !IsObject(oJSON[1])
MainTransText := trans := Trim(trans, ",+`n ")
else
trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
from := oJSON[2]
trans := Trim(trans, ",+`n ")
Return trans
}
SendRequest(JS, str, tl, sl, proxy) {
static http
ComObjError(false)
if !http
{
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
( proxy && http.SetProxy(2, proxy) )
http.open( "get", "https translate.google.com ", Broken Link for safety 1 )
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send()
http.WaitForResponse(-1)
}
http.open( "POST", "https translate.google.com /translate_a/single?client=webapp&sl=" Broken Link for safety
. sl . "&tl=" . tl . "&hl=" . tl
. "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=0&ssel=0&tsel=0&pc=1&kc=1"
. "&tk=" . JS.("tk").(str), 1 )
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send("q=" . URIEncode(str))
http.WaitForResponse(-1)
Return http.responsetext
}
URIEncode(str, encoding := "UTF-8") {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
While code := NumGet(Var, A_Index - 1, "UChar") {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
Return UrlStr
}
GetJScript()
{
script =
(
var TKK = ((function() {
var a = 561666268;
var b = 1526272306;
return 406398 + '.' + (a + b);
})());
function b(a, b) {
for (var d = 0; d < b.length - 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}
function tk(a) {
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
(c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a `%= 1E6;
return a.toString() + "." + (a ^ h)
}
)
Return script
}
CreateScriptObj() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
Return ObjBindMethod(doc.parentWindow, "eval")
}
Code: Select all
; to en
MsgBox, % GoogleTranslate("こんにちは世界!",, "en")
; to ja
MsgBox, % GoogleTranslate("Hello, World!",, "ja")
Code: Select all
~LButton::
;Loop, {
clipboard := ""
MouseGetPos, x, y
KeyWait, LButton
MouseGetPos, x0, y0
If (abs(x-x0) > 1 or abs(y-y0) > 1){
Send ^c
ClipWait, 0
If (RegExmatch(clipboard, "[^\x00-\x7F]")) { ; WORKS
Tooltip, % GoogleTranslate(clipboard)
} Else {
Tooltip, % GoogleTranslate(clipboard,, "ja") ;DOES WORK
}
}
;}
Return
~Esc:: ToolTip
#Include GoogleTranslate.ahk ; use original function!
hi, i'm a new AHKer. I'm really impressive with your script about this translation. But because i'm new ) so i wanna know how to do when i translate from clipboard. Is this % GoogleTranslate(clipboard) or what? Sorry if this bothers youteadrinker wrote: ↑27 Feb 2020, 07:25r2997790Code: Select all
res := GoogleTranslate("今日",, "en") MsgBox, % res MsgBox, % RegExReplace(res, "s)\R\+.+")
Thank you, i did it. Such a powerful tool for meteadrinker wrote: ↑28 Jul 2020, 06:34@phucnguyenphi123
If you need to translate the Clipboard contents from some language to English, this is the right way.
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance, Force
CoordMode, Mouse, Screen
SoundBeep, 2500
Return
~LButton::
clipboard := ""
MouseGetPos, x, y
KeyWait, LButton
MouseGetPos, x0, y0
If (abs(x-x0) > 1 or abs(y-y0) > 1){
Send ^c
ClipWait, 0
If (RegExmatch(clipboard, "[^\x00-\x7F]")) { ; WORKS
r:= GoogleTranslate(clipboard)
} Else {
r:= GoogleTranslate(clipboard,, "ja") ;DOES WORK
}
} else Return
ToolTip % r.3 "`n`n" r.4
Return
F2:: ToolTip % r.3 "`n`n" r.4
~Esc:: ToolTip
GoogleTranslate(str, from := "auto", to := "en") { ;function by RRR based on teadrinkers GoogleTranslate()
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.( "(" json ")" )
step1:= oJSON[0][0][8][0][0][1], step2:= oJSON[0][0][8][1][0][1]
int:= (step1? "1: " step1: "") (step2? " 2: " step2: "")
loop, % lln:=oJSON[8][0].length
lng1.= oJSON[8][0][A_Index-1] (A_Index<lln? ", ": "")
loop, % lln:=oJSON[8][0].length
lng2.= oJSON[8][3][A_Index-1] (A_Index<lln? ", ": "")
lng := "a: " (ls:=oJSON[2]) " b: " lng1 " c: " lng2
Src.= Str ((Pr1:= ojson[0][1][3])? "`n[" Pr1 "]": "")
Dym:= ojson[7][1]
¬ ((Tof:= ojson[7][5])=0)? Src.= (Dym? "`n ►Did you mean: ▌" Dym "?": "")
: (Tof=-1)? Src.= (Dym? "`n ►Showing result for ▌" Dym "`n ►Translate instead ▌" Str: ""): ""
Loop % lln:= oJSON[0].length
mTr .= oJSON[0][A_Index-1][0] (A_Index<lln? "": ((Pr2:= ojson[0][1][2])? "`n[" Pr2 "]": ""))
Main:= Src "`n----------------------`n" mTr
loop, % ojson[1].length
{
trn.= "♦ " ojson[1][cW:=A_Index-1][0] "`n"
Loop, % ojson[1][cW][2].length
{
Loop, % ln:=ojson[1][cW][2][tW:=A_Index-1][1].length
tr.= ojson[1][cW][2][tW][1][A_Index-1] (A_Index<ln? ", ": "")
;. (Mod(A_Index, 5)=0&&A_Index<ln? "`n ": "")
trn.= " ▪ " ((ar:=ojson[1][cW][2][A_Index-1][4])? ar " ": "")
. ojson[1][cW][2][A_Index-1][0] " ► " tr "`n", tr:= ""
}
;} trn:= (trn? "▬ Translations of ►" oJSON[1][0][3] "◄`n" RTrim(trn, "`n"): "")
} trn:= (trn? "▬ Translations of ▌" oJSON[1][0][3] "`n" RTrim(trn, "`n"): "")
loop, % ojson[11].length
{
syn.= "♦ " ojson[11][cW:=A_Index-1][0] " ▌" ojson[11][cW][2] "`n"
Loop, % ojson[11][cw].length
Loop, % ojson[11][cW][cN:=A_Index].length
Loop, % ojson[11][cW][cN][sW:=A_Index-1].length
Loop, % ln:=ojson[11][cW][cN][sW][tW:=A_Index-1].length
syn.= (A_Index=1? " ▪ ": "") ojson[11][cW][cN][sW][tW][A_Index-1] (A_Index<ln? ", ": "`n")
} syn:= (syn? "▬ Synonims`n" RTrim(syn, "`n"): "")
loop, % ojson[12].length
{
def.= "♦ " ojson[12][cW:=A_Index-1][0] " ▌" ojson[12][cW][2] "`n"
loop, % ojson[12][cW][1].length
qu:= ojson[12][0][1][A_Index-1][2]
, def.= " ▪ " ojson[12][cW][1][A_Index-1][0] (qu? "`n """ qu """" "`n": "`n")
} def:= (def? "▬ Definitions`n" RTrim(def, "`n "): "")
loop, % ln:=ojson[13][0].length
exp.= " ♦ " ojson[13][0][A_Index-1][0] (A_Index<ln? "`n": "")
exp:= (exp? "▬ Examples`n" StrReplace(StrReplace(exp, "<b>", "▌"), "</b>"): "")
Return [int, lng, Main, trn, syn, def, exp, Str, ls, mTr]
}
SendRequest(JS, str, tl, sl, proxy) { ; by malcev
static http
ComObjError(false)
loop
{
if !http
{
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
¬:= ( proxy && http.SetProxy(2, proxy) )
http.open( "get", "https://translate.google.com", 1 )
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send()
http.WaitForResponse(-1)
}
http.open( "POST", "https://translate.google.com/translate_a/single?client=webapp&sl="
. sl . "&tl=" . tl . "&hl=en" ;. tl ; &hl=en by RRR
. "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=0&ssel=0&tsel=0&pc=1&kc=1"
. "&tk=" . JS.("tk").(str), 1 )
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send("q=" . URIEncode(str))
http.WaitForResponse(-1)
if !InStr(http.responsetext, "<form id=""captcha-form""")
break
http := ""
Sleep, 300 ; by RRR
}
Return http.responsetext
}
URIEncode(str, encoding := "UTF-8") {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
While code := NumGet(Var, A_Index - 1, "UChar") {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
Return UrlStr
}
GetJScript()
{
script =
(
var TKK = ((function() {
var a = 561666268;
var b = 1526272306;
return 406398 + '.' + (a + b);
})());
function b(a, b) {
for (var d = 0; d < b.length - 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}
function tk(a) {
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
(c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a `%= 1E6;
return a.toString() + "." + (a ^ h)
}
)
Return script
}
CreateScriptObj() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
Return ObjBindMethod(doc.parentWindow, "eval")
}