class MedelaBstAndroidExcel2xml::Converter

Attributes

destination_path[RW]
languages[RW]

Public Class Methods

new(spreadsheet_path) click to toggle source
# File lib/medela_bst_android_excel2xml/converter.rb, line 7
def initialize(spreadsheet_path)
  @file = Roo::Spreadsheet.open(spreadsheet_path)
  @destination_path ||= Dir.pwd
  @languages ||= []
end

Public Instance Methods

run() click to toggle source
# File lib/medela_bst_android_excel2xml/converter.rb, line 13
def run
  translations = parse_sheet
  translations.select!(@languages) unless @languages.empty?
  translations.each do |language, translation_hash|
    xml = generate_xml(translation_hash)
    save_xml(xml, language)
  end

end

Protected Instance Methods

generate_xml(translations) click to toggle source
# File lib/medela_bst_android_excel2xml/converter.rb, line 58
def generate_xml(translations)
  Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.resources {
      translations.sort.each do |name, value|

        # Normalize attr names
        name_attr = name.downcase
        name_attr = name_attr.upcase if name_attr.include?('_b2c')
        name_attr = 'new_' if name_attr == 'new'

        #escape single quotes
        value.gsub!("'", %q(\\\'))

        #leading 0 replacement for dates
        if name_attr == 'sectionstatsfrequencyperiodtypeweeklyweekof'
          str_replace_index = 0
          value.gsub!(/(%@)/) do
            str_replace_index += 1
            "%#{str_replace_index}$02d"
          end
        end

        # Replace all %@ with numbered java placeholders, eg. %1$s %2$s etc
        str_replace_index = 0
        value.gsub!(/(%@)/) do
          str_replace_index += 1
          "%#{str_replace_index}$s"
        end

        int_replace_index = 0
        value.gsub!(/(%X%)/) do
          int_replace_index += 1
          "%#{int_replace_index}$d"
        end
        value.gsub!(/(%Y%)/, '%2$s')

        value.gsub!(/(%DAY%)/, '%1$s')

        # put html inside cdata
        if value.include?('html')
          xml.string(name: name_attr) { xml.cdata(CGI.unescapeHTML(value)) }
        else
          xml.string(value, name: name_attr)
        end
      end
    }
  end
end
parse_sheet() click to toggle source

Parse the entire sheet into a hash where the key represents the language and the entry is an array of name value hashes

# File lib/medela_bst_android_excel2xml/converter.rb, line 29
def parse_sheet
  translation_sheet = @file.sheet(0)
  all_headers = translation_sheet.row(2)

  row_hashes = []
  # parse header
  translation_sheet.parse(header_search: all_headers, clean: true).each do |row_hash|
    row_hashes << row_hash.delete_if { |key, value| key.to_s.match(/(notes|-chars)/) || value.nil? }
  end

  #remove header row from sheet hashes
  row_hashes.shift

  # cleanup array - delete hashes with empty tech keys
  row_hashes.delete_if { |entry| entry['tech. key'].nil? }

  i18n_hash = {}

  # transform to hash containing name value pairs grouped by language
  row_hashes.each do |hash|
    translation_hash = hash.reject { |k, v| k == 'tech. key' }
    translation_hash.each do |language, text|
      i18n_hash[language] ||= {}
      i18n_hash[language][hash['tech. key']] = text
    end
  end
  i18n_hash
end
save_xml(xml, language) click to toggle source
# File lib/medela_bst_android_excel2xml/converter.rb, line 107
def save_xml(xml, language)
  File.open(File.join(@destination_path, 'strings_' + language + '.xml'), 'w') { |f| f.write(xml.to_xml) }
end