Class: Translatomatic::Provider::Base Abstract

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

Overview

This class is abstract.

Base class for interfaces to translation APIs

Direct Known Subclasses

Frengly, Google, GoogleWeb, Microsoft, MyMemory, Yandex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



22
23
24
# File 'lib/translatomatic/provider/base.rb', line 22

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

Instance Attribute Details

#listenerObject

Listener for translation events



9
10
11
# File 'lib/translatomatic/provider/base.rb', line 9

def listener
  @listener
end

Class Method Details

.supports_alternative_translations?boolean

Returns True if a string can have alternative translations.

Returns:

  • True if a string can have alternative translations



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

def self.supports_alternative_translations?
  false
end

.supports_no_translate_html?boolean

Returns true if this provider supports html5 <span translate=“no”></span> tags.

Returns:

  • true if this provider supports html5 <span translate=“no”></span> tags.



18
19
20
# File 'lib/translatomatic/provider/base.rb', line 18

def self.supports_no_translate_html?
  false
end

Instance Method Details

#languagesArray<String>

Returns A list of languages supported by this provider.

Returns:

  • A list of languages supported by this provider.



38
39
40
# File 'lib/translatomatic/provider/base.rb', line 38

def languages
  []
end

#nameString

Returns The name of this provider.

Returns:

  • The name of this provider.



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

def name
  self.class.name.demodulize
end

#to_sString

Returns The name of this provider.

Returns:

  • The name of this provider



32
33
34
# File 'lib/translatomatic/provider/base.rb', line 32

def to_s
  name
end

#translate(strings, from, to) ⇒ Array<Translatomatic::Translation::Result>

Translate strings from one locale to another

Parameters:

  • A list of text/strings to translate.

  • The locale of the given strings.

  • The locale to translate to.

Returns:

  • Translations



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

def translate(strings, from, to)
  @updated_listener = false
  @translations = []
  @from = from
  @to = to
  strings = [strings] unless strings.is_a?(Array)
  from = build_locale(from)
  to = build_locale(to)
  if from.language == to.language
    strings.each { |i| add_translations(i, i) }
  else
    perform_translate(strings, from, to)
  end
  @translations
end