Class: Traductor::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/traductor/translator.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_path:, target_locales:, output_dir: nil, incremental: true) ⇒ Translator

Returns a new instance of Translator.



8
9
10
11
12
13
14
15
# File 'lib/traductor/translator.rb', line 8

def initialize(source_path:, target_locales:, output_dir: nil, incremental: true)
  @source_file = LocaleFile.new(source_path)
  @target_locales = Array(target_locales).map(&:to_s)
  @output_dir = output_dir || File.dirname(source_path)
  @incremental = incremental
  @config = Traductor.configuration
  @glossary = Glossary.new(@config.glossary_path)
end

Instance Method Details

#call ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/traductor/translator.rb', line 17

def call
  source_flat = KeyFlattener.flatten(@source_file.content)
  results = {}
  errors = []

  @target_locales.each do |target_locale|
    path = translate_locale(source_flat, target_locale)
    results[target_locale] = path
  rescue StandardError => e
    errors << { locale: target_locale, error: e.message }
  end

  Result.new(results: results, errors: errors)
end

#dry_run ⇒ Object

Returns keys that would be translated without calling the LLM



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/traductor/translator.rb', line 33

def dry_run
  source_flat = KeyFlattener.flatten(@source_file.content)
  summary = {}

  @target_locales.each do |target_locale|
    keys = if @incremental
             determine_keys_to_translate(source_flat, target_locale)
           else
             source_flat
           end
    summary[target_locale] = {
      keys_to_translate: keys.size,
      keys: keys.keys
    }
  end

  summary
end