module Translatomatic::Translation::Munging

Translation result mungification code

Constants

NoTranslateTag

@private

REGEX1
REGEX2

Private Instance Methods

find_notranslate_spacing(text) click to toggle source

scan text for no translate tags, record leading and trailing space.

# File lib/translatomatic/translation/munging.rb, line 89
def find_notranslate_spacing(text)
  text.scan(REGEX2).collect { |i| [i[0], i[2]] }
end
munge_translation_results(translations) click to toggle source
# File lib/translatomatic/translation/munging.rb, line 10
def munge_translation_results(translations)
  if use_notranslate?
    translations.collect { |tr| remove_notranslate(tr) }
  else
    translations.select { |tr| restore_preserved_text(tr) }
  end
end
notranslate_replacement(string, fix_spacing, num) click to toggle source
# File lib/translatomatic/translation/munging.rb, line 104
def notranslate_replacement(string, fix_spacing, num)
  spacing = fix_spacing[num - 1]
  leading = spacing[0]
  trailing = spacing[1]
  leading + string + trailing
end
remove_notranslate(tr) click to toggle source

Update the translation to remove notranslate directives. Fixes spacing to match the original. @param tr [Result] Translation result @return [Result] Updated translation result

# File lib/translatomatic/translation/munging.rb, line 67
def remove_notranslate(tr)
  return tr unless tr.original.preserve_regex
  original_spacing = find_notranslate_spacing(tr.original)
  result_spacing = find_notranslate_spacing(tr.result)
  if original_spacing.length != result_spacing.length
    # the number of notranslate directives in the result doesn't
    # match the number in the original. this could mean an invalid
    # translation?
    log.debug("possible invalid translation: #{tr.description}")
    original_spacing = nil # don't attempt to fix spacing.
  end
  original = remove_notranslate_text(tr.original)
  result = remove_notranslate_text(tr.result, original_spacing)
  Result.new(
    original, result, tr.provider, from_database: tr.from_database
  )
end
remove_notranslate_text(text, fix_spacing = nil) click to toggle source
# File lib/translatomatic/translation/munging.rb, line 93
def remove_notranslate_text(text, fix_spacing = nil)
  if fix_spacing
    num = 0 # match number
    text.gsub(REGEX2) do |i|
      notranslate_replacement(i[2], fix_spacing, num += 1)
    end
  else
    text.gsub(REGEX1) { |i| i[1] }
  end
end
restore_preserved_text(tr) click to toggle source

Restore parts of text that should be preserved in the translation. Used for providers that don't support translate=“no” html5 attribute. This works when the translator preserves parts of the string that match the preserve_regex, e.g. for variables like '%{name}' if the translation matches /%{.*}/ then the original text can be restored. @return [Boolean] True if no errors were encountered

# File lib/translatomatic/translation/munging.rb, line 28
def restore_preserved_text(tr)
  preserve_regex = tr.original.preserve_regex
  return true unless preserve_regex

  # find parts to preserve in the original string
  list1 = tr.original.substrings(preserve_regex)
  # find corresponding parts in the translated string
  list2 = tr.result.substrings(preserve_regex)

  return false unless list1.length == list2.length

  # we can restore text. sort by largest offset first.
  conversions = list1.zip(list2).collect.sort_by { |i| -i[0].offset }
  conversions.each do |v1, v2|
    tr.result[v2.offset, v2.length] = v1.value
  end
  true
end
use_notranslate?() click to toggle source
# File lib/translatomatic/translation/munging.rb, line 18
def use_notranslate?
  @provider.class.supports_no_translate_html?
end
wrap_notranslate(texts) click to toggle source

@param texts [Array<Text>] Texts to modify @return [Array<Text>] Texts with sections to preserve wrapped in a

notranslate directive.
# File lib/translatomatic/translation/munging.rb, line 50
def wrap_notranslate(texts)
  return texts unless use_notranslate? && texts.any?(&:preserve_regex)
  texts.collect do |text|
    if text.preserve_regex
      text.gsub(text.preserve_regex) do |i|
        '<span translate="no">' + i[0] + '</span>'
      end
    else
      text
    end
  end
end