Class: I18n::Tasks::Translators::BaseTranslator

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/i18n/tasks/translators/base_translator.rb

Constant Summary

Constants included from Logging

Logging::MUTEX, Logging::PROGRAM_NAME

Instance Method Summary collapse

Methods included from Logging

log_error, log_stderr, log_verbose, log_warn, program_name, warn_deprecated

Constructor Details

#initialize(i18n_tasks) ⇒ BaseTranslator

Returns a new instance of BaseTranslator.

Parameters:



11
12
13
# File 'lib/i18n/tasks/translators/base_translator.rb', line 11

def initialize(i18n_tasks)
  @i18n_tasks = i18n_tasks
end

Instance Method Details

#translate_forest(forest, from) ⇒ I18n::Tasks::Tree::Siblings

Returns translated forest.

Parameters:

  • forest (I18n::Tasks::Tree::Siblings)

    to translate to the locales of its root nodes

  • from (String)

    locale

Returns:

  • (I18n::Tasks::Tree::Siblings)

    translated forest



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/i18n/tasks/translators/base_translator.rb', line 18

def translate_forest(forest, from)
  forest.inject @i18n_tasks.empty_forest do |result, root|
    pairs = root.key_values(root: true)

    @progress_bar = ProgressBar.create(total: pairs.flatten.size, format: "%a <%B> %e %c/%C (%p%%)")

    begin
      translated = translate_pairs(pairs, to: root.key, from: from)
    rescue => e
      warn "Translation for locale #{root.key} failed: #{e.message}"
      # If translate_pairs raised, try to salvage any partial translations
      # by attempting to translate each slice individually and collecting successes.
      translated = []
      pairs.group_by { |k_v| @i18n_tasks.html_key? k_v[0], from }.each do |_is_html, list_slice|
        translated.concat(fetch_translations(list_slice, to: root.key, from: from))
      rescue => e2
        warn "Partial translation failed for locale #{root.key}: #{e2.message} - leaving keys untranslated"
        # leave the original list_slice untranslated
        translated.concat(list_slice)
      end
    end

    result.merge! Data::Tree::Siblings.from_flat_pairs(translated)
  end
end