Last active
September 11, 2020 12:48
-
-
Save oriolgual/c383b006f99e9be5ee87c9df6a675444 to your computer and use it in GitHub Desktop.
Converts all English YAML files in a Decidim folder to CSV or XLS
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
require "yaml" | |
require "csv" | |
def reduce_hash(value, parent = nil) | |
value.map do |key, value| | |
i18n_scope = [parent, key].compact.join(".") | |
if value.is_a?(Hash) | |
reduce_hash(value, i18n_scope) | |
else | |
[i18n_scope, value] | |
end | |
end | |
end | |
decidim_folders = %w(decidim-accountability decidim-admin decidim-api decidim-assemblies decidim-blogs decidim-budgets decidim-comments decidim-conferences decidim-consultations decidim-core decidim-debates decidim-dev decidim-elections decidim-forms decidim-generators decidim-initiatives decidim-meetings decidim-pages decidim-participatory_processes decidim-proposals decidim-sortitions decidim-surveys decidim-system decidim-verifications) | |
decidim_path = "/Users/oriol/code/decidim/" | |
base_write_path = "/Users/oriol/" | |
files = decidim_folders.map do |folder| | |
path = decidim_path + folder + "/config/locales/en.yml" | |
write_path = base_write_path + folder + ".csv" | |
unless File.exist?(path) | |
puts "Skipping #{path}" | |
next | |
end | |
puts "Processing #{path}" | |
content = YAML.load(File.read(path)) | |
rows = reduce_hash(content["en"]).flatten.each_slice(2).map {|k,v| [k,v]} | |
CSV.open(write_path, "wb") do |csv| | |
csv << ["Key", "Text"] | |
rows.each do |row| | |
csv << row | |
end | |
end | |
write_path | |
end.compact | |
system("tar -czvf decidim-english-texts.tar.gz #{files.join(' ')} -C #{base_write_path}") | |
system("rm #{files.join(' ')}") | |
puts "Done! All locales are at decidim-texts.tar.gz" |
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
require "yaml" | |
require "spreadsheet" | |
require "byebug" | |
def reduce_hash(value, parent = nil) | |
value.map do |key, value| | |
i18n_scope = [parent, key].compact.join(".") | |
if value.is_a?(Hash) | |
reduce_hash(value, i18n_scope) | |
else | |
[i18n_scope, value] | |
end | |
end | |
end | |
decidim_folders = %w(decidim-accountability decidim-admin decidim-api decidim-assemblies decidim-blogs decidim-budgets decidim-comments decidim-conferences decidim-consultations decidim-core decidim-debates decidim-dev decidim-elections decidim-forms decidim-generators decidim-initiatives decidim-meetings decidim-pages decidim-participatory_processes decidim-proposals decidim-sortitions decidim-surveys decidim-system decidim-verifications) | |
decidim_path = "/Users/oriol/code/decidim/" | |
base_write_path = "/Users/oriol/" | |
Spreadsheet.client_encoding = 'UTF-8' | |
files = decidim_folders.map do |folder| | |
path = decidim_path + folder + "/config/locales/en.yml" | |
file_path = folder + ".xls" | |
write_path = base_write_path + file_path | |
unless File.exist?(path) | |
puts "Skipping #{path}" | |
next | |
end | |
puts "Processing #{path}" | |
content = YAML.load(File.read(path)) | |
texts = reduce_hash(content["en"]).flatten.each_slice(2).map {|k,v| [k,v]} | |
book = Spreadsheet::Workbook.new | |
sheet = book.create_worksheet(name: folder) | |
sheet.insert_row(0, ["Key", "EN"]) | |
texts.each_with_index do |(key, value), index| | |
sheet.insert_row(index + 1, [key, value]) | |
end | |
book.write(write_path) | |
file_path | |
end | |
system("tar -czvf decidim-english-texts.tar.gz #{files.join(' ')} -C #{base_write_path}") | |
system("rm #{files.join(' ')}") | |
puts "Done! All locales are at decidim-english-texts.tar.gz" |
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
require "yaml" | |
require "axlsx" | |
require "byebug" | |
def reduce_hash(value, parent = nil) | |
value.map do |key, value| | |
i18n_scope = [parent, key].compact.join(".") | |
if value.is_a?(Hash) | |
reduce_hash(value, i18n_scope) | |
else | |
[i18n_scope, value] | |
end | |
end | |
end | |
decidim_folders = %w(decidim-accountability decidim-admin decidim-api decidim-assemblies decidim-blogs decidim-budgets decidim-comments decidim-conferences decidim-consultations decidim-core decidim-debates decidim-dev decidim-elections decidim-forms decidim-generators decidim-initiatives decidim-meetings decidim-pages decidim-participatory_processes decidim-proposals decidim-sortitions decidim-surveys decidim-system decidim-verifications) | |
decidim_path = "/Users/oriol/code/decidim/" | |
package = Axlsx::Package.new | |
book = package.workbook | |
decidim_folders.each do |folder| | |
path = decidim_path + folder + "/config/locales/en.yml" | |
unless File.exist?(path) | |
puts "Skipping #{path}" | |
next | |
end | |
puts "Processing #{path}" | |
content = YAML.load(File.read(path)) | |
texts = reduce_hash(content["en"]).flatten.each_slice(2).map {|k,v| [k,v]} | |
book.add_worksheet(name: folder) do |sheet| | |
sheet.add_row(["Key", "EN"]) | |
texts.each do |key, value| | |
sheet.add_row([key, value]) | |
end | |
end | |
end | |
package.serialize("/Users/oriol/decidim-texts.xlsx") | |
puts "Done! All locales are at decidim-texts.xlsx" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment