class Tolq::Parsers::Xliff::Unparser

Unparses a tolq response to xliff suitable As opposed to other unparsers, requires the original, AND the target language code Assumes single file per xliff

Public Instance Methods

from_hash(translations, original:, target_language_code:) click to toggle source

Unparses a translations hash of key => translation

@param translations [Hash] A hash of translations with key => translation @return [String] A xliff formatted string

# File lib/xliff/unparser.rb, line 20
def from_hash(translations, original:, target_language_code:)
  translate_xliff(translations, original, target_language_code).to_s
end
from_tolq_response(tolq_response, original:, target_language_code:) click to toggle source

Unparses the tolq response

@param tolq_response [Hash] A parsed response from our api @return [String] A xliff formatted string

# File lib/xliff/unparser.rb, line 11
def from_tolq_response(tolq_response, original:, target_language_code:)
  translations = tolq_response['translations'].values.first
  translate_xliff(translations, original, target_language_code).to_s
end

Private Instance Methods

translate_xliff(translations, xliff_text, target_language_code) click to toggle source
# File lib/xliff/unparser.rb, line 26
def translate_xliff(translations, xliff_text, target_language_code)
  xliff = XLIFFer::XLIFF.new(xliff_text)
  xliff.files.each.with_index do |file, file_idx|
    file.target_language = target_language_code
    file.strings.each do |string|
      if translation = translations["#{file_idx}-#{string.id}"]
        string.target = translation
      end
    end
  end
  xliff
end