Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Concat all i18n trads into one big file
require "yaml"
class ::Hash
# From activeressource
def deep_merge(other_hash)
self.merge(other_hash) do |key, oldval, newval|
oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
newval = newval.to_hash if newval.respond_to?(:to_hash)
oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval
end
end
# From http://dan.doezema.com/2012/04/recursively-sort-ruby-hash-by-key/
def sort_by_key(recursive = false, &block)
self.keys.sort(&block).reduce({}) do |seed, key|
seed[key] = self[key]
if recursive && seed[key].is_a?(Hash)
seed[key] = seed[key].sort_by_key(true, &block)
end
seed
end
end
end
ignored_folders = /\/admin\//
files = Dir.glob("config/locales/fr/**/*.yml")
files.delete_if{ |f| f.match(ignored_folders) }
all = {}
files.each do |file|
puts file
r = YAML.load_file(file)
all = all.deep_merge(r)
end
all = all.sort_by_key(true)
if File.file?('tmp/merged.yml')
File.delete('tmp/merged.yml')
end
File.open('tmp/merged.yml', 'w') {|f| f.write all.to_yaml }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment