Skip to content

Instantly share code, notes, and snippets.

@benhawker
Created June 7, 2017 16:36
Show Gist options
  • Save benhawker/a687e07247cac53868cace03af504ff6 to your computer and use it in GitHub Desktop.
Save benhawker/a687e07247cac53868cace03af504ff6 to your computer and use it in GitHub Desktop.
Combining some xml files.
require 'nokogiri'
require 'pry'
# Layout - There remains an issue with the XML formatting Layout with i need to resolve.
files = %w(BookingPhrases CommonTerms ClientSide Validation)
# files = ["Layout"]
languages = %w(en-US it pt fr es de nl zh-CN)
files.each do |filename|
file = File.open("#{filename}Translated.xml", "w") do |f|
f << ('<?xml version="1.0" encoding="UTF-8" standalone="no"?>')
f << "\n"
file = File.read("#{filename}.xml")
puts "Reading #{filename}.xml"
doc = Nokogiri::XML(file)
element_count = doc.xpath("//DictionaryItem").size
puts element_count
0.upto(element_count-1).each do |i|
# puts "Index: #{i}: #{doc.xpath("//DictionaryItem")[i]}"
f << "<DictionaryItem Key=\"#{doc.xpath("//DictionaryItem")[i].attributes["Key"].value}\">\n"
languages.each do |lang|
file = File.read("#{filename}_#{lang}.txt")
doc = Nokogiri::XML(file)
puts "-----------------------------------------------------------------------------------"
# puts "Index: #{i}: #{doc.xpath("//DictionaryItem")[i]}"
puts "Looking at #{filename} in #{lang} for key #{doc.xpath("//DictionaryItem")[i].attributes["Key"].value}"
f << "\t"
f << doc.xpath("//DictionaryItem")[i].children[1]
f << "\n"
end
f << "</DictionaryItem>"
f << "\n"
end
end
puts "#{filename} completed!"
sleep 1
end;nil
# puts "We have created the following files:"
# files.each_with_index do |filename, i|
# puts " - #{i+1}: #{filename}Translated.xml"
# end;nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment