Skip to content

Commit

Permalink
provide pronunciation in languages
Browse files Browse the repository at this point in the history
this will let you obtain a link to listen pronunciation, supports language auto-detection
  • Loading branch information
Benjamín Eidelman committed Apr 7, 2015
1 parent 21c8846 commit 2ce22f2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/google-translate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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
# hubot pronounce <phrase> in <language> - Provides pronounciation of <phrase> (<language> is optional)

languages =
"af": "Afrikaans",
Expand Down Expand Up @@ -112,3 +113,32 @@ module.exports = (robot) ->
else
msg.send "The #{language} #{term} translates as #{parsed} in #{languages[target]}"


pattern = new RegExp('pronounce ' +
'(.*?)' +
"(?: (?:in )?(#{language_choices}))?$", 'i')
robot.respond pattern, (msg) ->
term = "\"#{msg.match[1]}\""
origin = if msg.match[2] isnt undefined then getCode(msg.match[2], languages) else 'auto'

msg.http("https://translate.google.com/translate_a/t")
.query({
client: 't'
hl: 'en'
multires: 1
sc: 1
sl: origin
ssel: 0
tl: 'en'
tsel: 0
uptl: "en"
text: term
})
.header('User-Agent', 'Mozilla/5.0')
.get() (err, res, body) ->
data = body
if data.length > 4 and data[0] == '['
parsed = eval(data)
language =languages[parsed[2]]
parsed = parsed[0] and parsed[0][0] and parsed[0][0][0]
msg.send "http://translate.google.com/translate_tts?ie=UTF-8&q=#{encodeURIComponent(term)}&tl=#{getCode(language, languages)}"

0 comments on commit 2ce22f2

Please sign in to comment.