4 # :title: Translator plugin for rbot
6 # Author:: Yaohan Chen <yaohan.chen@gmail.com>
7 # Copyright:: (C) 2007 Yaohan Chen
10 # This plugin allows using rbot to translate text on a few translation services
14 # * Configuration for whether to show translation engine
15 # * Optionally sync default translators with karma.rb ranking
20 # base class for implementing a translation service
22 # direction:: supported translation directions, a hash where each key is a source
23 # language name, and each value is Set of target language names. The
24 # methods in the Direction module are convenient for initializing this
27 INFO = 'Some translation service'
29 class UnsupportedDirectionError < ArgumentError
32 class NoTranslationError < RuntimeError
35 attr_reader :directions, :cache
37 def initialize(directions, cache={})
38 @directions = directions
42 # Many translators use Mechanize, which changed namespace around version 1.0
43 # To support both pre-1.0 and post-1.0 namespaces, we use these auxiliary
44 # method. The translator still needs to require 'mechanize' on initialization
47 return Mechanize if defined? Mechanize
51 # whether the translator supports this direction
52 def support?(from, to)
53 from != to && @directions[from].include?(to)
56 # this implements argument checking and caching. subclasses should define the
57 # do_translate method to implement actual translation
58 def translate(text, from, to)
59 raise UnsupportedDirectionError unless support?(from, to)
60 raise ArgumentError, _("Cannot translate empty string") if text.empty?
61 request = [text, from, to]
62 unless @cache.has_key? request
63 translation = do_translate(text, from, to)
64 raise NoTranslationError if translation.empty?
65 @cache[request] = translation
72 # given the set of supported languages, return a hash suitable for the directions
73 # attribute which includes any language to any other language
74 def self.all_to_all(languages)
75 directions = all_to_none(languages)
76 languages.each {|l| directions[l] = languages.to_set}
80 # a hash suitable for the directions attribute which includes any language from/to
81 # the given set of languages (center_languages)
82 def self.all_from_to(languages, center_languages)
83 directions = all_to_none(languages)
84 center_languages.each {|l| directions[l] = languages - [l]}
85 (languages - center_languages).each {|l| directions[l] = center_languages.to_set}
89 # get a hash from a list of pairs
90 def self.pairs(list_of_pairs)
91 languages = list_of_pairs.flatten.to_set
92 directions = all_to_none(languages)
93 list_of_pairs.each do |(from, to)|
94 directions[from] << to
99 # an empty hash with empty sets as default values
100 def self.all_to_none(languages)
102 # always return empty set when the key is non-existent, but put empty set in the
103 # hash only if the key is one of the languages
104 if languages.include? k
114 class GoogleTranslator < Translator
115 INFO = 'Google Translate <http://www.google.com/translate_t>'
116 URL = 'https://translate.google.com/'
119 %w[af sq am ar hy az eu be bn bh bg my ca chr zh zh_CN zh_TW hr
120 cs da dv en eo et tl fi fr gl ka de el gn gu iw hi hu is id iu
121 ga it ja kn kk km ko lv lt mk ms ml mt mr mn ne no or ps fa pl
122 pt_PT pa ro ru sa sr sd si sk sl es sw sv tg ta tl te th bo tr
123 uk ur uz ug vi cy yi auto]
124 def initialize(cache={})
126 super(Translator::Direction.all_to_all(LANGUAGES), cache)
129 def do_translate(text, from, to)
130 agent = Mechanize.new
132 form = page.form_with(:id => 'gt-form')
137 return page.search('#result_box span').first.content
141 class YandexTranslator < Translator
142 INFO = 'Yandex Translator <http://translate.yandex.com/>'
143 LANGUAGES = %w{ar az be bg ca cs da de el en es et fi fr he hr hu hy it ka lt lv mk nl no pl pt ro ru sk sl sq sr sv tr uk}
145 URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=%s&lang=%s-%s&text=%s'
146 KEY = 'trnsl.1.1.20140326T031210Z.1e298c8adb4058ed.d93278fea8d79e0a0ba76b6ab4bfbf6ac43ada72'
147 def initialize(cache)
150 super(Translator::Direction.all_to_all(LANGUAGES), cache)
153 def translate(text, from, to)
154 res = Irc::Utils.bot.httputil.get_response(URL % [KEY, from, to, URI.escape(text)])
155 res = JSON.parse(res.body)
157 if res['code'] != 200
158 raise Translator::NoTranslationError
160 res['text'].join(' ')
166 class TranslatorPlugin < Plugin
167 Config.register Config::IntegerValue.new('translator.timeout',
168 :default => 30, :validate => Proc.new{|v| v > 0},
169 :desc => _("Number of seconds to wait for the translation service before timeout"))
170 Config.register Config::StringValue.new('translator.destination',
172 :desc => _("Default destination language to be used with translate command"))
175 'google_translate' => GoogleTranslator,
176 'yandex' => YandexTranslator,
181 @failed_translators = []
183 TRANSLATORS.each_pair do |name, c|
184 watch_for_fail(name) do
185 @translators[name] = c.new(@registry.sub_registry(name))
186 map "#{name} :from :to *phrase",
187 :action => :cmd_translate, :thread => true
191 Config.register Config::ArrayValue.new('translator.default_list',
192 :default => TRANSLATORS.keys,
193 :validate => Proc.new {|l| l.all? {|t| TRANSLATORS.has_key?(t)}},
194 :desc => _("List of translators to try in order when translator name not specified"),
195 :on_change => Proc.new {|bot, v| update_default})
199 def watch_for_fail(name, &block)
203 debug 'Translator error: '+$!.to_s
205 @failed_translators << { :name => name, :reason => $!.to_s }
207 warning _("Translator %{name} cannot be used: %{reason}") %
208 {:name => name, :reason => $!}
209 map "#{name} [*args]", :action => :failed_translator,
210 :defaults => {:name => name, :reason => $!}
214 def failed_translator(m, params)
215 m.reply _("Translator %{name} cannot be used: %{reason}") %
216 {:name => params[:name], :reason => params[:reason]}
219 def help(plugin, topic=nil)
220 case (topic.intern rescue nil)
222 unless @failed_translators.empty?
223 failed_list = @failed_translators.map { |t| _("%{bold}%{translator}%{bold}: %{reason}") % {
224 :translator => t[:name],
225 :reason => t[:reason],
229 _("Failed translators: %{list}") % { :list => failed_list.join(", ") }
231 _("None of the translators failed")
234 if @translators.has_key?(plugin)
235 translator = @translators[plugin]
236 _('%{translator} <from> <to> <phrase> => Look up phrase using %{info}, supported from -> to languages: %{directions}') % {
237 :translator => plugin,
238 :info => translator.class::INFO,
239 :directions => translator.directions.map do |source, targets|
240 _('%{source} -> %{targets}') %
241 {:source => source, :targets => targets.to_a.join(', ')}
245 help_str = _('Command: <translator> <from> <to> <phrase>, where <translator> is one of: %{translators}. If "translator" is used in place of the translator name, the first translator in translator.default_list which supports the specified direction will be picked automatically. Use "help <translator>" to look up supported from and to languages') %
246 {:translators => @translators.keys.join(', ')}
248 help_str << "\n" + _("%{bold}Note%{bold}: %{failed_amt} translators failed, see %{reverse}%{prefix}help translate failed%{reverse} for details") % {
249 :failed_amt => @failed_translators.size,
252 :prefix => @bot.config['core.address_prefix'].first
261 @languages ||= @translators.map { |t| t.last.directions.keys }.flatten.uniq
265 @default_translators = bot.config['translator.default_list'] & @translators.keys
268 def cmd_translator(m, params)
269 params[:to] = @bot.config['translator.destination'] if params[:to].nil?
270 params[:from] ||= 'auto'
271 translator = @default_translators.find {|t| @translators[t].support?(params[:from], params[:to])}
274 cmd_translate m, params.merge({:translator => translator, :show_provider => false})
276 # When translate command is used without source language, "auto" as source
277 # language is assumed. It means that google translator is used and we let google
278 # figure out what the source language is.
280 # Problem is that the google translator will fail if the system that the bot is
281 # running on does not have the json gem installed.
282 if params[:from] == 'auto'
283 m.reply _("Unable to auto-detect source language due to broken google translator, see %{reverse}%{prefix}help translate failed%{reverse} for details") % {
285 :prefix => @bot.config['core.address_prefix'].first
288 m.reply _('None of the default translators (translator.default_list) supports translating from %{source} to %{target}') % {:source => params[:from], :target => params[:to]}
293 def cmd_translate(m, params)
294 # get the first word of the command
295 tname = params[:translator] || m.message[/\A(\w+)\s/, 1]
296 translator = @translators[tname]
297 from, to, phrase = params[:from], params[:to], params[:phrase].to_s
299 watch_for_fail(tname) do
301 translation = Timeout.timeout(@bot.config['translator.timeout']) do
302 translator.translate(phrase, from, to)
304 m.reply(if params[:show_provider]
305 _('%{translation} (provided by %{translator})') %
306 {:translation => translation, :translator => tname.gsub("_", " ")}
311 rescue Translator::UnsupportedDirectionError
312 m.reply _("%{translator} doesn't support translating from %{source} to %{target}") %
313 {:translator => tname, :source => from, :target => to}
314 rescue Translator::NoTranslationError
315 m.reply _('%{translator} failed to provide a translation') %
316 {:translator => tname}
317 rescue Timeout::Error
318 m.reply _('The translator timed out')
322 m.reply _('No translator called %{name}') % {:name => tname}
326 # URL translation has nothing to do with Translators so let's make it
327 # separate, and Google exclusive for now
328 def cmd_translate_url(m, params)
329 params[:to] = @bot.config['translator.destination'] if params[:to].nil?
330 params[:from] ||= 'auto'
332 translate_url = "http://translate.google.com/translate?sl=%{from}&tl=%{to}&u=%{url}" % {
333 :from => params[:from],
335 :url => CGI.escape(params[:url].to_s)
338 m.reply(translate_url)
342 plugin = TranslatorPlugin.new
343 req = Hash[*%w(from to).map { |e| [e.to_sym, /#{plugin.languages.join("|")}/] }.flatten]
345 plugin.map 'translate [:from] [:to] :url',
346 :action => :cmd_translate_url, :requirements => req.merge(:url => %r{^https?://[^\s]*})
347 plugin.map 'translator [:from] [:to] :url',
348 :action => :cmd_translate_url, :requirements => req.merge(:url => %r{^https?://[^\s]*})
349 plugin.map 'translate [:from] [:to] *phrase',
350 :action => :cmd_translator, :thread => true, :requirements => req
351 plugin.map 'translator [:from] [:to] *phrase',
352 :action => :cmd_translator, :thread => true, :requirements => req