AutoHotkey Community

It is currently May 27th, 2012, 1:30 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: April 7th, 2009, 9:57 pm 
after netbook launcher, ill release my next script:
Google API Functions
three functions:
translate(search, from, to) translates any text
getlang(input) gets the language the input is written in
getcode(code) either get googles 2-char code or the name fo the language.

code:
Code:
;Google Language API by friese - 2009
;credits to olfen for urldownloadtofile function

;Example: get clipboard, check what langugae the content of it is and translate it to english, when Insert is pressed:
Ins::
oldclip := clipboard
2charlang := getlang(clipboard)
lang := getcode(2charlang)
result := translate(clipboard, 2charlang, "en")
msgbox Clipboard contained %oldclip%`nThat is %lang% and means`n%result%
return


translate(search, from, to) ;Translation, from and to are googles language-codes got by getlang
{
per := "%"
url = http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%search%&langpair=%from%%per%7C%to%
temp := urldownloadtovar(url)
StringReplace, temp, temp, {"responseData": {"translatedText":"
tmp = "}, "responseDetails": null, "responseStatus": 20
StringReplace, temp, temp, %tmp%
return temp
}

getlang(input) ;Returns Googles 2-character language-code, Input is text the language from should be outputted
{
url = http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=%input%
temp := urldownloadtovar(url)
StringReplace, temp, temp, {"responseData": {"language":"
stringleft, temp, temp, 2
return temp
}

getcode(code) ;Input either 2-char code or Long name, Output is the opposite
{
list =
(
af|Afrikaans
sq|Albanian
am|Amharic
ar|Arabic
hy|Armenian
az|Azerbaijani
eu|Basque
be|Belarusian
bn|Bengali
bh|Bihari
xx-bork|Bork,bork,bork!
bs|Bosnian
br|Breton
bg|Bulgarian
km|Cambodian
ca|Catalan
zh-CN|Chinese(Simplified)
zh-TW|Chinese(Traditional)
co|Corsican
hr|Croatian
cs|Czech
da|Danish
nl|Dutch
xx-elmer|ElmerFudd
en|English
eo|Esperanto
et|Estonian
fo|Faroese
tl|Filipino
fi|Finnish
fr|French
fy|Frisian
gl|Galician
ka|Georgian
de|German
el|Greek
gn|Guarani
gu|Gujarati
xx-hacker|Hacker
iw|Hebrew
hi|Hindi
hu|Hungarian
is|Icelandic
id|Indonesian
ia|Interlingua
ga|Irish
it|Italian
ja|Japanese
jw|Javanese
kn|Kannada
kk|Kazakh
xx-klingon|Klingon
ko|Korean
ku|Kurdish
ky|Kyrgyz
lo|Laothian
la|Latin
lv|Latvian
ln|Lingala
lt|Lithuanian
mk|Macedonian
ms|Malay
ml|Malayalam
mt|Maltese
mr|Marathi
mo|Moldavian
mn|Mongolian
ne|Nepali
no|Norwegian
nn|Norwegian(Nynorsk)
oc|Occitan
or|Oriya
ps|Pashto
fa|Persian
xx-piglatin|PigLatin
xx-pirate|Pirate
pl|Polish
pt-BR|Portuguese(Brazil)
pt-PT|Portuguese(Portugal)
pa|Punjabi
qu|Quechua
ro|Romanian
rm|Romansh
ru|Russian
gd|ScotsGaelic
sr|Serbian
sh|Serbo-Croatian
st|Sesotho
sn|Shona
sd|Sindhi
si|Sinhalese
sk|Slovak
sl|Slovenian
so|Somali
es|Spanish
su|Sundanese
sw|Swahili
sv|Swedish
tg|Tajik
ta|Tamil
tt|Tatar
te|Telugu
th|Thai
ti|Tigrinya
to|Tonga
tr|Turkish
tk|Turkmen
tw|Twi
ug|Uighur
uk|Ukrainian
ur|Urdu
uz|Uzbek
vi|Vietnamese
cy|Welsh
xh|Xhosa
yi|Yiddish
yo|Yoruba
zu|Zulu
)
loop, parse, list, `n
{
ifinstring, A_LoopField, %code%
{
stringsplit, temp, A_LoopField,|
if temp1 = %code%
 result = %temp2%
if temp2 = %code%
 result = %temp1%
}
}
return result
}



;from olfen, needed for g-api
UrlDownloadToVar(URL, Proxy="", ProxyBypass="") {
AutoTrim, Off
hModule := DllCall("LoadLibrary", "str", "wininet.dll")

If (Proxy != "")
AccessType=3
Else
AccessType=1
;INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
;INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
;INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
;INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS

io_hInternet := DllCall("wininet\InternetOpenA"
, "str", "" ;lpszAgent
, "uint", AccessType
, "str", Proxy
, "str", ProxyBypass
, "uint", 0) ;dwFlags

iou := DllCall("wininet\InternetOpenUrlA"
, "uint", io_hInternet
, "str", url
, "str", "" ;lpszHeaders
, "uint", 0 ;dwHeadersLength
, "uint", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item
, "uint", 0) ;dwContext

If (ErrorLevel != 0 or iou = 0) {
DllCall("FreeLibrary", "uint", hModule)
return 0
}

VarSetCapacity(buffer, 512, 0)
VarSetCapacity(NumberOfBytesRead, 4, 0)
Loop
{
  irf := DllCall("wininet\InternetReadFile", "uint", iou, "uint", &buffer, "uint", 512, "uint", &NumberOfBytesRead)
  NOBR = 0
  Loop 4  ; Build the integer by adding up its bytes. - ExtractInteger
    NOBR += *(&NumberOfBytesRead + A_Index-1) << 8*(A_Index-1)
  IfEqual, NOBR, 0, break
  ;BytesReadTotal += NOBR
  DllCall("lstrcpy", "str", buffer, "uint", &buffer)
  res = %res%%buffer%
}
StringTrimRight, res, res, 2

DllCall("wininet\InternetCloseHandle",  "uint", iou)
DllCall("wininet\InternetCloseHandle",  "uint", io_hInternet)
DllCall("FreeLibrary", "uint", hModule)
AutoTrim, on
return, res
}

if u have tipps for me please post them


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 9th, 2009, 2:40 pm 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
The example doesn't quite work on my system, maybe ANSI/Unicode issues (I'm running Win98SE)?

I had Romanian text in clipboard and it was detected as English. Then I had Japanese text and it detected it as Swahili. Moreover, since Google returned an error, apparently errors are not handled at all in the script - the 'translation' returned:
Code:
{"responseData": null, "responseDetails": "invalid translation language pair", "responseStatus": 40


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 10:08 am 
Offline

Joined: January 15th, 2008, 12:20 pm
Posts: 14
Russian text was detected as Swahili. Very funny and exciting code!
:lol: It will be very useful when it become working script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 12:17 pm 
text detection works not really cuz its afaik just an beta from google, hope itll be better in future


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 14th, 2009, 4:21 am 
Offline

Joined: January 15th, 2008, 12:20 pm
Posts: 14
Text detection may be doesn't work at Google side... but the script returns nothing good even if I make it to translate from one fixed language to another.

---
I tried to make UTF-convertation, so with it and with fixed language pair the code works well.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group