]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/translator.rb
nickserv: add 'ghosted' to nickserv.nick_avail
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / translator.rb
index 39d3c1fe618ff676b412ed3de6c51867e391f965..a32ea16dfa059d56789a83ae0f4942f4b54c1e08 100644 (file)
@@ -39,7 +39,7 @@ class Translator
     @cache = cache
   end
 
+
   # whether the translator supports this direction
   def support?(from, to)
     from != to && @directions[from].include?(to)
@@ -230,13 +230,13 @@ class BabelfishTranslator < Translator
   end
 
   def do_translate(text, from, to)
-    if @form.fields_with(:name => 'trtext').first.empty?
+    if @form.fields_with(:name => 'trtext').empty?
       @form.add_field!('trtext', text)
     else
       @form.fields_with(:name => 'trtext').first.value = text
     end
     @lang_list.value = "#{from}_#{to}"
-    @form.submit.parser.search("td.s/div[@style]").inner_html
+    @form.submit.parser.search("div[@id='result']/div[@style]").inner_html
   end
 end
 
@@ -266,6 +266,9 @@ class TranslatorPlugin < Plugin
   Config.register Config::IntegerValue.new('translator.timeout',
     :default => 30, :validate => Proc.new{|v| v > 0},
     :desc => _("Number of seconds to wait for the translation service before timeout"))
+  Config.register Config::StringValue.new('translator.destination',
+    :default => "en",
+    :desc => _("Default destination language to be used with translate command"))
 
   TRANSLATORS = {
     'nifty' => NiftyTranslator,
@@ -315,13 +318,26 @@ class TranslatorPlugin < Plugin
     end
   end
 
+  def languages
+    @languages ||= @translators.map { |t| t.last.directions.keys }.flatten.uniq
+  end
+
   def update_default
-    @default_translators = bot.config['translator.default_list'] & @translators.keys 
+    @default_translators = bot.config['translator.default_list'] & @translators.keys
   end
 
   def cmd_translator(m, params)
-    from, to = params[:from], params[:to]
-    translator = @default_translators.find {|t| @translators[t].support?(from, to)}
+    params[:to] = @bot.config['translator.destination'] if params[:to].nil?
+
+    # Use google translate as translator if source language has not been given
+    # and auto-detect it
+    if params[:from].nil?
+      params[:from] = "auto"
+      translator = "google_translate"
+    else
+      translator = @default_translators.find {|t| @translators[t].support?(params[:from], params[:to])}
+    end
+
     if translator
       cmd_translate m, params.merge({:translator => translator, :show_provider => true})
     else
@@ -341,7 +357,7 @@ class TranslatorPlugin < Plugin
         end
         m.reply(if params[:show_provider]
                   _('%{translation} (provided by %{translator})') %
-                    {:translation => translation, :translator => tname}
+                    {:translation => translation, :translator => tname.gsub("_", " ")}
                 else
                   translation
                 end)
@@ -362,5 +378,9 @@ class TranslatorPlugin < Plugin
 end
 
 plugin = TranslatorPlugin.new
-plugin.map 'translator :from :to *phrase',
-           :action => :cmd_translator, :thread => true
+req = Hash[*%w(from to).map { |e| [e.to_sym, /#{plugin.languages.join("|")}/] }.flatten]
+
+plugin.map 'translate [:from] [:to] *phrase',
+           :action => :cmd_translator, :thread => true, :requirements => req
+plugin.map 'translator [:from] [:to] *phrase',
+           :action => :cmd_translator, :thread => true, :requirements => req