Module: Translatomatic::Provider

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

Overview

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

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

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

Find all configured providers

Parameters:

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

    Provider options

Returns:

  • (Array<#translate>)

    A list of provider instances



51
52
53
54
# File 'lib/translatomatic/provider.rb', line 51

def available(options = {})
  available = types.collect { |klass| create_provider(klass, options) }
  available.compact
end

.find(name) ⇒ Class

Returns The provider class corresponding to the given name.

Returns:

  • (Class)

    The provider class corresponding to the given name



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

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

.get_error(name) ⇒ Object

Get errors for the specified provider

Parameters:

  • name (String)

    Provider name



58
59
60
# File 'lib/translatomatic/provider.rb', line 58

def get_error(name)
  @provider_errors[name]
end

.namesList<String>

Returns A list of all providers.

Returns:

  • (List<String>)

    A list of all providers



44
45
46
# File 'lib/translatomatic/provider.rb', line 44

def names
  types.collect { |i| i.name.demodulize }
end

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

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

Parameters:

  • list (Array<String>)

    Provider names or providers

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

    Provider options

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/translatomatic/provider.rb', line 19

def resolve(list, options = {})
  list = [list].flatten.compact.collect do |provider|
    if provider.respond_to?(:translate)
      provider
    else
      klass = find(provider)
      provider = create_provider(klass, options)
    end
    provider
  end

  # if we didn't resolve to any providers, find all available
  # providers that work with the given options.
  list.empty? ? available(options) : list
end

.typesList<Class>

Returns A list of all provider classes.

Returns:

  • (List<Class>)

    A list of all provider classes



36
37
38
39
40
41
# File 'lib/translatomatic/provider.rb', line 36

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