Skip to content

Instantly share code, notes, and snippets.

@gacha
Created August 24, 2012 06:53
Show Gist options
  • Save gacha/3446857 to your computer and use it in GitHub Desktop.
Save gacha/3446857 to your computer and use it in GitHub Desktop.
Merge rails i18n YAML files
# LOAD RAILS ENV ...
def returning(value)
yield(value)
value
end
def convert_hash_to_ordered_hash_and_sort(object, deep = false)
# from http://seb.box.re/2010/1/15/deep-hash-ordering-with-ruby-1-8/
if object.is_a?(Hash)
# Hash is ordered in Ruby 1.9!
res = returning(RUBY_VERSION >= '1.9' ? Hash.new : ActiveSupport::OrderedHash.new) do |map|
object.each {|k, v| map[k.to_s] = deep ? convert_hash_to_ordered_hash_and_sort(v, deep) : v }
end
return res.class[res.sort {|a, b| a[0].to_s <=> b[0].to_s } ]
elsif deep && object.is_a?(Array)
array = Array.new
object.each_with_index {|v, i| array[i] = convert_hash_to_ordered_hash_and_sort(v, deep) }
return array
else
return object
end
end
translations = convert_hash_to_ordered_hash_and_sort(I18n.backend.send(:translations),true)
f = File.open(Rails.root.join('en.yml'),'w+')
f.write translations[:en].to_yaml
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment