-
Notifications
You must be signed in to change notification settings - Fork 36
/
google-translate.coffee
126 lines (120 loc) · 3.43 KB
/
google-translate.coffee
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
# Description:
# Allows Hubot to know many languages.
#
# Commands:
# hubot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
# hubot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
languages =
"af": "Afrikaans",
"sq": "Albanian",
"ar": "Arabic",
"az": "Azerbaijani",
"eu": "Basque",
"bn": "Bengali",
"be": "Belarusian",
"bg": "Bulgarian",
"ca": "Catalan",
"zh-CN": "Simplified Chinese",
"zh-TW": "Traditional Chinese",
"hr": "Croatian",
"cs": "Czech",
"da": "Danish",
"nl": "Dutch",
"en": "English",
"eo": "Esperanto",
"et": "Estonian",
"tl": "Filipino",
"fi": "Finnish",
"fr": "French",
"gl": "Galician",
"ka": "Georgian",
"de": "German",
"el": "Greek",
"gu": "Gujarati",
"ht": "Haitian Creole",
"iw": "Hebrew",
"hi": "Hindi",
"hu": "Hungarian",
"is": "Icelandic",
"id": "Indonesian",
"ga": "Irish",
"it": "Italian",
"ja": "Japanese",
"kn": "Kannada",
"ko": "Korean",
"la": "Latin",
"lv": "Latvian",
"lt": "Lithuanian",
"mk": "Macedonian",
"ms": "Malay",
"mt": "Maltese",
"no": "Norwegian",
"fa": "Persian",
"pl": "Polish",
"pt": "Portuguese",
"ro": "Romanian",
"ru": "Russian",
"sr": "Serbian",
"sk": "Slovak",
"sl": "Slovenian",
"es": "Spanish",
"sw": "Swahili",
"sv": "Swedish",
"ta": "Tamil",
"te": "Telugu",
"th": "Thai",
"tr": "Turkish",
"uk": "Ukrainian",
"ur": "Urdu",
"vi": "Vietnamese",
"cy": "Welsh",
"yi": "Yiddish"
getCode = (language,languages) ->
for code, lang of languages
return code if lang.toLowerCase() is language.toLowerCase()
module.exports = (robot) ->
language_choices = (language for _, language of languages).sort().join('|')
pattern = new RegExp('translate(?: me)?' +
"(?: from (#{language_choices}))?" +
"(?: (?:in)?to (#{language_choices}))?" +
'(.*)', 'i')
robot.respond pattern, (msg) ->
term = "\"#{msg.match[3]?.trim()}\""
origin = if msg.match[1] isnt undefined then getCode(msg.match[1], languages) else 'auto'
target = if msg.match[2] isnt undefined then getCode(msg.match[2], languages) else 'en'
msg.http("https://translate.google.com/translate_a/single")
.query({
client: 't'
hl: 'en'
sl: origin
ssel: 0
tl: target
tsel: 0
q: term
ie: 'UTF-8'
oe: 'UTF-8'
otf: 1
dt: ['bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't', 'at']
})
.header('User-Agent', 'Mozilla/5.0')
.get() (err, res, body) ->
if err
msg.send "Failed to connect to GAPI"
robot.emit 'error', err, res
return
try
if body.length > 4 and body[0] == '['
parsed = eval(body)
language = languages[parsed[2]]
parsed = parsed[0] and parsed[0][0] and parsed[0][0][0]
parsed and= parsed.trim()
if parsed
if msg.match[2] is undefined
msg.send "#{term} is #{language} for #{parsed}"
else
msg.send "The #{language} #{term} translates as #{parsed} in #{languages[target]}"
else
throw new SyntaxError 'Invalid JS code'
catch err
msg.send "Failed to parse GAPI response"
robot.emit 'error', err