]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/translator.rb
url plugin: only chop non-word characters on 404
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / translator.rb
index 0e09cb4855e717dd81473ead737443331570accd..a32ea16dfa059d56789a83ae0f4942f4b54c1e08 100644 (file)
@@ -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
   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