[translate] fix #184 - move from yandex to watson

This commit is contained in:
streetturtle 2020-11-09 10:57:52 -05:00
parent 6a229f6fce
commit 3cf9ce8ffe
2 changed files with 100 additions and 84 deletions

View File

@ -0,0 +1,12 @@
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" version="1">
<path style="opacity:0.5" d="M 16,32 32,44 V 20 L 16,4 Z"/>
<path style="fill:#e4e4e4" d="M 6,16 C 4.892,16 4,16.892 4,18 V 42 C 4,43.108 4.892,44 6,44 H 32 V 16 Z"/>
<path style="opacity:0.2" d="M 15.403,22.554 C 16.003,22.97 16.557,23.478 17.065,24.078 L 16.165,25.186 C 15.611,24.493 15.034,23.916 14.433,23.454 L 15.403,22.554 M 15.403,26.918 C 15.957,27.333 16.488,27.795 16.996,28.303 L 16.096,29.48 C 15.587,28.834 15.033,28.28 14.433,27.818 L 15.402,26.918 M 15.679,31.697 16.788,32.459 C 16.233,34.352 15.425,36.176 14.363,37.931 L 13.185,37.03 C 14.247,35.321 15.079,33.544 15.679,31.697 M 18.934,22 20.25,22.277 C 20.158,22.738 20.019,23.177 19.835,23.593 H 25.237 V 24.839 H 19.281 L 18.657,25.878 C 18.472,26.248 18.172,26.64 17.757,27.056 L 16.925,26.087 C 17.802,25.025 18.472,23.663 18.933,22 M 18.587,26.295 H 24.959 C 24.959,27.726 24.936,29.065 24.89,30.312 H 25.791 V 31.559 H 24.821 C 24.728,32.529 24.659,33.475 24.613,34.399 H 25.652 V 35.576 H 24.475 C 24.29,37.192 23.643,38 22.535,38 22.258,38 21.866,37.954 21.358,37.862 L 21.15,36.684 C 21.75,36.777 22.212,36.823 22.535,36.823 22.858,36.823 23.066,36.407 23.159,35.576 H 17.756 C 17.941,34.329 18.102,32.99 18.241,31.559 H 17.271 V 30.312 H 18.379 C 18.472,29.019 18.541,27.68 18.587,26.295 M 19.626,30.312 H 23.574 C 23.62,29.342 23.643,28.396 23.643,27.472 H 19.834 C 19.788,28.488 19.718,29.435 19.626,30.312 M 21.358,27.819 C 21.866,28.142 22.327,28.511 22.743,28.927 L 21.912,29.966 C 21.496,29.504 21.035,29.088 20.527,28.719 L 21.358,27.818 M 23.297,34.399 C 23.389,33.429 23.459,32.482 23.505,31.559 H 19.557 C 19.465,32.575 19.349,33.521 19.211,34.399 H 23.297 M 21.15,31.905 C 21.704,32.228 22.212,32.598 22.674,33.013 L 21.773,34.052 C 21.312,33.544 20.827,33.106 20.319,32.736 L 21.15,31.905 M 13.877,23.663 V 34.537 H 12.63 V 33.359 H 11.453 V 35.16 H 10.206 V 23.662 H 13.877 M 11.453,32.182 H 12.63 V 24.84 H 11.453 V 32.182"/>
<path style="opacity:0.2" d="M 4,42 V 43 C 4,44.108 4.892,45 6,45 H 32 V 44 H 6 C 4.892,44 4,43.108 4,42 Z"/>
<path style="fill:#a3a3a3" d="M 16,32 32,44 V 20 L 16,4 Z"/>
<path style="opacity:0.2;fill:#ffffff" d="M 6,16 C 4.892,16 4,16.892 4,18 V 19 C 4,17.892 4.892,17 6,17 H 32 V 16 Z"/>
<path style="fill:#2095f2" d="M 16,4 V 32 H 42 C 43.108,32 44,31.108 44,30 V 6 C 44,4.892 43.108,4 42,4 Z"/>
<path style="fill:#ffffff" d="M 28.221,19.374 28.614,17.902 C 29.055,16.306 29.473,14.441 29.865,12.748 H 29.963 C 30.381,14.417 30.822,16.306 31.264,17.902 L 31.632,19.374 H 28.221 M 33.349,26 H 37.177 L 32.171,10 H 27.828 L 22.821,26 H 26.502 L 27.484,22.196 H 32.367 L 33.349,26"/>
<path style="opacity:0.2" d="M 44,30 C 44,31.108 43.108,32 42,32 H 16 V 33 H 42 C 43.108,33 44,32.108 44,31 Z"/>
<path style="opacity:0.2;fill:#ffffff" d="M 16,4 V 5 H 42 C 43.108,5 44,5.892 44,7 V 6 C 44,4.892 43.108,4 42,4 Z"/>
</svg>

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -7,16 +7,17 @@
------------------------------------------------- -------------------------------------------------
local awful = require("awful") local awful = require("awful")
local spawn = require("awful.spawn")
local capi = {keygrabber = keygrabber } local capi = {keygrabber = keygrabber }
local https = require("ssl.https") local beautiful = require("beautiful")
local json = require("json") local json = require("json")
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local gears = require("gears") local gears = require("gears")
local gfs = require("gears.filesystem") local gfs = require("gears.filesystem")
local BASE_URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate' local TRANSLATE_CMD = [[bash -c 'curl -s -u "apikey:%s" -H "Content-Type: application/json" -d '\''{"text": ["%s"], "model_id":"%s"}'\'' "%s/v3/translate?version=2018-05-01"']]
local ICON = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg' local ICON = os.getenv("HOME") .. '/.config/awesome/awesome-wm-widgets/translate-widget/gnome-translate.svg'
--- Returns two values - string to translate and direction: --- Returns two values - string to translate and direction:
-- 'dog enfr' -> 'dog', 'en-fr' -- 'dog enfr' -> 'dog', 'en-fr'
@ -31,88 +32,85 @@ local function extract(input_string)
return word, lang return word, lang
end end
--- Simple url encoder - replaces spaces with '+' sign local function show_warning(message)
-- @param url to encode naughty.notify{
local function urlencode(url) preset = naughty.config.presets.critical,
if (url) then title = 'Translate Shell',
url = string.gsub(url, " ", "+") text = message}
end
return url
end end
local w = wibox { local w = awful.popup {
width = 300, widget = {},
visible = false,
border_width = 1, border_width = 1,
maximum_width = 400,
width = 400,
border_color = '#66ccff', border_color = '#66ccff',
ontop = true, ontop = true,
expand = true, bg = beautiful.bg_normal,
bg = '#1e252c',
max_widget_size = 500,
shape = function(cr, width, height) shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, 3) gears.shape.rounded_rect(cr, width, height, 3)
end end,
} }
awful.placement.top(w, { margins = {top = 40}})
w:setup {
{
{
image = ICON,
widget = wibox.widget.imagebox,
resize = false
},
id = 'img',
layout = wibox.container.margin(_, 0, 0, 10)
},
{
{
id = 'header',
widget = wibox.widget.textbox
},
{
id = 'src',
widget = wibox.widget.textbox
},
{
id = 'res',
widget = wibox.widget.textbox
},
id = 'text',
layout = wibox.layout.fixed.vertical,
},
id = 'left',
layout = wibox.layout.fixed.horizontal
}
--- Main function - takes the user input and shows the widget with translation --- Main function - takes the user input and shows the widget with translation
-- @param request_string - user input (dog enfr) -- @param request_string - user input (dog enfr)
local function translate(to_translate, lang, api_key) local function translate(to_translate, lang, api_key, url)
local urll = BASE_URL .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. api_key
local resp_json, code = https.request(urll) local cmd = string.format(TRANSLATE_CMD, api_key, to_translate, lang, url)
if (code == 200 and resp_json ~= nil) then spawn.easy_async(cmd, function (stdout, stderr)
local resp = json.decode(resp_json).text[1] if stderr ~= '' then
show_warning(stderr)
end
w.left.text.header:set_markup('<big>' .. lang .. '</big>') local resp = json.decode(stdout)
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>')
awful.placement.top(w, { margins = {top = 40}}) w:setup {
{
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) {
image = ICON,
-- calculate height of the widget widget = wibox.widget.imagebox,
w.height = h1 + h2 + h3 + 20 resize = false
-- try to vertically align the icon },
w.left.img:set_top((h1 + h2 + h3 + 20 - 48)/2) valigh = 'center',
layout = wibox.container.place,
},
{
{
id = 'src',
markup = '<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> ' .. to_translate .. '</span>',
widget = wibox.widget.textbox
},
{
id = 'res',
markup = '<b>' .. lang:sub(4) .. '</b>: <span color="#FFFFFF"> ' .. resp.translations[1].translation .. '</span>',
widget = wibox.widget.textbox
},
id = 'text',
layout = wibox.layout.fixed.vertical,
},
id = 'left',
spacing = 8,
layout = wibox.layout.fixed.horizontal
},
bg = beautiful.bg_normal,
forced_width = 400,
widget = wibox.container.background
},
color = beautiful.bg_normal,
margins = 8,
widget = wibox.container.margin
}
w.visible = true w.visible = true
w:buttons( w:buttons(
awful.util.table.join( awful.util.table.join(
awful.button({}, 1, function() awful.button({}, 1, function()
awful.spawn.with_shell("echo '" .. resp .. "' | xclip -selection clipboard") awful.spawn.with_shell("echo '" .. resp.translations[1].translation .. "' | xclip -selection clipboard")
w.visible = false w.visible = false
end), end),
awful.button({}, 3, function() awful.button({}, 3, function()
@ -129,40 +127,46 @@ local function translate(to_translate, lang, api_key)
w.visible = false w.visible = false
end end
end) end)
else end)
naughty.notify({
preset = naughty.config.presets.critical,
title = 'Translate Widget Error',
text = resp_json,
})
end
end end
local prompt = awful.widget.prompt()
local input_widget = wibox { local input_widget = wibox {
visible = false,
width = 300, width = 300,
height = 100,
maxmimum_width = 300,
maxmimum_height = 900,
ontop = true, ontop = true,
screen = mouse.screen, screen = mouse.screen,
expand = true, expand = true,
bg = '#1e252c', bg = beautiful.bg_normal,
max_widget_size = 500, max_widget_size = 500,
border_width = 1, border_width = 1,
border_color = '#66ccff', border_color = '#66ccff',
shape = function(cr, width, height) shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, 3) gears.shape.rounded_rect(cr, width, height, 3)
end end,
} }
local prompt = awful.widget.prompt() input_widget:setup{
{
input_widget:setup { prompt,
layout = wibox.container.margin, bg = beautiful.bg_normal,
prompt, widget = wibox.container.background
left = 10 },
margins = 8,
widget = wibox.container.margin
} }
local function show_translate_prompt(api_key) local function launch(args)
local args = args or {}
local api_key = args.api_key
local url = args.url
awful.placement.top(input_widget, { margins = {top = 40}, parent = awful.screen.focused()}) awful.placement.top(input_widget, { margins = {top = 40}, parent = awful.screen.focused()})
input_widget.height = 40
input_widget.visible = true input_widget.visible = true
awful.prompt.run { awful.prompt.run {
@ -181,7 +185,7 @@ local function show_translate_prompt(api_key)
}) })
return return
end end
translate(to_translate, lang, api_key) translate(to_translate, lang, api_key, url)
end, end,
done_callback = function() done_callback = function()
input_widget.visible = false input_widget.visible = false
@ -190,5 +194,5 @@ local function show_translate_prompt(api_key)
end end
return { return {
show_translate_prompt = show_translate_prompt launch = launch
} }