Created
February 7, 2018 08:26
-
-
Save spickermann/5b42f324638a7f38e28655e5573e2759 to your computer and use it in GitHub Desktop.
Sort locale yml files (quick'n'dirty)
This file contains hidden or 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
def deeply_sort_hash(object) | |
return object.sort if object.is_a?(Array) | |
return object unless object.is_a?(Hash) | |
hash = RUBY_VERSION >= '1.9' ? Hash.new : ActiveSupport::OrderedHash.new | |
object.each { |k, v| hash[k] = deeply_sort_hash(v) } | |
sorted = hash.sort_by { |(k, v)| [v.is_a?(String) ? 0 : 1, k.to_s.downcase] } | |
hash.class[sorted] | |
end | |
['de', 'en'].each do |locale| | |
files = Dir[Rails.root.join('config', 'locales', "*#{locale}.yml").to_s].sort; nil | |
translations = {}; nil | |
files.each { |file| translations.deep_merge!(YAML.load_file(file)) }; nil | |
output = Rails.root.join('config', 'locales', "#{locale}.yml").to_s | |
sorted = deeply_sort_hash(DeepHashTransformer.new(translations).stringify); nil | |
File.open(output, 'w') { |file| file.write(sorted.to_yaml) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment