translate plugin works now

This commit is contained in:
Akamaru 2015-06-08 21:53:37 +02:00
parent 43eebda05d
commit 09899ac662

View File

@ -1,17 +1,21 @@
--[[
-- Translate text using Google Translate. -- Translate text using Google Translate.
-- http://translate.google.com/translate_a/t?client=z&ie=UTF-8&oe=UTF-8&hl=en&tl=en&text=hello -- http://translate.google.com/translate_a/single?client=t&ie=UTF-8&oe=UTF-8&hl=en&dt=t&tl=en&sl=auto&text=hello
--]]
do do
function translate(source_lang, target_lang, text) function translate(source_lang, target_lang, text)
local path = "http://translate.google.com/translate_a/t" local path = "http://translate.google.com/translate_a/single"
-- URL query parameters -- URL query parameters
local params = { local params = {
client = "z", -- JSON client = "t",
ie = "UTF-8", ie = "UTF-8",
oe = "UTF-8", oe = "UTF-8",
hl = "de", hl = "de",
dt = "t",
tl = target_lang or "de", tl = target_lang or "de",
sl = source_lang or "", sl = source_lang or "auto",
text = URL.escape(text) text = URL.escape(text)
} }
@ -19,21 +23,11 @@ function translate(source_lang, target_lang, text)
local url = path..query local url = path..query
local res, code = https.request(url) local res, code = https.request(url)
if res == nil then
return 'Text kann momentan nicht übersetzt werden. Probiere es später erneut. URL: '..url
end
-- Return nil if error -- Return nil if error
if code > 200 then return nil end if code > 200 then return nil end
local trans = res:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "")
local trans = json:decode(res) return trans
local sentences = ""
-- Join multiple sencentes
for k,sentence in pairs(trans.sentences) do
sentences = sentences..sentence.trans..'\n'
end
return sentences
end end
function run(msg, matches) function run(msg, matches)
@ -52,7 +46,7 @@ function run(msg, matches)
return translate(nil, target, text) return translate(nil, target, text)
end end
-- Frist pattern -- First pattern
if #matches == 3 then if #matches == 3 then
print("Third") print("Third")
local source = matches[1] local source = matches[1]