Class: I18n::Tasks::Translators::BaseTranslator
- Inherits:
-
Object
- Object
- I18n::Tasks::Translators::BaseTranslator
- Includes:
- Logging
- Defined in:
- lib/i18n/tasks/translators/base_translator.rb
Direct Known Subclasses
DeeplTranslator, GoogleTranslator, OpenAiTranslator, WatsonxTranslator, YandexTranslator
Constant Summary
Constants included from Logging
Logging::MUTEX, Logging::PROGRAM_NAME
Instance Method Summary collapse
-
#initialize(i18n_tasks) ⇒ BaseTranslator
constructor
A new instance of BaseTranslator.
-
#translate_forest(forest, from) ⇒ I18n::Tasks::Tree::Siblings
Translated forest.
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.
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.
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.}" # 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.} - 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 |