diff --git a/README_ru.md b/README_ru.md index 3dcd9b7..6e273e1 100644 --- a/README_ru.md +++ b/README_ru.md @@ -45,6 +45,7 @@ First line
Second line * `system:copy_to_clipboard(string)` - копирует строку в буфер обмена; * `system:get_from_clipboard()` - возвращает строку из буфера обмена: * `system:share_text(string)` - открывает системный диалог "Поделиться"; +* `system:get_lang()` - возвращает выбранный в системе язык; Результат выполнения shell-команды приходит в колбек `on_shell_result(string)`. diff --git a/community/google-translate-widget.lua b/community/google-translate-widget.lua new file mode 100644 index 0000000..d3570be --- /dev/null +++ b/community/google-translate-widget.lua @@ -0,0 +1,36 @@ +-- name = "Google Translate" +-- data_source = "https://translate.google.com" +-- type = "widget" +-- author = "Andrey Gavrilov" +-- version = "1.0" + +local json = require "json" +local text_from = "" +local text_color = ui:get_secondary_text_color() + +function on_alarm() + ui:show_text("Tap to enter text") +end + +function on_click() + ui:show_edit_dialog("Введите текст") +end + +function on_dialog_action(text) + if text == "" then + on_alarm() + else + text_from = text + translate(text) + end +end + +function translate(str) + http:get("http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto".."&tl="..system:get_lang().."&dt=t&q="..str) +end + +function on_network_result(result) + local t = json.decode(result) + local text_to = t[1][1][1] + ui:show_lines({text_from}, {text_to}) +end diff --git a/community/icndb-translate-widget.lua b/community/icndb-translate-widget.lua new file mode 100644 index 0000000..2b6bc8b --- /dev/null +++ b/community/icndb-translate-widget.lua @@ -0,0 +1,57 @@ +-- name = "Chuck Norris translated" +-- description = "Jokes with translation" +-- data_source = "icndb.com" +-- type = "widget" +-- author = "Alexander Ivanitsa (griva99@gmail.com)" +-- version = "1.0" + +local joke = "" +local txt = "" + +function urlencode(str) + if (str) then + str = string.gsub(str, "\n", "\r\n") + str = string.gsub(str, "([^%w _ %- . ~])", function (c) return string.format ("%%%02X", string.byte(c)) end) + str = string.gsub(str, " ", "+") + end + return str +end + +function urldecode(str) + str = string.gsub(str, "+", " ") + str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) + return str +end + +function on_alarm() + http:get("http://api.icndb.com/jokes/random") +end + +function on_network_result(result) + local first_letter = string.sub(result, 1, 1) + local textColor = ui:get_secondary_text_color() + + joke = ajson:get_value(result, "object object:value string:joke") + local id = ajson:get_value(result, "object object:value string:id") + txt = ""..tostring(id)..": "..joke + ui:show_text(txt) +end + +-- translate -- +function on_click(idx) + local lang = system:get_lang() + local link_href="http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl="..lang.."&dt=t&q=" + http:get(link_href..urlencode(joke), "translate") +end + +function on_network_result_translate(result) + local js = require "json" + local tt= "" + local elem = js.decode(result)[1] + for k,v in pairs(elem) do + if type(v[1]) == "string" then + tt = tt..v[1] + end + end + ui:show_lines({txt,"---",tt}) +end diff --git a/samples/bad-script5.lua b/samples/bad-script5.lua new file mode 100644 index 0000000..c9ca73c --- /dev/null +++ b/samples/bad-script5.lua @@ -0,0 +1,3 @@ +function on_resume() + ui:show_aaa() +end diff --git a/samples/lang-sample.lua b/samples/lang-sample.lua new file mode 100644 index 0000000..e4702e8 --- /dev/null +++ b/samples/lang-sample.lua @@ -0,0 +1,3 @@ +function on_resume() + ui:show_text("Phone language: "..system:get_lang()) +end