Skip to content

Instantly share code, notes, and snippets.

@bajalovic
Created March 13, 2015 14:30
Show Gist options
  • Save bajalovic/b9936be1463f3cdb2e1b to your computer and use it in GitHub Desktop.
Save bajalovic/b9936be1463f3cdb2e1b to your computer and use it in GitHub Desktop.
require 'yaml'
def main
lang_file_1 = ARGV[0]
lang_file_2 = ARGV[1]
lang_1 = YAML::load(File.open lang_file_1)
lang_2 = YAML::load(File.open lang_file_2)
lang_1_keys = all_keys lang_1[lang_1.keys.first]
lang_2_keys = all_keys lang_2[lang_2.keys.first]
a_not_in_b = lang_1_keys - lang_2_keys
b_not_in_a = lang_2_keys - lang_1_keys
if a_not_in_b.empty? && b_not_in_a.empty?
puts "============================"
puts "Both files contain same keys"
puts "============================"
else
puts "Keys exist in the first file, but not in the second one"
puts "========================================================"
puts a_not_in_b
puts "========================================================"
puts "\n\n"
puts "========================================================"
puts "Keys exist in the second file, but not in the first one"
puts "========================================================"
puts b_not_in_a
end
end
def all_keys hash, parent = ""
keys = []
hash.each do |key, value|
if value.is_a? Hash
keys += all_keys(value, [parent, key].select{|i| i unless i == ""}.join("."))
else
keys << [parent, key].select{|i| i unless i == ""}.join(".")
end
end
keys
end
main
@bajalovic
Copy link
Author

Usage:

ruby compare_lang_files.rb /path/to/config/locales/en.yml /path/to/config/locales/de.yml

Output are only the keys (no values). For example with both keys and values check out https://gist.github.com/bajalovic/75fac374ef57a1cbbe25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment