Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. def deeply_sort_hash(object)
  2. return object.sort if object.is_a?(Array)
  3. return object unless object.is_a?(Hash)
  4.  
  5. hash = RUBY_VERSION >= '1.9' ? Hash.new : ActiveSupport::OrderedHash.new
  6. object.each { |k, v| hash[k] = deeply_sort_hash(v) }
  7. sorted = hash.sort_by { |(k, v)| [v.is_a?(String) ? 0 : 1, k.to_s.downcase] }
  8. hash.class[sorted]
  9. end
  10.  
  11.  
  12. ['de', 'en'].each do |locale|
  13. files = Dir[Rails.root.join('config', 'locales', "*#{locale}.yml").to_s].sort; nil
  14.  
  15. translations = {}; nil
  16.  
  17. files.each { |file| translations.deep_merge!(YAML.load_file(file)) }; nil
  18.  
  19. output = Rails.root.join('config', 'locales', "#{locale}.yml").to_s
  20.  
  21. sorted = deeply_sort_hash(DeepHashTransformer.new(translations).stringify); nil
  22.  
  23. File.open(output, 'w') { |file| file.write(sorted.to_yaml) }
  24. end
Add Comment
Please, Sign In to add comment