This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/translate.lua

67 lines
1.5 KiB
Lua
Raw Normal View History

2015-06-08 21:53:37 +02:00
--[[
2015-04-26 13:28:31 +02:00
-- Translate text using Google Translate.
2015-06-08 21:53:37 +02:00
-- 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
--]]
2015-04-26 13:28:31 +02:00
do
function translate(source_lang, target_lang, text)
2015-06-08 21:53:37 +02:00
local path = "http://translate.google.com/translate_a/single"
2015-04-26 13:28:31 +02:00
-- URL query parameters
local params = {
2015-06-08 21:53:37 +02:00
client = "t",
2015-04-26 13:28:31 +02:00
ie = "UTF-8",
oe = "UTF-8",
hl = "de",
2015-06-08 21:53:37 +02:00
dt = "t",
2015-04-26 13:28:31 +02:00
tl = target_lang or "de",
2015-06-08 21:53:37 +02:00
sl = source_lang or "auto",
2015-04-26 13:28:31 +02:00
text = URL.escape(text)
}
local query = format_http_params(params, true)
local url = path..query
local res, code = https.request(url)
-- Return nil if error
if code > 200 then return nil end
2015-06-08 21:53:37 +02:00
local trans = res:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "")
2015-04-26 13:28:31 +02:00
2015-06-08 21:53:37 +02:00
return trans
2015-04-26 13:28:31 +02:00
end
function run(msg, matches)
-- Third pattern
if #matches == 1 then
print("First")
local text = matches[1]
return translate(nil, nil, text)
end
-- Second pattern
if #matches == 2 then
print("Second")
local target = matches[1]
local text = matches[2]
return translate(nil, target, text)
end
2015-06-08 21:53:37 +02:00
-- First pattern
2015-04-26 13:28:31 +02:00
if #matches == 3 then
print("Third")
local source = matches[1]
local target = matches[2]
local text = matches[3]
return translate(source, target, text)
end
end
return {
description = "<EFBFBD>bersetze Text",
2015-04-28 17:49:11 +02:00
usage = {"/translate [Text]"},
patterns = {"^/translate ([%w]+),([%a]+) (.+)","^/translate ([%w]+) (.+)","^/translate (.+)",},
2015-04-26 13:28:31 +02:00
run = run
}
end