2017-12-04 17:56:34 +01:00
|
|
|
package.path = package.path .. ";../../secrets.lua"
|
|
|
|
local secrets = require("secrets")
|
|
|
|
local awful = require("awful")
|
|
|
|
local json = require("json")
|
|
|
|
local https = require("ssl.https")
|
|
|
|
local wibox = require("wibox")
|
2017-12-05 22:22:33 +01:00
|
|
|
local capi = {keygrabber = keygrabber }
|
2017-12-04 17:56:34 +01:00
|
|
|
|
|
|
|
local api_key = secrets.translate_widget_api_key
|
|
|
|
local base_url = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
|
|
|
|
|
|
|
|
-- extracts string for translation and langs
|
|
|
|
local function extract(input_string)
|
|
|
|
local word, lang = input_string:match('^(.+)%s(%a%a%a%a)$')
|
|
|
|
|
|
|
|
if word ~= nill and lang ~= nill then
|
|
|
|
lang = lang:sub(1, 2) .. '-' .. lang:sub(3)
|
|
|
|
end
|
|
|
|
return word, lang
|
|
|
|
end
|
|
|
|
|
|
|
|
-- replaces spaces with '+' sign
|
|
|
|
local function urlencode(str)
|
|
|
|
if (str) then
|
|
|
|
str = string.gsub(str, " ", "+")
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2017-12-04 23:02:37 +01:00
|
|
|
local translate_widget = wibox.widget {
|
2017-12-04 23:36:14 +01:00
|
|
|
{
|
|
|
|
image = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg',
|
|
|
|
resize = false,
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
},
|
2017-12-05 22:22:33 +01:00
|
|
|
{
|
|
|
|
{
|
|
|
|
id = 'header',
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'src',
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'res',
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
|
|
|
id = 'text',
|
|
|
|
layout = wibox.layout.flex.vertical
|
|
|
|
},
|
2017-12-04 23:02:37 +01:00
|
|
|
layout = wibox.layout.fixed.horizontal
|
2017-12-04 17:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
local function translate(request_string)
|
|
|
|
local to_translate, lang = extract(request_string)
|
|
|
|
local urll = base_url .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. api_key
|
|
|
|
|
|
|
|
local resp_json, code = https.request(urll)
|
|
|
|
if (code == 200 and resp_json ~= nil) then
|
|
|
|
local resp = json.decode(resp_json).text[1]
|
|
|
|
|
2017-12-05 22:22:33 +01:00
|
|
|
translate_widget.text.header:set_markup('<big>' .. lang.. '</big>')
|
|
|
|
translate_widget.text.src:set_markup('<span color="#FFFFFF"> ' .. to_translate .. '</span>')
|
|
|
|
translate_widget.text.res:set_markup('<span color="#FFFFFF"> ' .. resp .. '</span>')
|
2017-12-04 17:56:34 +01:00
|
|
|
|
|
|
|
local w = wibox {
|
|
|
|
width = 300,
|
2017-12-04 23:36:14 +01:00
|
|
|
height = 80,
|
2017-12-04 17:56:34 +01:00
|
|
|
ontop = true,
|
|
|
|
screen = mouse.screen,
|
2017-12-04 23:02:37 +01:00
|
|
|
expand = true,
|
2017-12-04 17:56:34 +01:00
|
|
|
widget = translate_widget
|
|
|
|
}
|
2017-12-04 23:02:37 +01:00
|
|
|
awful.placement.top(w, { margins = {top = 25}})
|
2017-12-04 17:56:34 +01:00
|
|
|
w.visible = true
|
|
|
|
w:buttons(
|
|
|
|
awful.util.table.join(
|
2017-12-05 22:22:33 +01:00
|
|
|
awful.button({}, 1, function()
|
|
|
|
awful.spawn.with_shell("echo '" .. resp .. "' | xclip -selection clipboard")
|
2017-12-04 17:56:34 +01:00
|
|
|
w.visible = false
|
|
|
|
end),
|
|
|
|
awful.button({}, 3, function()
|
2017-12-05 22:22:33 +01:00
|
|
|
awful.spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard")
|
2017-12-04 17:56:34 +01:00
|
|
|
w.visible = false
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
)
|
2017-12-05 22:22:33 +01:00
|
|
|
|
|
|
|
capi.keygrabber.run(function(_, key, event)
|
|
|
|
if event == "release" then return end
|
|
|
|
if key then
|
|
|
|
capi.keygrabber.stop()
|
|
|
|
w.visible = false
|
|
|
|
end
|
|
|
|
end)
|
2017-12-04 17:56:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
translate = translate
|
|
|
|
}
|