5 class BabelPlugin < Plugin
6 LANGS = %w{en fr de it pt es nl ru zh zt el ja ko}
7 def help(plugin, topic="")
8 "translate to <lang> <string> => translate from english to <lang>, translate from <lang> <string> => translate to english from <lang>, translate <fromlang> <tolang> <string> => translate from <fromlang> to <tolang>. Languages: #{LANGS.join(', ')}"
10 def translate(m, params)
12 trans_from = params[:fromlang] ? params[:fromlang] : 'en'
13 trans_to = params[:tolang] ? params[:tolang] : 'en'
14 trans_text = params[:phrase].to_s
16 query = "/babelfish/tr"
17 lang_match = langs.join("|")
18 unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/)
19 m.reply "invalid language: valid languagess are: #{langs.join(' ')}"
23 data_text = URI.escape trans_text
24 trans_pair = "#{trans_from}_#{trans_to}"
25 data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
27 # check cache for previous lookups
28 if @registry.has_key?("#{trans_pair}/#{data_text}")
29 m.reply @registry["#{trans_pair}/#{data_text}"]
33 http = @bot.httputil.get_proxy(URI.parse("http://babelfish.altavista.com"))
36 "content-type" => "application/x-www-form-urlencoded; charset=utf-8",
37 'accept-charset' => 'utf-8'
41 resp = http.post(query, data, headers)
43 if (resp.code == "200")
45 resp.body.each_line do |l|
50 debug "babelfish response: #{l}"
52 if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/)
56 @registry["#{trans_pair}/#{data_text}"] = answer
61 m.reply "couldn't parse babelfish response html :("
63 m.reply "couldn't talk to babelfish :("
68 plugin = BabelPlugin.new
69 plugin.map 'translate to :tolang *phrase'
70 plugin.map 'translate from :fromlang *phrase'
71 plugin.map 'translate :fromlang :tolang *phrase'