class Lit::CloudTranslation::Providers::Base

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.

Attributes

config[R]

Private Class Methods

config() click to toggle source
# File lib/lit/cloud_translation/providers/base.rb, line 61
def config
  @config ||= OpenStruct.new
end
configure() { |config| ... } click to toggle source
# File lib/lit/cloud_translation/providers/base.rb, line 51
def configure
  yield config if block_given?
end
instance() click to toggle source
# File lib/lit/cloud_translation/providers/base.rb, line 57
def instance
  @instance ||= new(config)
end
new(config) click to toggle source
# 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
translate(text:, from: nil, to:, **opts) click to toggle source

Using the provider object's singleton instance, translates a given text from a given language to a different one. @param [String, Array] text The text (or array of texts) to translate @param [Symbol, String] from The language to translate from. If not given,

auto-detection will be attempted.

@param [Symbol, String] to The language to translate to. @param [Hash] opts Additional, provider-specific optional parameters.

# 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

Public Instance Methods

translate(text:, from: nil, to:, **opts) click to toggle source

Translates a given text from a given language to a different one. @param [String, Array] text The text (or array of texts) to translate @param [Symbol, String] from The language to translate from. If not given,

auto-detection will be attempted.

@param [Symbol, String] to The language to translate to. @param [Hash] opts Additional, provider-specific optional parameters.

# 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

Private Instance Methods

default_config() click to toggle source

Loads specific information from environment variables or other sources as the default configuartion for the translation provider.

This can be overridden using `Lit::CloudTranslation.configure`.

# File lib/lit/cloud_translation/providers/base.rb, line 33
def default_config
  {}
end