2015-03-21 03:39:25 +00:00
|
|
|
local JSON = require("json")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2014-08-20 06:41:45 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
-- 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
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local Translator = {
|
|
|
|
trans_servers = {
|
|
|
|
"http://translate.google.cn",
|
|
|
|
"http://translate.google.com",
|
|
|
|
},
|
|
|
|
trans_path = "/translate_a/t",
|
|
|
|
trans_params = {
|
|
|
|
client = "z", -- client z returns normal JSON result
|
|
|
|
ie = "UTF-8",
|
|
|
|
oe = "UTF-8",
|
|
|
|
hl = "en",
|
|
|
|
tl = "en",
|
|
|
|
sl = nil, -- we don't specify source languagae to detect language
|
|
|
|
},
|
|
|
|
default_lang = "en",
|
|
|
|
}
|
|
|
|
|
|
|
|
function Translator:getTransServer()
|
|
|
|
return G_reader_settings:readSetting("trans_server") or self.trans_servers[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
-- return decoded JSON table from translate server
|
|
|
|
--]]
|
|
|
|
function Translator:loadPage(target_lang, source_lang, text)
|
2014-09-25 14:19:36 +00:00
|
|
|
local socket = require('socket')
|
|
|
|
local url = require('socket.url')
|
|
|
|
local http = require('socket.http')
|
|
|
|
local https = require('ssl.https')
|
|
|
|
local ltn12 = require('ltn12')
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
local request, sink = {}, {}
|
|
|
|
local query = ""
|
|
|
|
self.trans_params.tl = target_lang
|
|
|
|
self.trans_params.sl = source_lang
|
|
|
|
for k,v in pairs(self.trans_params) do
|
|
|
|
query = query .. k .. '=' .. v .. '&'
|
|
|
|
end
|
|
|
|
local parsed = url.parse(self:getTransServer())
|
|
|
|
parsed.path = self.trans_path
|
|
|
|
parsed.query = query .. "text=" .. url.escape(text)
|
|
|
|
|
|
|
|
-- HTTP request
|
|
|
|
request['url'] = url.build(parsed)
|
|
|
|
request['method'] = 'GET'
|
|
|
|
request['sink'] = ltn12.sink.table(sink)
|
|
|
|
http.TIMEOUT, https.TIMEOUT = 10, 10
|
|
|
|
local httpRequest = parsed.scheme == 'http' and http.request or https.request
|
2016-02-16 02:08:04 +00:00
|
|
|
-- first argument returned by skip is code
|
|
|
|
local _, headers, status = socket.skip(1, httpRequest(request))
|
2014-08-20 06:41:45 +00:00
|
|
|
|
|
|
|
-- raise error message when network is unavailable
|
|
|
|
if headers == nil then
|
|
|
|
error("Network is unreachable")
|
|
|
|
end
|
|
|
|
|
2015-08-23 18:39:08 +00:00
|
|
|
if status ~= "HTTP/1.1 200 OK" then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("translator HTTP status not okay:", status)
|
2015-08-23 18:39:08 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
local content = table.concat(sink)
|
2015-08-23 18:39:08 +00:00
|
|
|
if content ~= "" and string.sub(content, 1,1) == "{" then
|
2015-03-21 03:39:25 +00:00
|
|
|
local ok, result = pcall(JSON.decode, content)
|
2014-08-20 06:41:45 +00:00
|
|
|
if ok and result then
|
|
|
|
return result
|
|
|
|
else
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("translator error:", result)
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
2015-08-23 18:39:08 +00:00
|
|
|
else
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("not JSON in translator response:", content)
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Translator:detect(text)
|
|
|
|
local result = self:loadPage("en", nil, text)
|
|
|
|
if result then
|
|
|
|
local src_lang = result.src
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("detected language:", src_lang)
|
2014-08-20 06:41:45 +00:00
|
|
|
return src_lang
|
2014-08-20 07:46:43 +00:00
|
|
|
else
|
|
|
|
return self.default_lang
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Translator
|