Skip to content

Instantly share code, notes, and snippets.

@luki3k5
Created February 9, 2013 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luki3k5/4745366 to your computer and use it in GitHub Desktop.
Save luki3k5/4745366 to your computer and use it in GitHub Desktop.
simply converting yaml translation files into the CSV for google spreadsheet upload (to be used with google_spreadsheet2yml gem)
require 'csv'
require 'yaml'
yaml_file_en = YAML::load( File.open( 'config/locales/en.yml' ) )
yaml_file_de = YAML::load( File.open( 'config/locales/de.yml' ) )
def flat_hash(hash, k = [])
return {k => hash} unless hash.is_a?(Hash)
hash.inject({}){ |h, v| h.merge! flat_hash(v[-1], k + [v[0]]) }
end
def old_to_new_hash(hash)
returnee = {}
hash.each do |k,v|
k.shift
returnee[k.join('.')] = v
end
returnee
end
old_hash_en = flat_hash(yaml_file_en)
old_hash_de = flat_hash(yaml_file_de)
# merging arrays into string as keys:
new_hash_en = old_to_new_hash(old_hash_en)
new_hash_de = old_to_new_hash(old_hash_de)
all_keys = new_hash_en.keys + new_hash_de.keys
# puts all_keys#new_hash_en.keys
csv_table = []
all_keys.uniq.each do |key|
csv_table << [key, " ", new_hash_en[key], new_hash_de[key]]
end
File.open 'output_all.csv', 'w' do |f|
f.puts( csv_table.map do |row|
CSV.generate_line row
end )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment