Class: Cinch::Plugins::Translate

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/translate.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Translate

Returns a new instance of Translate.



19
20
21
22
23
24
25
# File 'lib/cinch/plugins/translate.rb', line 19

def initialize(*args)
  super
  @auto = config[:auto] || false
  @from = config[:from] || nil
  @to   = config[:to]   || 'en'
  ::ToLang.start config[:api_key]
end

Instance Method Details

#auto_from(m, lang) ⇒ Object



48
49
50
51
# File 'lib/cinch/plugins/translate.rb', line 48

def auto_from(m, lang)
  @from = (lang == 'auto' ? nil : lang)
  m.reply "From Language set to #{lang}"
end

#auto_to(m, lang) ⇒ Object



44
45
46
# File 'lib/cinch/plugins/translate.rb', line 44

def auto_to(m, lang)
  m.reply "To Language set to #{@to = lang}"
end

#auto_translate(m) ⇒ Object



39
40
41
42
# File 'lib/cinch/plugins/translate.rb', line 39

def auto_translate(m)
  msg = (@auto = !@auto ? "on" : "off")
  m.reply "Auto Translate is #{msg}"
end

#codemap(m, lang) ⇒ Object



67
68
69
70
# File 'lib/cinch/plugins/translate.rb', line 67

def codemap(m, lang)
  code = ::ToLang::CODEMAP[lang] || 'No code exists for that language.'
  m.reply code
end

#languages(m) ⇒ Object



63
64
65
# File 'lib/cinch/plugins/translate.rb', line 63

def languages(m)
  m.reply ::ToLang::CODEMAP.keys.join(',')
end

#listen(m) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cinch/plugins/translate.rb', line 27

def listen(m)
  return if m.message =~ /^!/ # ignore any commands
  if @auto
    if @from
      translation = m.message.translate(@to, :from => @from) rescue "That's not a valid language."
    else
      translation = m.message.translate(@to) rescue "That's not a valid language."
    end
    m.reply "#{m.user.nick}: #{translation}"
  end
end

#translate(m, lang, message) ⇒ Object



53
54
55
56
# File 'lib/cinch/plugins/translate.rb', line 53

def translate(m, lang, message)
  translation = message.translate(lang) rescue "That's not a valid language."
  m.reply translation
end

#translate_from(m, from, to, message) ⇒ Object



58
59
60
61
# File 'lib/cinch/plugins/translate.rb', line 58

def translate_from(m, from, to, message)
  translation = message.translate(to, :from => from) rescue "That's not a valid language."
  m.reply translation
end