Created
April 9, 2015 19:41
-
-
Save lukas/41152daaf7ff01df31cc to your computer and use it in GitHub Desktop.
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 'csv' | |
require 'pp' | |
require 'yaml' | |
# builds lanugage files | |
# go to config/locales | |
# example: ruby convert_i18n_cf_to_yaml.rb f1234.csv ar | |
class Hash | |
def deep_merge(hash) | |
target = dup | |
hash.keys.each do |key| | |
if hash[key].is_a? Hash and self[key].is_a? Hash | |
target[key] = target[key].deep_merge(hash[key]) | |
next | |
end | |
target[key] = hash[key] | |
end | |
target | |
end | |
end | |
if (!ARGV[1]) | |
abort ("Usage ruby convert_i18n_cf_to_yaml.rb [i18n_csv] [language_extention]") | |
end | |
data = CSV.read(ARGV[0], headers:true) | |
text_column_name = data[1].headers.find { |k| k[/the_text/]} | |
if !text_column_name | |
abort "File is missing text header" | |
end | |
if !data[1].headers.find { |k| k[/path/]} | |
abort "File is missing path header" | |
end | |
files = {} | |
data.each do |line| | |
pathblob = line["path"] | |
text = line[text_column_name] | |
if (!pathblob) | |
abort "Missing or blank path column" | |
end | |
filename, path = pathblob.split("+") | |
path.gsub!(/^en/, ARGV[1]) | |
filename.gsub!("en.yml", "#{ARGV[1]}.yml") | |
files[filename] ||= {} | |
files[filename][path] = text | |
end | |
files.each_pair do |file, data| | |
hashdata = data.map do |main_key, main_value| | |
main_key.to_s.split(".").reverse.inject(main_value) do |value, key| | |
{key.to_s => value} | |
end | |
end.inject(&:deep_merge) | |
puts "Writing File #{file}" | |
File.open(file,'w') do |h| | |
h.write hashdata.to_yaml | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment