Created
March 8, 2016 15:50
-
-
Save karuppasamy/fead6ae6c4ef71a1f3fa to your computer and use it in GitHub Desktop.
Sort I18n yaml files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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