Created
August 26, 2010 22:10
-
-
Save andsve/552356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module("modules/tr/tr") | |
function parse_message(bot, msg) | |
local i,j,sender,chan,q = string.find(msg, ":(.-)!.- PRIVMSG (.-) :" .. tostring(bot.config.triggerprefix) .. "tr(.*)") | |
if not (i == nil) then | |
if not (string.sub(chan, 1, 1) == "#") then | |
chan = sender | |
end | |
if q == "" then | |
bot:say(chan, "Usage: tr <fromlang> <tolang> <translationstring>, language list: http://gist.github.com/552383") | |
return | |
end | |
local i,j,langfrom,langto,langstring = string.find(q, " ([%S]-) ([%S]-) (.+)") | |
if not (i == nil) then | |
if (langfrom == "auto") then | |
langfrom = "" | |
end | |
local langtable = {auto = "Detect language", | |
af = "Afrikaans", | |
sq = "Albanian", | |
ar = "Arabic", | |
hy = "Armenian", | |
az = "Azerbaijani", | |
eu = "Basque", | |
be = "Belarusian", | |
bg = "Bulgarian", | |
ca = "Catalan", | |
hr = "Croatian", | |
cs = "Czech", | |
da = "Danish", | |
nl = "Dutch", | |
en = "English", | |
et = "Estonian", | |
tl = "Filipino", | |
fi = "Finnish", | |
fr = "French", | |
gl = "Galician", | |
ka = "Georgian", | |
de = "German", | |
el = "Greek", | |
ht = "Haitian Creole", | |
iw = "Hebrew", | |
hi = "Hindi", | |
hu = "Hungarian", | |
is = "Icelandic", | |
id = "Indonesian", | |
ga = "Irish", | |
it = "Italian", | |
ja = "Japanese", | |
ko = "Korean", | |
lv = "Latvian", | |
lt = "Lithuanian", | |
mk = "Macedonian", | |
ms = "Malay", | |
mt = "Maltese", | |
no = "Norwegian", | |
fa = "Persian", | |
pl = "Polish", | |
pt = "Portuguese", | |
ro = "Romanian", | |
ru = "Russian", | |
sr = "Serbian", | |
sk = "Slovak", | |
sl = "Slovenian", | |
es = "Spanish", | |
sw = "Swahili", | |
sv = "Swedish", | |
th = "Thai", | |
tr = "Turkish", | |
uk = "Ukrainian", | |
ur = "Urdu", | |
vi = "Vietnamese", | |
cy = "Welsh", | |
yi = "Yiddish"} | |
langtable["zh-CN"] = "Chinese (Simplified)" | |
langtable["zh-TW"] = "Chinese (Traditional)" | |
local http = require("socket.http") | |
local ltn12 = require("ltn12") | |
local langstring_html = string.gsub(langstring, " ", "+") | |
local request_body = "q=" .. tostring(langstring_html) .. "&v=1.0&langpair=" .. tostring(langfrom) .. "%7C" .. tostring(langto) | |
local b, c, h = http.request("http://ajax.googleapis.com/ajax/services/language/translate?" .. request_body) | |
if not (b == nil) then | |
local i,j,trans = string.find(b, '"translatedText":%s?"(.-)"') | |
if not (i == nil) then | |
if (langfrom == "") then | |
langfrom = "auto" | |
end | |
bot:say(chan, "[" .. langtable[langfrom] .. " - " .. langtable[langto] .. "] " .. trans) | |
else | |
local i,j,err = string.find(b, '"responseDetails":%s?"(.-)"') | |
if not (i == nil) then | |
bot:say(chan, "Could not translate, error: " .. tostring(err)) | |
else | |
bot:say(chan, "Error while translating!") | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment