Skip to content

Instantly share code, notes, and snippets.

@haslo
Last active October 26, 2016 12:26
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 haslo/6835002b66e68c699d72 to your computer and use it in GitHub Desktop.
Save haslo/6835002b66e68c699d72 to your computer and use it in GitHub Desktop.
Export locale files from YAML to CSV, for further processing by clients
#!/usr/bin/env ruby
require 'yaml'
require 'deep_merge'
require 'find'
require 'spreadsheet'
def process_hash(translations, current_key, hash)
hash.each do |new_key, value|
combined_key = [current_key, new_key].delete_if { |k| k == '' }.join(".")
if value.is_a?(Hash)
process_hash(translations, combined_key, value)
else
translations[combined_key] = value
end
end
end
def yaml_to_list(filename)
file_content = File.read(filename)
yaml_content = YAML.load(file_content)
translations = {}
process_hash(translations, '', yaml_content)
translations
end
def directory_to_list(path)
translations = {}
Find.find(path) do |path|
# maybe you want to change this?
translations = translations.deep_merge(yaml_to_list(path)) if path =~ /\/de\.yml\Z/
end
translations
end
def generate_excel(translations)
Spreadsheet.client_encoding = 'UTF-8'
workbook = Spreadsheet::Workbook.new
sheet = workbook.create_worksheet
sheet.name = 'Texte'
title_format = Spreadsheet::Format.new(weight: :bold)
# you might need to change this?
sheet.row(0).push 'Schlüssel', 'de', 'fr', 'it?', 'en?'
sheet.row(0).default_format = title_format
current_row = 1
translations.each do |translation_key, translated_string|
sheet.row(current_row).push translation_key, translated_string
current_row += 1
end
workbook
end
translations = directory_to_list('<input path>')
workbook = generate_excel(translations)
filename = '<output file>.xls'
File.delete(filename) if File.exist?(filename)
workbook.write filename
@haslo
Copy link
Author

haslo commented Oct 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment