Gui, Add, text, x0 y0 w400 h20, 要翻译的文字 Gui, Add, edit, x0 y20 w400 h180 vsrc Gui, Add, text, x400 y0 w400 h20, 结果 Gui, Add, Edit, x400 y20 w400 h180 vdst gui, Add, Button, x0 y200 w800 h20 gtrans vtrans, 翻译 gui, Show, , 百度翻译测试 return trans: Gui, Submit, NoHide GuiControl, Disable, trans url := "http://fanyi.baidu.com/v2transapi" if RegExMatch(src,"[^a-zA-Z0-9\.\?\-\!\s]") slang := "zh",dlang := "en" else slang := "en",dlang := "zh" post := "from=" slang "&to=" dlang "&query=" encodeURIComponent(src) "&transtype=realtime&simple_means_flag=3" json := URLDownloadToVar(url,"UTF-8","POST",post) obj := json_toobj(json) trans_result := decodeu(obj.trans_result.data.1.dst) res := "翻译结果:`n" trans_result "`n`n" dict_result := obj.dict_result.simple_means.symbols if IsObject(dict_result.1) { res .= "词典结果:`n" for k,v in dict_result.1.parts { res .= k ". " v.part if IsObject(v.means) { for x,y in v.means { if IsObject(y) res .= decodeu(y.word_mean) ";" else res .= decodeu(y) ";" } res .= "`n" } } } if IsObject(obj.dict_result.cizu) { res .= "`n词组结果:`n" for k,v in obj.dict_result.cizu res .= k ". " decodeu(v.fanyi) "(" decodeu(v.cz_name) "):" v.jx.1.jx_en " " decodeu(v.jx.1.jx_zh) "`n" } GuiControl, , dst, % res GuiControl, Enable, trans return GuiClose: ExitApp json_toobj(str){ quot := """" ws := "`t`n`r " Chr(160) obj := {} objs := [] keys := [] isarrays := [] literals := [] y := nest := 0 StringGetPos, z, str, %quot% while !ErrorLevel { StringGetPos, x, str, %quot%,, % z + 1 while !ErrorLevel { StringMid, key, str, z + 2, x - z - 1 StringReplace, key, key, \\, \u005C, A If SubStr( key, 0 ) != "\" Break StringGetPos, x, str, %quot%,, % x + 1 } str := ( z ? SubStr( str, 1, z ) : "" ) quot SubStr( str, x + 2 ) StringReplace, key, key, \%quot%, %quot%, A StringReplace, key, key, \b, % Chr(08), A StringReplace, key, key, \t, % A_Tab, A StringReplace, key, key, \n, `n, A StringReplace, key, key, \f, % Chr(12), A StringReplace, key, key, \r, `r, A StringReplace, key, key, \/, /, A while y := InStr( key, "\u", 0, y + 1 ) if ( A_IsUnicode || Abs( "0x" SubStr( key, y + 2, 4 ) ) < 0x100 ) key := ( y = 1 ? "" : SubStr( key, 1, y - 1 ) ) Chr( "0x" SubStr( key, y + 2, 4 ) ) SubStr( key, y + 6 ) literals.insert(key) StringGetPos, z, str, %quot%,, % z + 1 } key := isarray := 1 Loop Parse, str, % "]}" { StringReplace, str, A_LoopField, [, [], A Loop Parse, str, % "[{" { if ( A_Index != 1 ) { objs.insert( obj ) isarrays.insert( isarray ) keys.insert( key ) obj := {} isarray := key := Asc( A_LoopField ) = 93 } if ( isarray ) { Loop Parse, A_LoopField, `,, % ws "]" if ( A_LoopField != "" ) obj[key++] := A_LoopField = quot ? literals.remove(1) : A_LoopField } else { Loop Parse, A_LoopField, `, Loop Parse, A_LoopField, :, % ws if ( A_Index = 1 ) key := A_LoopField = quot ? literals.remove(1) : A_LoopField else if ( A_Index = 2 && A_LoopField != "" ) obj[key] := A_LoopField = quot ? literals.remove(1) : A_LoopField } nest += A_Index > 1 } If !--nest Break pbj := obj obj := objs.remove() obj[key := keys.remove()] := pbj If ( isarray := isarrays.remove() ) key++ } Return obj } decodeu(ustr){ Loop { if !ustr break if RegExMatch(ustr,"^\s*\\u([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})(.*)",m) { word_u := Chr("0x" m2) Chr("0x" m1), ustr := m3, word_a := "" Unicode2Ansi(word_u,word_a,0) out .= word_a } else if RegExMatch(ustr, "^([a-zA-Z0-9\.\?\-\!\s]*)(.*)",n) { ustr := n2 out .= n1 } } return out } encodeURIComponent(p) { str:=Ansi2UTF8(p) res:=Encode(&str) return res } Encode(p) { SetFormat,integer,hex res := "" while,value := *p++ { if(value==33 || (value>=39 && value <=42) || value==45 || value ==46 || (value>=48 && value<=57) || (value>=65 && value<=90) || value==95 || (value>=97 && value<=122) || value==126) res .= Chr(value) Else { res .= "%" res .= SubStr(value,3,2) } } SetFormat,integer, d Return res } Ansi2UTF8(sString) { Ansi2Unicode(sString, wString, 0) Unicode2Ansi(wString, zString, 65001) Return zString } UTF82Ansi(zString) { Ansi2Unicode(zString, wString, 65001) Unicode2Ansi(wString, sString, 0) Return sString } Ansi2Unicode(ByRef sString, ByRef wString, CP = 0) { nSize := DllCall("MultiByteToWideChar" , "Uint", CP , "Uint", 0 , "Uint", &sString , "int", -1 , "Uint", 0 , "int", 0) VarSetCapacity(wString, nSize * 2) DllCall("MultiByteToWideChar" , "Uint", CP , "Uint", 0 , "Uint", &sString , "int", -1 , "Uint", &wString , "int", nSize) } Unicode2Ansi(ByRef wString, ByRef sString, CP = 0) { nSize := DllCall("WideCharToMultiByte" , "Uint", CP , "Uint", 0 , "Uint", &wString , "int", -1 , "Uint", 0 , "int", 0 , "Uint", 0 , "Uint", 0) VarSetCapacity(sString, nSize) DllCall("WideCharToMultiByte" , "Uint", CP , "Uint", 0 , "Uint", &wString , "int", -1 , "str", sString , "int", nSize , "Uint", 0 , "Uint", 0) } URLDownloadToVar(url, Encoding = "",Method="GET",postData=""){ hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1") if Method = GET { hObject.Open("GET",url) Try hObject.Send() catch e return -1 } else if Method = POST { hObject.Open("POST",url,False) hObject.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") hObject.SetRequestHeader("Accept", "*/*") hObject.SetRequestHeader("Referer", "http://fanyi.baidu.com/") hObject.SetRequestHeader("X-Requested-With", "XMLHttpRequest") Try hObject.Send(postData) catch e return -1 } if Encoding { oADO := ComObjCreate("adodb.stream") oADO.Type := 1 oADO.Mode := 3 oADO.Open() oADO.Write(hObject.ResponseBody) oADO.Position := 0 oADO.Type := 2 oADO.Charset := Encoding return oADO.ReadText(), oADO.Close() } return hObject.ResponseText }
60 queries in 2.167 seconds |