1 class BabelPlugin < Plugin
2 LANGS = %w{en fr de it pt es nl ru zh zt el ja ko}
3 BASEURL = 'http://babelfish.yahoo.com'
5 Config.register Config::EnumValue.new('translate.default_from',
6 :values => LANGS, :default => 'en',
7 :desc => "Default language to translate from")
8 Config.register Config::EnumValue.new('translate.default_to',
9 :values => LANGS, :default => 'en',
10 :desc => "Default language to translate to")
12 def help(plugin, topic="")
15 "translate cache [view|clear] => view or clear the translate cache contents"
17 from = @bot.config['translate.default_from']
18 to = @bot.config['translate.default_to']
19 "translate to <lang> <string> => translate from #{from} to <lang>, translate from <lang> <string> => translate to #{to} from <lang>, translate <fromlang> <tolang> <string> => translate from <fromlang> to <tolang>. If <string> is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}. Other topics: cache"
23 def translate(m, params)
25 trans_from = params[:fromlang] ? params[:fromlang] : @bot.config['translate.default_from']
26 trans_to = params[:tolang] ? params[:tolang] : @bot.config['translate.default_to']
27 trans_text = params[:phrase].to_s
29 lang_match = langs.join("|")
30 unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/)
31 m.reply "invalid language: valid languagess are: #{langs.join(' ')}"
35 data_text = CGI.escape trans_text
36 trans_pair = "#{trans_from}_#{trans_to}"
38 if (trans_text =~ /^http:\/\//) && (URI.parse(trans_text) rescue nil)
39 m.reply 'webpage translation is not currently supported'
42 # url = BASEURL+'/translate_url' +
43 # "?lp=#{trans_pair}&trurl=#{data_text}"
45 # return Utils.get_first_pars([url], 1, :message => m)
48 data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
50 # check cache for previous lookups
51 if @registry.has_key?("#{trans_pair}/#{data_text}")
52 m.reply @registry["#{trans_pair}/#{data_text}"]
57 "content-type" => "application/x-www-form-urlencoded; charset=utf-8"
60 query = "/translate_txt"
63 body = @bot.httputil.get(BASEURL+query,
68 m.reply "http error: #{e.message}"
74 m.reply "couldn't talk to babelfish :("
75 when /^\s*<div id="result"><div style="[^"]*">(.*?)<\/div><\/div>\s*$/
79 @registry["#{trans_pair}/#{data_text}"] = answer
83 when /^\s+<option value="#{trans_pair}"\s+SELECTED>/
84 m.reply "couldn't parse babelfish response html :("
86 m.reply "babelfish doesn't support translation from #{trans_from} to #{trans_to}"
90 def cache_mgmt(m, params)
91 cmd = params[:cmd].intern
95 @registry.each { |key, val|
96 cache << "%s => %s" % [key, val]
98 m.reply "translate cache: #{cache.inspect}"
101 @registry.each { |key, val|
105 @registry.delete(key)
107 cache_mgmt(m, :cmd => 'view')
113 plugin = BabelPlugin.new
115 plugin.default_auth('cache', false)
117 plugin.map 'translate to :tolang *phrase', :thread => true
118 plugin.map 'translate from :fromlang *phrase', :thread => true
119 plugin.map 'translate cache :cmd', :action => :cache_mgmt, :auth_path => 'cache!', :requirements => { :cmd => /view|clear/ }
120 plugin.map 'translate :fromlang :tolang *phrase', :thread => true