summaryrefslogtreecommitdiff
path: root/translate-widget/translate.lua
blob: 98a3e46f1e51346585fbad1e2e2cfabe778b9076 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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")
local capi = {keygrabber = keygrabber }

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

local translate_widget = wibox.widget {
    {
        image  = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg',
        resize = false,
        widget = wibox.widget.imagebox
    },
    {
        {
            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
    },
    layout  = wibox.layout.fixed.horizontal
}

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]

        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>')

        local w = wibox {
            width = 300,
            height = 80,
            ontop = true,
            screen = mouse.screen,
            expand = true,
            widget = translate_widget
        }
        awful.placement.top(w, { margins = {top = 25}})
        w.visible = true
        w:buttons(
            awful.util.table.join(
                awful.button({}, 1, function()
                    awful.spawn.with_shell("echo '" .. resp .. "' | xclip -selection clipboard")
                    w.visible = false
                end),
                awful.button({}, 3, function()
                    awful.spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard")
                    w.visible = false
                end)
            )
        )

        capi.keygrabber.run(function(_, key, event)
            if event == "release" then return end
            if key then
                capi.keygrabber.stop()
                w.visible = false
            end
        end)
    end
end

return {
    translate = translate
}