Skip to content

Instantly share code, notes, and snippets.

@karuppasamy
Created March 8, 2016 15:50
Show Gist options
  • Save karuppasamy/fead6ae6c4ef71a1f3fa to your computer and use it in GitHub Desktop.
Save karuppasamy/fead6ae6c4ef71a1f3fa to your computer and use it in GitHub Desktop.
Sort I18n yaml files
# Recursive sorting algorithm for Hash
class Hash
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
locales = Dir.glob("#{Rails.root}/config/locales/**/*.yml")
locales.each do |locale_path|
# YAML to Hash conversion
hash = HashWithIndifferentAccess.new(YAML.load(File.read(locale_path)))
File.open(locale_path, 'w') { |f| f << hash.sort_by_key(true).to_yaml}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment