-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtranslate.lua
131 lines (101 loc) · 3.6 KB
/
gtranslate.lua
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
local mod = {}
mod.APIKEY = ""
-- gtranslate
function mod.gtranslate()
if mod.APIKEY == "" then
hs.alert('You must enter your Google Cloud API KEY')
return
end
local GOOGLE_ENDPOINT = 'https://www.googleapis.com/language/translate/v2?target=%s&source=%s&key=%s&q=%s'
local API_KEY = mod.APIKEY
local target = mod.target
local source = mod.source
local alerts = {}
local current = hs.application.frontmostApplication()
local tab = nil
local copy = nil
local t = nil
local choices = {}
local chooser = hs.chooser.new(function(choosen)
if copy then copy:delete() end
if tab then tab:delete() end
if t then t:delete() end
current:activate()
hs.eventtap.keyStrokes(choosen.text)
end)
-- Removes all items in list
function reset()
chooser:choices({})
end
function setLang(so, ta)
source = so
target = ta
local style = {}
hs.alert.closeSpecific( alerts["langPrimary"], 0 )
hs.alert.closeSpecific( alerts["langSecondary"], 0 )
alerts["langPrimary"] = hs.alert.show( string.format('%s ⇢ %s', string.upper(source), string.upper(target)), { ["textSize"] = 50 }, 2 )
alerts["langSecondary"] = hs.alert.show( '⌘T to switch.', 2 )
end
tab = hs.hotkey.bind('', 'tab', function()
local id = chooser:selectedRow()
local item = choices[id]
-- If no row is selected, but tab was pressed
if not item then return end
chooser:query(item.subText)
reset()
updateChooser()
end)
t = hs.hotkey.bind('cmd', 't', function()
setLang(target, source)
reset()
end)
copy = hs.hotkey.bind('cmd', 'c', function()
local id = chooser:selectedRow()
local item = choices[id]
if item then
chooser:hide()
hs.pasteboard.setContents(item.text)
hs.alert.show("Copied to clipboard", 1)
else
hs.alert.show("No search result to copy", 1)
end
end)
function updateChooser()
local string = chooser:query()
local query = hs.http.encodeForQuery(string)
-- Reset list when no query is given
if string:len() == 0 then return reset() end
hs.http.asyncGet(string.format(GOOGLE_ENDPOINT, target, source, API_KEY, query), nil, function(status, data)
if not data then return end
local ok, results = pcall(function() return hs.json.decode(data) end)
if not ok then return end
hs.fnutils.each( results["data"]["translations"], function(result)
local translation = result["translatedText"]
if string == translation then return end
local choice = {
["text"] = translation,
["subText"] = string,
}
local found = hs.fnutils.find( choices, function( element )
return element["text"] == translation
end)
if found == nil then table.insert(choices, 1, choice) end
end)
hs.printf( hs.inspect(choices) )
chooser:choices(choices)
end)
end
chooser:queryChangedCallback(updateChooser)
chooser:searchSubText(false)
chooser:show()
setLang(source, target)
end
function mod.init( APIKEY, source, target, mods, key )
mod.APIKEY = APIKEY
mod.source = source or "fi"
mod.target = target or "en"
mods = mods or {"cmd", "alt", "ctrl"}
key = key or "T"
hs.hotkey.bind(mods, key, mod.gtranslate)
end
return mod