-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgoogle-translate-widget.lua
More file actions
48 lines (38 loc) · 1.02 KB
/
google-translate-widget.lua
File metadata and controls
48 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-- name = "Google Translate"
-- data_source = "https://translate.google.com"
-- type = "widget"
-- author = "Andrey Gavrilov"
-- version = "1.3"
local json = require "json"
local uri = "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto"
local text_from = ""
function on_resume()
ui:show_text("Tap to enter text")
end
function on_click()
dialogs:show_edit_dialog("Enter text")
end
function on_dialog_action(text)
if text == "" or text == -1 then
on_resume()
else
text_from = text
translate(text)
end
end
function translate(str)
http:get(uri.."&tl="..system:lang().."&dt=t&q="..str)
end
function on_network_result(result)
local t = json.decode(result)
local text_to = ""
if not t or not t[1] or type(t[1]) ~= "table" then
ui:show_text("Error: invalid response")
return
end
for i, v in ipairs(t[1]) do
text_to = text_to..v[1]
end
local lang_from = t[3]
ui:show_lines({text_from.." ("..lang_from..")"}, {text_to})
end