Class: TranslationDiff::Previewer

Inherits:
Object
  • Object
show all
Includes:
CallPreparation
Defined in:
lib/translation_diff/previewer.rb

Overview

Answers what #translate would send and find cached, using the same segmenter, cache key and provider resolution translate uses -- without calling the provider or writing anything. Detection is a paid request this method never makes, so a nil from: for a provider that must detect the language is refused, not guessed at.

Defined Under Namespace

Classes: Error

Constant Summary collapse

EMPTY =
TranslationDiff::Preview.new(sendable_sentences: 0, cached_sentences: 0, sendable_characters: 0,
characters: 0).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

provider:, config: and assume_supported: are reserved, exactly as they are for Translator#initialize.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/translation_diff/previewer.rb', line 16

def initialize(values, from: nil, to: nil, provider: nil, config: nil, assume_supported: false, **options)
  raise ArgumentError, "a preview 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.



13
14
15
# File 'lib/translation_diff/previewer.rb', line 13

def config
  @config
end

Instance Method Details

#call ⇒ Object



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

def call
  segments = document_segments
  return EMPTY if segments.empty? || same_language?(@from)

  provider = resolve_provider
  from = resolve_source_language(provider)
  return EMPTY if same_language?(from)

  preview_for(provider, from, segments)
end