Skip to content

Instantly share code, notes, and snippets.

@dommmel
Created July 23, 2014 08:31
Show Gist options
  • Save dommmel/7da6ee376866771c7514 to your computer and use it in GitHub Desktop.
Save dommmel/7da6ee376866771c7514 to your computer and use it in GitHub Desktop.
Rake task to compare keys in Rails locale.yml files
desc "TODO"
task :compare_yml, [:locale1, :locale2] => :environment do |t, args|
LOCALE_1 = "config/locales/#{args[:locale1]}.yml"
LOCALE_2 = "config/locales/#{args[:locale2]}.yml"
require 'yaml'
def flatten_keys(hash, prefix="")
keys = []
hash.keys.each do |key|
if hash[key].is_a? Hash
current_prefix = prefix + "#{key}."
keys << flatten_keys(hash[key], current_prefix)
else
keys << "#{prefix}#{key}"
end
end
prefix == "" ? keys.flatten : keys
end
def compare(locale_1, locale_2)
yaml_1 = YAML.load(File.open(File.expand_path(locale_1)))
yaml_2 = YAML.load(File.open(File.expand_path(locale_2)))
keys_1 = flatten_keys(yaml_1[yaml_1.keys.first])
keys_2 = flatten_keys(yaml_2[yaml_2.keys.first])
missing = keys_2 - keys_1
file = locale_1.split('/').last
if missing.any?
puts "Missing from #{file}:"
missing.each { |key| puts " - #{key}" }
else
puts "Nothing missing from #{file}."
end
end
compare(LOCALE_1, LOCALE_2)
puts
compare(LOCALE_2, LOCALE_1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment