Skip to content

Instantly share code, notes, and snippets.

@aalin
Created March 23, 2012 14:15
Show Gist options
  • Save aalin/2171019 to your computer and use it in GitHub Desktop.
Save aalin/2171019 to your computer and use it in GitHub Desktop.
# encoding: utf-8
class LocaleDiffer
RED = 31
GREEN = 32
def initialize
load_locales
end
def print
all_keys = @locales_and_keys.values.flatten.uniq.sort
key_length = all_keys.inject(0) { |value, key| value < key.length ? key.length : value }
all_keys.each do |key|
next if @locales_and_keys.keys.none? { |locale| locale_has_key?(locale, key) }
puts [
key.ljust(key_length.succ),
@locales_and_keys.keys.map { |locale| check_for_key(locale, key) }
].join(" ")
end
end
private
def load_locales
@locales_and_keys = {}
Dir['config/locales/{sv,en}.yml'].each do |file|
locale = File.basename(file, ".*")
yaml = YAML.load_file(file)
@locales_and_keys[locale] = get_keys(yaml[locale])
end
end
def locale_has_key?(locale, key)
@locales_and_keys[locale].include?(key)
end
def check_for_key(locale, key)
color = locale_has_key?(locale, key) ? GREEN : RED
format("\e[%dm%s\e[0m", color, locale)
end
def get_keys(lang, path = [])
lang.map do |k, v|
case v
when Hash
get_keys(v, path + [k])
when String
[path + [k]].join(".")
else
raise "wtf? #{ v }"
end
end.flatten
end
end
LocaleDiffer.new.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment