2017-12-07 23:31:07 +01:00
|
|
|
-------------------------------------------------
|
2017-12-27 19:30:56 +01:00
|
|
|
-- Translate Widget based on the Yandex.Translate API
|
|
|
|
-- https://tech.yandex.com/translate/
|
2017-12-07 23:31:07 +01:00
|
|
|
|
|
|
|
-- @author Pavel Makhov
|
|
|
|
-- @copyright 2017 Pavel Makhov
|
|
|
|
-------------------------------------------------
|
|
|
|
|
2017-12-27 19:30:56 +01:00
|
|
|
--package.path = package.path .. ";/home/streetturtle/.config/awesome/awesome-wm-widgets/secrets.lua"
|
2017-12-04 17:56:34 +01:00
|
|
|
local secrets = require("secrets")
|
2017-12-07 23:31:07 +01:00
|
|
|
|
2017-12-04 17:56:34 +01:00
|
|
|
local awful = require("awful")
|
2017-12-07 23:31:07 +01:00
|
|
|
local capi = {keygrabber = keygrabber }
|
2017-12-04 17:56:34 +01:00
|
|
|
local https = require("ssl.https")
|
2017-12-07 23:31:07 +01:00
|
|
|
local json = require("json")
|
2017-12-27 19:30:56 +01:00
|
|
|
local naughty = require("naughty")
|
2017-12-04 17:56:34 +01:00
|
|
|
local wibox = require("wibox")
|
|
|
|
|
2017-12-07 23:31:07 +01:00
|
|
|
local API_KEY = secrets.translate_widget_api_key
|
|
|
|
local BASE_URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
|
2017-12-04 17:56:34 +01:00
|
|
|
|
2017-12-07 23:31:07 +01:00
|
|
|
--- Returns two values - string to translate and direction:
|
|
|
|
-- 'dog enfr' -> 'dog', 'en-fr'
|
|
|
|
-- @param input_string user's input which consists of
|
|
|
|
-- text to translate and direction, 'dog enfr'
|
2017-12-04 17:56:34 +01:00
|
|
|
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
|
2017-12-27 19:30:56 +01:00
|
|
|
if lang == nil then
|
|
|
|
naughty.notify({
|
|
|
|
preset = naughty.config.presets.critical,
|
|
|
|
title = 'Translate Widget Error',
|
|
|
|
text = 'Language is not provided',
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2017-12-04 17:56:34 +01:00
|
|
|
return word, lang
|
|
|
|
end
|
|
|
|
|
2017-12-07 23:31:07 +01:00
|
|
|
--- Simple url encoder - replaces spaces with '+' sign
|
|
|
|
-- @param url to encode
|
|
|
|
local function urlencode(url)
|
|
|
|
if (url) then
|
|
|
|
url = string.gsub(url, " ", "+")
|
2017-12-04 17:56:34 +01:00
|
|
|
end
|
2017-12-07 23:31:07 +01:00
|
|
|
return url
|
2017-12-04 17:56:34 +01:00
|
|
|
end
|
|
|
|
|
2017-12-06 23:24:21 +01:00
|
|
|
local w = wibox {
|
|
|
|
width = 300,
|
|
|
|
ontop = true,
|
|
|
|
screen = mouse.screen,
|
|
|
|
expand = true,
|
|
|
|
bg = '#1e252c',
|
|
|
|
max_widget_size = 500
|
|
|
|
}
|
|
|
|
|
|
|
|
w:setup {
|
2017-12-04 23:36:14 +01:00
|
|
|
{
|
2017-12-07 23:31:07 +01:00
|
|
|
{
|
|
|
|
image = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg',
|
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
resize = false
|
|
|
|
},
|
|
|
|
id = 'img',
|
|
|
|
layout = wibox.container.margin(_, 0, 0, 10)
|
2017-12-04 23:36:14 +01:00
|
|
|
},
|
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',
|
2017-12-07 23:31:07 +01:00
|
|
|
layout = wibox.layout.fixed.vertical,
|
2017-12-05 22:22:33 +01:00
|
|
|
},
|
2017-12-06 23:24:21 +01:00
|
|
|
id = 'left',
|
2017-12-04 23:02:37 +01:00
|
|
|
layout = wibox.layout.fixed.horizontal
|
2017-12-04 17:56:34 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 23:31:07 +01:00
|
|
|
--- Main function - takes the user input and shows the widget with translations
|
2017-12-04 17:56:34 +01:00
|
|
|
local function translate(request_string)
|
|
|
|
local to_translate, lang = extract(request_string)
|
2017-12-07 23:31:07 +01:00
|
|
|
local urll = BASE_URL .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. API_KEY
|
2017-12-04 17:56:34 +01:00
|
|
|
|
|
|
|
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-06 23:24:21 +01:00
|
|
|
w.left.text.header:set_markup('<big>' .. lang .. '</big>')
|
2017-12-07 23:31:07 +01:00
|
|
|
w.left.text.src:set_markup('<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> ' .. to_translate .. '</span>')
|
|
|
|
w.left.text.res:set_markup('<b>' .. lang:sub(4) .. '</b>: <span color="#FFFFFF"> ' .. resp .. '</span>')
|
2017-12-04 17:56:34 +01:00
|
|
|
|
2017-12-04 23:02:37 +01:00
|
|
|
awful.placement.top(w, { margins = {top = 25}})
|
2017-12-07 23:31:07 +01:00
|
|
|
|
|
|
|
local h1 = w.left.text.header:get_height_for_width(w.width, w.screen)
|
|
|
|
local h2 = w.left.text.src:get_height_for_width(w.width, w.screen)
|
|
|
|
local h3 = w.left.text.res:get_height_for_width(w.width, w.screen)
|
|
|
|
|
|
|
|
w.height = h1 + h2 + h3 + 20
|
|
|
|
w.left.img:set_top((h1 + h2 + h3 + 20 - 48)/2)
|
|
|
|
|
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-27 19:30:56 +01:00
|
|
|
else
|
|
|
|
naughty.notify({
|
|
|
|
preset = naughty.config.presets.critical,
|
|
|
|
title = 'Translate Widget Error',
|
|
|
|
text = 'resp_json',
|
|
|
|
})
|
2017-12-04 17:56:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
translate = translate
|
|
|
|
}
|