I've updated this to use the new Json() by polyethene found
here. It's a piece of a much larger script, so I'll just post the functions used as well:
Code:
::T_T::
T_T()
Return
T_T() {
T := Input("Text", "V T30", ".")
If(T == "")
{
SendInput, Cancelled.
Return
}
L := Input("To language", "V T15", ".")
If(L == "")
{
SendInput, Cancelled.
Return
}
If(Language_List_Check(L) == False)
{
SendInput, Unsupported language.
Return
}
S := UnHTM(UnSlashUnicode(UTF82Ansi(URL_DTF("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" uriEncode(Transform_Unicode(T)) "&langpair=%7C" L))))
SetKeyDelay, 1
Send, % String_Replace(String_Replace(String_Replace(String_Replace(json(S, "responseData.translatedText"), "!", "{!}", "All"), "^", "{^}", "All"), "&", "{&}", "All"), "$", "{$}", "All") "{Enter}"
Return
}
;---
URL_DTF(U) {
F := % A_Temp "\" Random_String(5) ".Tmp"
URLDownloadToFile, % U, % F
If(ErrorLevel == 1)
TrayTip, Error, A problem occured during download., 3
FileRead, R, % F
FileDelete, % F
Return R
}
Parse(V, S, N, D="") {
If(N == "L")
{
Loop, Parse, V, % S
I := A_Index
Loop, Parse, V, % S
If(A_Index == I)
Return A_LoopField
}
Else
{
Loop, Parse, V, % S
If(A_Index == N)
Return A_LoopField
}
Return D
}
Random_String(N) {
Loop, % N
{
Random, T, 1, 2
If(T == 1)
Random, S, 0, 9
Else
{
Random, S, 97, 122
Transform, S, Chr, % S
}
R := R S
}
Return R
}
String_Replace(S, T, R, A="") {
StringReplace, S, S, % T, % R, % A
Return S
}
Input(S, O, E) {
SendInput, % S ": "
Input, I, %O%, %E%
IfInString, ErrorLevel, Endkey
SendInput, % "{Backspace " StrLen(S) + StrLen(I) + 3 "}"
Else IfInString, ErrorLevel, Max
SendInput, % "{Backspace " StrLen(S) + StrLen(I) + 2 "}"
Else IfInString, ErrorLevel, Timeout
SendInput, % "{Backspace " StrLen(S) + StrLen(I) + 2 "}"
Return I
}
json(ByRef js, s, v = "") {
j = %js%
Loop, Parse, s, .
{
p = 2
RegExMatch(A_LoopField, "([+\-]?)([^[]+)((?:\[\d+\])*)", q)
Loop {
If (!p := RegExMatch(j, "(?<!\\)(""|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|"
. "(?<!\\)(""|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", x, p))
Return
Else If (x2 == q2 or q2 == "*") {
j = %x3%
z += p + StrLen(x2) - 2
If (q3 != "" and InStr(j, "[") == 1) {
StringTrimRight, q3, q3, 1
Loop, Parse, q3, ], [
{
z += 1 + RegExMatch(SubStr(j, 2, -1), "^(?:\s*((\[(?:[^[\]]++|(?-1))*\])|(\{(?:[^{\}]++|(?-1))*\})|[^,]*?)\s*(?:,|$)){" . SubStr(A_LoopField, 1) + 1 . "}", x)
j = %x1%
}
}
Break
}
Else p += StrLen(x)
}
}
If v !=
{
vs = "
If (RegExMatch(v, "^\s*(?:""|')*\s*([+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:""|')*\s*$", vx)
and (vx1 + 0 or vx1 == 0 or vx1 == "true" or vx1 == "false" or vx1 == "null" or vx1 == "nul"))
vs := "", v := vx1
StringReplace, v, v, ", \", All
js := SubStr(js, 1, z := RegExMatch(js, ":\s*", zx, z) + StrLen(zx) - 1) . vs . v . vs . SubStr(js, z + StrLen(x3) + 1)
}
Return, j == "false" ? 0 : j == "true" ? 1 : j == "null" or j == "nul"
? "" : SubStr(j, 1, 1) == """" ? SubStr(j, 2, -1) : j
}
Language_List_Check(L) {
A = af|sq|am|ar|hy|az|eu|be|bn|bh|xx-bork|bs|br|bg|km|ca|zh-CN|zh-TW|co|hr|cs|da|nl|xx-elmer|en|eo|et|fo|tl|fi|fr|fy|gl|ka|de|el|gn|gu|xx-hacker|ha|iw|hi|hu|is|id|ia|ga|it|ja|jw|kn|kk|rw|rn|xx-kilngon|ko|ku|ky|lo|la|lv|ln|lt|mk|mg|ms|ml|mt|mi|mr|mo|mn|sr-ME|ne|no|nn|oc|or|om|ps|fa|xx-pirate|pl|pt-BR|pt-PT|pa|qu|ro|rm|ru|gd|sr|sh|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|tt|te|th|ti|to|tr|tk|tw|ug|uk|ur|uz|vi|cy|xh|yi|yo|zu
Loop, Parse, A, |
If(L == A_LoopField)
Return True
Return False
}
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)
}
UriEncode(str)
{ ; v 0.3 / (w) 24.06.2008 by derRaphael / zLib-Style release
b_Format := A_FormatInteger
data := ""
SetFormat,Integer,H
Loop,Parse,str
if ((Asc(A_LoopField)>0x7f) || (Asc(A_LoopField)<0x30) || (asc(A_LoopField)=0x3d))
data .= "%" . ((StrLen(c:=SubStr(ASC(A_LoopField),3))<2) ? "0" . c : c)
Else
data .= A_LoopField
SetFormat,Integer,%b_format%
return data
}
UnSlashUnicode(s)
{
; unslash unicode sequences like \u0026
; by Mikhail Kuropyatnikov 2009 (micdelt@mail.ru)
rx = \\u([0-9a-fA-F]{4})
pos = 0
loop
{
pos := RegExMatch(s,rx,m,pos+1)
if (pos = 0)
break
StringReplace, s, s, %m%, % Chr("0x" . SubStr(m,3,4))
}
return s
}
Transform_Unicode(V) {
C := Clipboard
Clipboard := V
Transform, U, Unicode
Clipboard := C
Return U
}
UnHTM(HTM) {
Static HT
IfEqual,HT,, SetEnv,HT, % "ááââ´´ææàà&ååãã&au"
. "mlä&bdquo„¦¦&bull•ç縸¢¢&circˆ©©¤¤&dagger†&dagger‡°"
. "°÷÷ééêêèèððëë&euro€&fnofƒ½½¼¼¾¾>>&h"
. "ellip…ííîî¡¡ìì¿¿ïï««&ldquo“&lsaquo‹&lsquo‘<<&m"
. "acr¯&mdash—µµ··  &ndash–¬¬ññóóôô&oeligœòò&or"
. "dfªººøøõõöö¶¶&permil‰±±££"""»»&rdquo”®"
. "®&rsaquo›&rsquo’&sbquo‚&scaronš§§­¹¹²²³³ßßþþ&tilde˜&tim"
. "es×&trade™úúûûùù¨¨üüýý¥¥ÿÿ"
TXT := RegExReplace( HTM,"<[^>]+>" )
Loop, Parse, TXT, &`;
L := "&" A_LoopField ";", R .= (!(A_Index&1)) ? ( (!InStr(R,L,1)) ? L:"" ) : ""
StringTrimRight, R, R, 1
Loop, Parse, R , `;
If F := InStr( HT, A_LoopField )
StringReplace, TXT,TXT, %A_LoopField%`;, % SubStr( HT,F+StrLen(A_LoopField), 1 ), All
Else If ( SubStr( A_LoopField,2,1)="#" )
StringReplace, TXT, TXT, %A_LoopField%`;, % Chr(SubStr(A_LoopField,3)), All
Return RegExReplace( TXT, "(^\s*|\s*$)")
}
For example:T_T This sentence is written in French!.fr.
Note, you
must type the . button to end the input!
Watch this video for the example!A full list of supported languages (Ignore the hl=.. CBA to remove them all. Lol):
Code:
/*
Language List:
hl=af Afrikaans
hl=sq Albanian
hl=am Amharic
hl=ar Arabic
hl=hy Armenian
hl=az Azerbaijani
hl=eu Basque
hl=be Belarusian
hl=bn Bengali
hl=bh Bihari
hl=xx-bork Bork, bork, bork!
hl=bs Bosnian
hl=br Breton
hl=bg Bulgarian
hl=km Cambodian
hl=ca Catalan
hl=zh-CN Chinese (Simplified)
hl=zh-TW Chinese (Traditional)
hl=co Corsican
hl=hr Croatian
hl=cs Czech
hl=da Danish
hl=nl Dutch
hl=xx-elmer Elmer Fudd
hl=en English
hl=eo Esperanto
hl=et Estonian
hl=fo Faroese
hl=tl Filipino
hl=fi Finnish
hl=fr French
hl=fy Frisian
hl=gl Galician
hl=ka Georgian
hl=de German
hl=el Greek
hl=gn Guarani
hl=gu Gujarati
hl=xx-hacker Hacker
hl=ha Hausa
hl=iw Hebrew
hl=hi Hindi
hl=hu Hungarian
hl=is Icelandic
hl=id Indonesian
hl=ia Interlingua
hl=ga Irish
hl=it Italian
hl=ja Japanese
hl=jw Javanese
hl=kn Kannada
hl=kk Kazakh
hl=rw Kinyarwanda
hl=rn Kirundi
hl=xx-klingon Klingon
hl=ko Korean
hl=ku Kurdish
hl=ky Kyrgyz
hl=lo Laothian
hl=la Latin
hl=lv Latvian
hl=ln Lingala
hl=lt Lithuanian
hl=mk Macedonian
hl=mg Malagasy
hl=ms Malay
hl=ml Malayalam
hl=mt Maltese
hl=mi Maori
hl=mr Marathi
hl=mo Moldavian
hl=mn Mongolian
hl=sr-ME Montenegrin
hl=ne Nepali
hl=no Norwegian
hl=nn Norwegian (Nynorsk)
hl=oc Occitan
hl=or Oriya
hl=om Oromo
hl=ps Pashto
hl=fa Persian
hl=xx-pirate Pirate
hl=pl Polish
hl=pt-BR Portuguese (Brazil)
hl=pt-PT Portuguese (Portugal)
hl=pa Punjabi
hl=qu Quechua
hl=ro Romanian
hl=rm Romansh
hl=ru Russian
hl=gd Scots Gaelic
hl=sr Serbian
hl=sh Serbo-Croatian
hl=st Sesotho
hl=sn Shona
hl=sd Sindhi
hl=si Sinhalese
hl=sk Slovak
hl=sl Slovenian
hl=so Somali
hl=es Spanish
hl=su Sundanese
hl=sw Swahili
hl=sv Swedish
hl=tg Tajik
hl=ta Tamil
hl=tt Tatar
hl=te Telugu
hl=th Thai
hl=ti Tigrinya
hl=to Tonga
hl=tr Turkish
hl=tk Turkmen
hl=tw Twi
hl=ug Uighur
hl=uk Ukrainian
hl=ur Urdu
hl=uz Uzbek
hl=vi Vietnamese
hl=cy Welsh
hl=xh Xhosa
hl=yi Yiddish
hl=yo Yoruba
hl=zu Zulu
*/
I find it very useful for games without actual edit controls.. Where you just type a normal sentence in-game to have it translated.