-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtranslate.lua
54 lines (46 loc) · 1.82 KB
/
translate.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
local json = require("json")
local chatMessages = {}
local function translateText(input, targetLang)
local result = ""
local response = web.get("http://translate.googleapis.com", "/translate_a/single?client=gtx&sl=auto&tl=" .. targetLang .. "&dt=t&q=" .. input)
for k, v in pairs(json.decode(response.body)[1]) do
result = result .. v[1]
end
return result
end
function onFireEvent(event)
-- if the attacker is localplayer play hitsound and add a hitmarker to the table
if event:getName() == "player_say" then
chatMessages[#chatMessages+1] = {
["userid"] = event:getInt("userid"),
["translated"] = translateText(event:getString("text"), "en")
}
end
end
function onDraw()
ui.setNextWindowSize(Vec2(300, 100))
ui.beginComplexWindow("chat translator", 131) -- no background + no resize + no titlebar
draw.drawBlurRect(ui.getCurrentWindowPos(), ui.getCurrentWindowPos() + ui.getCurrentWindowSize(), 1)
ui.label("chat translator")
ui.separator()
ui.separator()
for i=0,#chatMessages-1 do
ui.label(chatMessages[#chatMessages-i]["translated"])
ui.separator()
end
ui.endWindow()
-- draw the send chat message window if menu open
if ui.isMenuOpen() then
ui.setNextWindowSize(Vec2(300, 100))
ui.beginWindow("send translated message")
ui.textInput("message", "message to translate")
ui.textInput("target language (e.g ru,en,fr)", "target language")
if ui.button("send message") then
eclipse.clientCmd("say " .. translateText(ui.getConfigStr("message to translate"), ui.getConfigStr("target language")))
end
ui.endWindow()
end
end
eclipse.addEventListener("player_say", false)
eclipse.registerHook("fireEvent", onFireEvent)
eclipse.registerHook("draw", onDraw)