Class: TranslationDiff::Translator

Inherits:
Object
  • Object
show all
Includes:
CallPreparation, Instrumentation
Defined in:
lib/translation_diff/translator.rb

Overview

One translation, coordinated: each step hands its value to the next, and a translation rides home on its own segment.

Defined Under Namespace

Classes: Error

Constant Summary

Constants included from Instrumentation

Instrumentation::SUFFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, from: nil, to: nil, provider: nil, config: nil, assume_supported: false, **options) ⇒ Translator

provider:, config: and assume_supported: are reserved; every other keyword reaches the provider untouched.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/translation_diff/translator.rb', line 12

def initialize(values, from: nil, to: nil, provider: nil, config: nil, assume_supported: false, **options)
  raise ArgumentError, "a translation needs a target language: pass `to:` a language code." if to.nil?

  @values = values
  @from = from
  @to = to
  @options = options
  @config = config || TranslationDiff.config
  @requested_provider = provider
  @assume_supported = assume_supported
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/translation_diff/translator.rb', line 9

def config
  @config
end

Instance Method Details

#call ⇒ Object

Hands back the caller's value untouched unless something in it was actually translated.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/translation_diff/translator.rb', line 25

def call
  document = TranslationDiff::Document.new(TranslationDiff::Leaves.collapse_nils(@values))
  passages = document.strings.map { |string| passage(string) }
  segments = passages.flat_map(&:segments).reject(&:empty?)
  return @values if segments.empty?
  return @values if same_language?(@from)

  provider = resolve_provider
  from = resolve_source_language(provider, segments)
  return @values if same_language?(from)

  translated(document, passages, segments, provider, from)
end