Class: Lit::CloudTranslation::Providers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lit/cloud_translation/providers/base.rb

Overview

Abstract base class for cloud translation providers, providing a skeleton for the provider’s functionality (mainly the #translate method) as well as a configuration management mechanism.

Direct Known Subclasses

Google, Yandex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
# File 'lib/lit/cloud_translation/providers/base.rb', line 10

def initialize(config)
  default_config.each do |key, value|
    config[key] ||= value
  end
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/lit/cloud_translation/providers/base.rb', line 8

def config
  @config
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


51
52
53
# File 'lib/lit/cloud_translation/providers/base.rb', line 51

def configure
  yield config if block_given?
end

.translate(text:, from: nil, to:, **opts) ⇒ Object

Using the provider object’s singleton instance, translates a given text from a given language to a different one.

Parameters:

  • text (String, Array)

    The text (or array of texts) to translate

  • from (Symbol, String) (defaults to: nil)

    The language to translate from. If not given, auto-detection will be attempted.

  • to (Symbol, String)

    The language to translate to.

  • opts (Hash)

    Additional, provider-specific optional parameters.



47
48
49
# File 'lib/lit/cloud_translation/providers/base.rb', line 47

def translate(text:, from: nil, to:, **opts)
  instance.translate(text: text, from: from, to: to, **opts)
end

Instance Method Details

#translate(text:, from: nil, to:, **opts) ⇒ Object

Translates a given text from a given language to a different one.

Parameters:

  • text (String, Array)

    The text (or array of texts) to translate

  • from (Symbol, String) (defaults to: nil)

    The language to translate from. If not given, auto-detection will be attempted.

  • to (Symbol, String)

    The language to translate to.

  • opts (Hash)

    Additional, provider-specific optional parameters.

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/lit/cloud_translation/providers/base.rb', line 23

def translate(text:, from: nil, to:, **opts) # rubocop:disable Lint/UnusedMethodArgument, Metrics/LineLength
  raise NotImplementedError
end