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, 翻译
gui, Show, , 翻译测试
return
trans:
Gui, Submit, NoHide
url := "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc"
post := "type=AUTO&i=" encodeURIComponent(src) "&doctype=json&xmlVersion=1.8&keyfrom=fanyi.web&ue=UTF-8&action=FY_BY_CLICKBUTTON&typoResult=true"
json := URLDownloadToVar(url,"UTF-8","POST",post)
obj := json_toobj(json)
if (obj.errorcode="0")
{
dst := obj.translateResult.1.1.tgt
a := obj.smartResult.entries
for k,v in a
dst .= "`n" (A_Index=1 ? "智能翻译:" : Ceil(A_index-1) "." v)
GuiControl, , dst, % dst
}
else
GuiControl, , dst, % obj.errorcode
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
}
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)
}
}
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=""){ ;网址,编码,请求方式,post数据
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")
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 1.976 seconds |