class I18n::Tasks::Translators::BaseTranslator

Constants

INTERPOLATION_KEY_RE
UNTRANSLATABLE_STRING

Public Class Methods

new(i18n_tasks) click to toggle source

@param [I18n::Tasks::BaseTask] i18n_tasks

# File lib/i18n/tasks/translators/base_translator.rb, line 7
def initialize(i18n_tasks)
  @i18n_tasks = i18n_tasks
end

Public Instance Methods

translate_forest(forest, from) click to toggle source

@param [I18n::Tasks::Tree::Siblings] forest to translate to the locales of its root nodes @param [String] from locale @return [I18n::Tasks::Tree::Siblings] translated forest

# File lib/i18n/tasks/translators/base_translator.rb, line 14
def translate_forest(forest, from)
  forest.inject @i18n_tasks.empty_forest do |result, root|
    translated = translate_pairs(root.key_values(root: true), to: root.key, from: from)
    result.merge! Data::Tree::Siblings.from_flat_pairs(translated)
  end
end

Protected Instance Methods

dump_value(value) click to toggle source

Prepare value for translation. @return [String, Array<String, nil>, nil] value for Google Translate or nil for non-string values

# File lib/i18n/tasks/translators/base_translator.rb, line 66
def dump_value(value)
  case value
  when Array
    # dump recursively
    value.map { |v| dump_value v }
  when String
    replace_interpolations value unless value.empty?
  end
end
fetch_translations(list, opts) click to toggle source

@param [Array<[String, Object]>] list of key-value pairs @return [Array<[String, Object]>] translated list

# File lib/i18n/tasks/translators/base_translator.rb, line 43
def fetch_translations(list, opts)
  from_values(list, translate_values(to_values(list), **options_for_translate_values(**opts))).tap do |result|
    fail CommandError, no_results_error_message if result.blank?
  end
end
from_values(list, translated_values) click to toggle source

@param [Array<[String, Object]>] list @param [Array<String>] translated_values @return [Array<[String, Object]>] translated key-value pairs

# File lib/i18n/tasks/translators/base_translator.rb, line 58
def from_values(list, translated_values)
  keys = list.map(&:first)
  untranslated_values = list.map(&:last)
  keys.zip parse_value(untranslated_values, translated_values.to_enum)
end
no_results_error_message() click to toggle source

@return [String] @abstract

# File lib/i18n/tasks/translators/base_translator.rb, line 153
def no_results_error_message; end
options_for_html() click to toggle source

@return [Hash] @abstract

# File lib/i18n/tasks/translators/base_translator.rb, line 145
def options_for_html; end
options_for_plain() click to toggle source

@return [Hash] @abstract

# File lib/i18n/tasks/translators/base_translator.rb, line 149
def options_for_plain; end
options_for_translate_values(options) click to toggle source

@param [Hash] options @return [Hash] @abstract

# File lib/i18n/tasks/translators/base_translator.rb, line 141
def options_for_translate_values(options); end
parse_value(untranslated, each_translated) click to toggle source

Parse translated value from the each_translated enumerator @param [Object] untranslated @param [Enumerator] each_translated @return [Object] final translated value

# File lib/i18n/tasks/translators/base_translator.rb, line 80
def parse_value(untranslated, each_translated)
  case untranslated
  when Array
    # implode array
    untranslated.map { |from| parse_value(from, each_translated) }
  when String
    if untranslated.empty?
      untranslated
    else
      restore_interpolations untranslated, each_translated.next
    end
  else
    untranslated
  end
end
raise_interpolation_error(untranslated, translated, e) click to toggle source
# File lib/i18n/tasks/translators/base_translator.rb, line 123
      def raise_interpolation_error(untranslated, translated, e)
        fail CommandError.new(e, <<~TEXT.strip)
          Error when restoring interpolations:
            original: "#{untranslated}"
            response: "#{translated}"
            error: #{e.message} (#{e.class.name})
        TEXT
      end
replace_interpolations(value) click to toggle source

@param [String] value @return [String] 'hello, %{name}' => 'hello, <round-trippable string>'

# File lib/i18n/tasks/translators/base_translator.rb, line 101
def replace_interpolations(value)
  i = -1
  value.gsub INTERPOLATION_KEY_RE do
    i += 1
    "#{UNTRANSLATABLE_STRING}#{i}"
  end
end
restore_interpolations(untranslated, translated) click to toggle source

@param [String] untranslated @param [String] translated @return [String] 'hello, <round-trippable string>' => 'hello, %{name}'

# File lib/i18n/tasks/translators/base_translator.rb, line 112
def restore_interpolations(untranslated, translated)
  return translated if untranslated !~ INTERPOLATION_KEY_RE

  values = untranslated.scan(INTERPOLATION_KEY_RE)
  translated.gsub(/#{Regexp.escape(UNTRANSLATABLE_STRING)}\d+/i) do |m|
    values[m[UNTRANSLATABLE_STRING.length..-1].to_i]
  end
rescue StandardError => e
  raise_interpolation_error(untranslated, translated, e)
end
to_values(list) click to toggle source

@param [Array<[String, Object]>] list of key-value pairs @return [Array<String>] values for translation extracted from list

# File lib/i18n/tasks/translators/base_translator.rb, line 51
def to_values(list)
  list.map { |l| dump_value l[1] }.flatten.compact
end
translate_pairs(list, opts) click to toggle source

@param [Array<[String, Object]>] list of key-value pairs @return [Array<[String, Object]>] translated list

# File lib/i18n/tasks/translators/base_translator.rb, line 25
def translate_pairs(list, opts)
  return [] if list.empty?

  opts = opts.dup
  key_pos = list.each_with_index.inject({}) { |idx, ((k, _v), i)| idx.update(k => i) }
  # copy reference keys as is, instead of translating
  reference_key_vals = list.select { |_k, v| v.is_a? Symbol } || []
  list -= reference_key_vals
  result = list.group_by { |k_v| @i18n_tasks.html_key? k_v[0], opts[:from] }.map do |is_html, list_slice|
    fetch_translations list_slice, opts.merge(is_html ? options_for_html : options_for_plain)
  end.reduce(:+) || []
  result.concat(reference_key_vals)
  result.sort! { |a, b| key_pos[a[0]] <=> key_pos[b[0]] }
  result
end
translate_values(list, **options) click to toggle source

@param [Array<String>] list @param [Hash] options @return [Array<String>] @abstract

# File lib/i18n/tasks/translators/base_translator.rb, line 136
def translate_values(list, **options); end