Last active
October 26, 2016 12:26
-
-
Save haslo/6835002b66e68c699d72 to your computer and use it in GitHub Desktop.
Export locale files from YAML to CSV, for further processing by clients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also see https://gist.github.com/haslo/0434bd4c1e94c45bb8cc747e73e4e5a7