translate nutzt nun Bing
This commit is contained in:
		
							
								
								
									
										67
									
								
								plugins/pluginsold/translate.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								plugins/pluginsold/translate.lua
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
 | 
			
		||||
--[[
 | 
			
		||||
-- Translate text using Google Translate.
 | 
			
		||||
-- 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
 | 
			
		||||
 | 
			
		||||
function translate(source_lang, target_lang, text)
 | 
			
		||||
  local path = "http://translate.google.com/translate_a/single"
 | 
			
		||||
  -- URL query parameters
 | 
			
		||||
  local params = {
 | 
			
		||||
    client = "t",
 | 
			
		||||
    ie = "UTF-8",
 | 
			
		||||
    oe = "UTF-8",
 | 
			
		||||
    hl = "de",
 | 
			
		||||
    dt = "t",
 | 
			
		||||
    tl = target_lang or "de",
 | 
			
		||||
    sl = source_lang or "auto",
 | 
			
		||||
    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
 | 
			
		||||
  local trans = res:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "")
 | 
			
		||||
 | 
			
		||||
  return trans
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
  -- First pattern
 | 
			
		||||
  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", 
 | 
			
		||||
  usage = {"/translate [Text]"},
 | 
			
		||||
  patterns = {"^/translate ([%w]+),([%a]+) (.+)","^/translate ([%w]+) (.+)","^/translate (.+)",}, 
 | 
			
		||||
  run = run 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
@@ -1,49 +1,72 @@
 | 
			
		||||
 | 
			
		||||
--[[
 | 
			
		||||
-- Translate text using Google Translate.
 | 
			
		||||
-- 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
 | 
			
		||||
--]]
 | 
			
		||||
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
 | 
			
		||||
-- DO NOT USE WITHOUT PERMISSION
 | 
			
		||||
do
 | 
			
		||||
 | 
			
		||||
function translate(source_lang, target_lang, text)
 | 
			
		||||
  local path = "http://translate.google.com/translate_a/single"
 | 
			
		||||
  -- URL query parameters
 | 
			
		||||
  local params = {
 | 
			
		||||
    client = "t",
 | 
			
		||||
    ie = "UTF-8",
 | 
			
		||||
    oe = "UTF-8",
 | 
			
		||||
    hl = "de",
 | 
			
		||||
    dt = "t",
 | 
			
		||||
    tl = target_lang or "de",
 | 
			
		||||
    sl = source_lang or "auto",
 | 
			
		||||
    text = URL.escape(text)
 | 
			
		||||
local mime = require("mime")
 | 
			
		||||
 | 
			
		||||
local bing_key = cred_data.bing_key
 | 
			
		||||
local accountkey = mime.b64(bing_key..':'..bing_key)
 | 
			
		||||
 | 
			
		||||
local function translate(source_lang, target_lang, text)
 | 
			
		||||
  if not target_lang then target_lang = 'de' end
 | 
			
		||||
  local url = 'https://api.datamarket.azure.com/Bing/MicrosoftTranslator/Translate?$format=json&Text=%27'..URL.escape(text)..'%27&To=%27'..target_lang..'%27&From=%27'..source_lang..'%27'
 | 
			
		||||
  local response_body = {}
 | 
			
		||||
  local request_constructor = {
 | 
			
		||||
      url = url,
 | 
			
		||||
      method = "GET",
 | 
			
		||||
      sink = ltn12.sink.table(response_body),
 | 
			
		||||
      headers = {
 | 
			
		||||
	    Authorization = "Basic "..accountkey
 | 
			
		||||
	  }
 | 
			
		||||
  }
 | 
			
		||||
  local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
 | 
			
		||||
  if not ok or response_code ~= 200 then return 'Ein Fehler ist aufgetreten.' end
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
  local trans = res:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "")
 | 
			
		||||
  local trans = json:decode(table.concat(response_body)).d.results[1].Text
 | 
			
		||||
 | 
			
		||||
  return trans
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function run(msg, matches)
 | 
			
		||||
local function detect_language(text)
 | 
			
		||||
  local url = 'https://api.datamarket.azure.com/Bing/MicrosoftTranslator/Detect?$format=json&Text=%27'..URL.escape(text)..'%27'
 | 
			
		||||
  local response_body = {}
 | 
			
		||||
  local request_constructor = {
 | 
			
		||||
      url = url,
 | 
			
		||||
      method = "GET",
 | 
			
		||||
      sink = ltn12.sink.table(response_body),
 | 
			
		||||
      headers = {
 | 
			
		||||
	    Authorization = "Basic "..accountkey
 | 
			
		||||
	  }
 | 
			
		||||
  }
 | 
			
		||||
  local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
 | 
			
		||||
  if not ok or response_code ~= 200 then return 'en' end
 | 
			
		||||
  
 | 
			
		||||
  local language = json:decode(table.concat(response_body)).d.results[1].Code
 | 
			
		||||
  return language
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function run(msg, matches)
 | 
			
		||||
  if matches[1] == 'whatlang' and matches[2] then
 | 
			
		||||
    local text = matches[2]
 | 
			
		||||
	local lang = detect_language(text)
 | 
			
		||||
	return 'Erkannte Sprache: '..lang
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  -- Third pattern
 | 
			
		||||
  if #matches == 1 then
 | 
			
		||||
    print("First")
 | 
			
		||||
    local text = matches[1]
 | 
			
		||||
    return translate(nil, nil, text)
 | 
			
		||||
	local language = detect_language(text)
 | 
			
		||||
    return translate(language, nil, text)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  -- Second pattern
 | 
			
		||||
  if #matches == 2 then
 | 
			
		||||
  if #matches == 3 and matches[1] == "to:" then
 | 
			
		||||
    print("Second")
 | 
			
		||||
    local target = matches[1]
 | 
			
		||||
    local text = matches[2]
 | 
			
		||||
    return translate(nil, target, text)
 | 
			
		||||
    local target = matches[2]
 | 
			
		||||
    local text = matches[3]
 | 
			
		||||
	local language = detect_language(text)
 | 
			
		||||
    return translate(language, target, text)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  -- First pattern
 | 
			
		||||
@@ -59,8 +82,18 @@ end
 | 
			
		||||
 | 
			
		||||
return {
 | 
			
		||||
  description = "<EFBFBD>bersetze Text", 
 | 
			
		||||
  usage = {"/translate [Text]"},
 | 
			
		||||
  patterns = {"^/translate ([%w]+),([%a]+) (.+)","^/translate ([%w]+) (.+)","^/translate (.+)",}, 
 | 
			
		||||
  usage = {
 | 
			
		||||
    "/translate [Text]: <20>bersetze Text in deutsch",
 | 
			
		||||
    "/translate to:Zielsprache [Text]: <20>bersetze Text in Zielsprache",
 | 
			
		||||
    "/translate Quellsprache,Zielsprache [Text]: <20>bersetze Text von beliebiger Sprache in beliebige Sprache",
 | 
			
		||||
	"/whatlang [Text]: Gibt erkannte Sprache zur<75>ck"
 | 
			
		||||
  },
 | 
			
		||||
  patterns = {
 | 
			
		||||
    "^/translate ([%w]+),([%a]+) (.+)",
 | 
			
		||||
    "^/translate (to%:)([%w]+) (.+)",
 | 
			
		||||
    "^/translate (.+)",
 | 
			
		||||
	"^/(whatlang) (.+)"
 | 
			
		||||
  }, 
 | 
			
		||||
  run = run 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user