@cache = cache
end
+ # Many translators use Mechanize, which changed namespace around version 1.0
+ # To support both pre-1.0 and post-1.0 namespaces, we use these auxiliary
+ # method. The translator still needs to require 'mechanize' on initialization
+ # if it needs it.
+ def mechanize
+ return Mechanize if defined? Mechanize
+ return WWW::Mechanize
+ end
# whether the translator supports this direction
def support?(from, to)
end
def do_translate(text, from, to)
- @form ||= WWW::Mechanize.new.
+ @form ||= mechanize.new.
get('http://nifty.amikai.com/amitext/indexUTF8.jsp').
forms_with(:name => 'translateForm').last
@radio = @form.radiobuttons_with(:name => 'langpair').first
end
def open_form(name)
- WWW::Mechanize.new.get("http://www.excite.co.jp/world/#{name}").
+ mechanize.new.get("http://www.excite.co.jp/world/#{name}").
forms_with(:name => 'world').first
end
def initialize(cache)
require 'mechanize'
- form = WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/').
- forms_with(:name => 'frmTrText').first
- lang_list = form.fields_with(:name => 'lp').first
+ (_, lang_list) = parse_page
language_pairs = lang_list.options.map {|o| o.value.split('_')}.
reject {|p| p.empty?}
super(Translator::Direction.pairs(language_pairs), cache)
end
- def do_translate(text, from, to)
- @form ||= WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/').
- forms_with(:name => 'frmTrText').first
+ def parse_page
+ form = mechanize.new.get('http://babelfish.altavista.com/babelfish/').
+ forms_with(:name => 'frmTrText').first
+ lang_list = form.fields_with(:name => 'lp').first
+ [form, lang_list]
+ end
+ def do_translate(text, from, to)
+ unless @form && @lang_list
+ @form, @lang_list = parse_page
+ end
+
if @form.fields_with(:name => 'trtext').empty?
@form.add_field!('trtext', text)
else
translator = @default_translators.find {|t| @translators[t].support?(params[:from], params[:to])}
if translator
- cmd_translate m, params.merge({:translator => translator, :show_provider => true})
+ cmd_translate m, params.merge({:translator => translator, :show_provider => false})
else
# When translate command is used without source language, "auto" as source
# language is assumed. It means that google translator is used and we let google
m.reply _('No translator called %{name}') % {:name => tname}
end
end
+
+ # URL translation has nothing to do with Translators so let's make it
+ # separate, and Google exclusive for now
+ def cmd_translate_url(m, params)
+ params[:to] = @bot.config['translator.destination'] if params[:to].nil?
+ params[:from] ||= 'auto'
+
+ translate_url = "http://translate.google.com/translate?sl=%{from}&tl=%{to}&u=%{url}" % {
+ :from => params[:from],
+ :to => params[:to],
+ :url => CGI.escape(params[:url].to_s)
+ }
+
+ m.reply(translate_url)
+ end
end
plugin = TranslatorPlugin.new
req = Hash[*%w(from to).map { |e| [e.to_sym, /#{plugin.languages.join("|")}/] }.flatten]
+plugin.map 'translate [:from] [:to] :url',
+ :action => :cmd_translate_url, :requirements => req.merge(:url => %r{^https?://[^\s]*})
+plugin.map 'translator [:from] [:to] :url',
+ :action => :cmd_translate_url, :requirements => req.merge(:url => %r{^https?://[^\s]*})
plugin.map 'translate [:from] [:to] *phrase',
:action => :cmd_translator, :thread => true, :requirements => req
plugin.map 'translator [:from] [:to] *phrase',