Class: Translatomatic::Translator::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/translator/base.rb

Overview

This class is abstract.

Base class for interfaces to translation APIs

Direct Known Subclasses

Frengly, Google, Microsoft, MyMemory, Yandex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(options = {})
  @listener = options[:listener]
end

Instance Attribute Details

#listenerObject

Listener for translation events



12
13
14
# File 'lib/translatomatic/translator/base.rb', line 12

def listener
  @listener
end

Instance Method Details

#languagesArray<String>

Returns A list of languages supported by this translator.

Returns:

  • (Array<String>)

    A list of languages supported by this translator.



24
25
26
# File 'lib/translatomatic/translator/base.rb', line 24

def languages
  []
end

#nameString

Returns The name of this translator.

Returns:

  • (String)

    The name of this translator.



19
20
21
# File 'lib/translatomatic/translator/base.rb', line 19

def name
  self.class.name.demodulize
end

#translate(strings, from, to) ⇒ Array<String>

Translate strings from one locale to another

Parameters:

Returns:

  • (Array<String>)

    Translated strings



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

def translate(strings, from, to)
  @updated_listener = false
  strings = [strings] unless strings.kind_of?(Array)
  from = locale(from)
  to = locale(to)
  return strings if from.language == to.language
  translated = perform_translate(strings, from, to)
  update_translated(translated) unless @updated_listener
  translated
end