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

Overview

Provides methods to access and create instances of interfaces to translation APIs.

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

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

Find all configured translators



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/translatomatic/translator.rb', line 63

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

.find(name) ⇒ Class



17
18
19
# File 'lib/translatomatic/translator.rb', line 17

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

.listString



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/translatomatic/translator.rb', line 77

def self.list
  out = t("translator.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 ? t("translator.required_option") : "",
        opt.use_env ? "ENV[#{opt.name.upcase}]" : ""]
    end
  end
  out += "\n"
  out += t("translator.configured") + "\n"
  configured = available
  configured.each do |translator|
    out += "  " + translator.name + "\n"
  end
  out += t("translator.no_translators") if configured.empty?
  out + "\n"
end

.modulesList<Class>



49
50
51
52
53
# File 'lib/translatomatic/translator.rb', line 49

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>



56
57
58
# File 'lib/translatomatic/translator.rb', line 56

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

.resolve(list, options = {}) ⇒ Array<Translatomatic::Translator::Base>

Resolve the given list of translator names to a list of translators. If the list is empty, return all translators that are configured.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/translatomatic/translator.rb', line 26

def self.resolve(list, options = {})
  list = [list] unless list.kind_of?(Array)
  list = list.compact.collect do |translator|
    if translator.respond_to?(:translate)
      translator
    else
      klass = Translatomatic::Translator.find(translator)
      translator = klass.new(options)
    end
    translator
  end

  if list.empty?
    # find all available translators that work with the given options
    list = Translatomatic::Translator.available(options)
    if list.empty?
      raise t("cli.no_translators")
    end
  end
  list
end