Skip to content

Instantly share code, notes, and snippets.

@SauloSilva
Created August 3, 2015 12:52
Show Gist options
  • Save SauloSilva/8e62f527c9ec6bac4f74 to your computer and use it in GitHub Desktop.
Save SauloSilva/8e62f527c9ec6bac4f74 to your computer and use it in GitHub Desktop.
yml compare
require 'yaml'
ymls = Dir['config/locales/**/*.yml'].sort.each_slice(2).to_a
def get_keys(object)
if object.is_a? Hash
(object.keys + get_keys(object.values)).flatten.uniq
elsif object.is_a? Array
object.collect{ |value| get_keys value }
else
[]
end
end
def locales_content(groups)
locales = {}
groups.each do |locale|
content = YAML.load_file(locale)
content = get_keys(content)
key = content.shift
locales[key] = content
end
locales
end
errors = []
ymls.each do |groups|
locales = locales_content(groups)
default_locale = locales.shift
locales.to_a.each do |key, value|
normal = default_locale.last - value
inverse = value - default_locale.last
origin = groups.detect do |group|
normal.any? ? group.include?(key) : group.include?(default_locale.first)
end
errors << if normal.any?
"Is missing the key(s): #{ normal.join(', ') } in #{ origin }"
end
errors << if inverse.any?
"Is missing the key(s): #{ inverse.join(', ') } in #{ origin }"
end
end
end
puts errors.compact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment