Module: Translatomatic::Translator

Extended by:
Util
Defined in:
lib/translatomatic/translator.rb,
lib/translatomatic/translator/base.rb,
lib/translatomatic/translator/google.rb,
lib/translatomatic/translator/yandex.rb,
lib/translatomatic/translator/frengly.rb,
lib/translatomatic/translator/microsoft.rb,
lib/translatomatic/translator/my_memory.rb

Defined Under Namespace

Classes: Base, Frengly, Google, Microsoft, MyMemory, Yandex

Class Method Summary collapse

Methods included from Util

locale, log, string

Class Method Details

.available(options = {}) ⇒ Array<#translate>

Find all configured translators

Parameters:

  • options (Hash<String,String>) (defaults to: {})

    Translator options

Returns:

  • (Array<#translate>)

    A list of translator instances



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/translatomatic/translator.rb', line 34

def self.available(options = {})
  available = []
  modules.each do |mod|
    begin
      translator = mod.new(options)
      available << translator
    rescue Exception
      log.debug("translator #{mod.name.demodulize} is unavailable")
    end
  end
  available
end

.find(name) ⇒ Class

Returns The translator class corresponding to the given name.

Returns:

  • (Class)

    The translator class corresponding to the given name



15
16
17
# File 'lib/translatomatic/translator.rb', line 15

def self.find(name)
  name ? self.const_get(name) : nil
end

.listString

Returns A description of all translators and options.

Returns:

  • (String)

    A description of all translators and options



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/translatomatic/translator.rb', line 48

def self.list
  out = "Translators:\n"
  modules.each do |mod|
    out += "\n" + mod.name.demodulize + ":\n"
    opts = mod.options
    opts.each do |opt|
      optname = opt.name.to_s.gsub("_", "-")
      out += "  --%-18s  %18s  %10s  %15s\n" % [optname, opt.description,
        opt.required ? "(required)" : "",
        opt.use_env ? "ENV[#{opt.name.upcase}]" : ""]
    end
  end
  out + "\n"
end

.modulesList<Class>

Returns A list of all translator classes.

Returns:

  • (List<Class>)

    A list of all translator classes



20
21
22
23
24
# File 'lib/translatomatic/translator.rb', line 20

def self.modules
  self.constants.collect { |c| self.const_get(c) }.select do |klass|
    klass.is_a?(Class) && klass != Translatomatic::Translator::Base
  end
end

.namesList<String>

Returns A list of all translators.

Returns:

  • (List<String>)

    A list of all translators



27
28
29
# File 'lib/translatomatic/translator.rb', line 27

def self.names
  modules.collect { |i| i.name.demodulize }
end