class Tolq::Parsers::Xliff::Parser

Public Class Methods

new(quality:, source_language_code: nil, target_language_code: nil) click to toggle source

Creates a new Xliff parser

@param quality [Symbol] the Tolq quality to order in, one of `:basic`, `:standard`, `:professional` or `:expert` @return [Tolq::Parsers::Xliff::Parser] a parser ready to parse an xliff text

# File lib/xliff/parser.rb, line 22
def initialize(quality:, source_language_code: nil, target_language_code: nil)
  @quality = quality
  @source_language_code = source_language_code
  @target_language_code = target_language_code
end
to_hash(xliff_text, **parse_opts) click to toggle source

Helper method to convert a xliff into an annotated hash. Format is different from Tolq api

@param xliff_text [String] String of file @param parse_opts [Hash] Noop @return [Hash] Hash representation of xliff

# File lib/xliff/parser.rb, line 11
def self.to_hash(xliff_text, **parse_opts)
  xliff = XLIFFer::XLIFF.new(xliff_text)
  validate_xliff!(xliff)
  extract_request_data_from_files(xliff.files, text_node: false)
end

Private Class Methods

extract_request_data_from_files(xliff_files, text_node: true) click to toggle source
# File lib/xliff/parser.rb, line 47
def self.extract_request_data_from_files(xliff_files, text_node: true)
  acc = {}
  xliff_files.each.with_index do |file, file_idx|
    file.strings.each do |string|
      unless string.source && string.source.strip.length > 0
        next acc
      end
      id = "#{file_idx}-#{string.id}"
      if text_node
        acc[id] = {"text" => string.source }
      else
        acc[id] = string.source
      end
      if string.note.length > 0 && text_node # xliffs so far use this for bogus anyway
        acc[id].merge!("translator_message" => string.note)
      end
    end
  end
  acc
end
validate_xliff!(xliff) click to toggle source
# File lib/xliff/parser.rb, line 72
def self.validate_xliff!(xliff)
  if xliff.files.map(&:source_language).compact.uniq.length != 1
    raise ParseError.new("More than one source language, cannot parse file")
  end
  if xliff.files.any? { |file| file.strings.map(&:id).any?(&:nil?) }
    raise ParseError.new("Missing mandatory ID for one or more 'trans-unit'")
  end
end

Public Instance Methods

parse(xliff_text) click to toggle source

Parses an Xliff text

@param xliff_text [String] the xlif format text to parse @return [Hash] A hash suitable to be converted to json for the Tolq Api

# File lib/xliff/parser.rb, line 32
def parse(xliff_text)
  xliff = XLIFFer::XLIFF.new(xliff_text)
  self.class.validate_xliff!(xliff)
  request_data = self.class.extract_request_data_from_files(xliff.files)

  {
    "request" => request_data,
    "source_language_code" => @source_language_code || strip_region_language(xliff.files.first.source_language),
    "target_language_code" => @target_language_code || strip_region_language(xliff.files.first.target_language),
    "quality" => @quality.to_s
  }
end

Private Instance Methods

strip_region_language(language) click to toggle source
# File lib/xliff/parser.rb, line 68
def strip_region_language(language)
  language[0..1]
end