Skip to content

Instantly share code, notes, and snippets.

@charlypoly
Last active November 5, 2015 15:07
Show Gist options
  • Save charlypoly/bb6d348c4bfc80a89ca6 to your computer and use it in GitHub Desktop.
Save charlypoly/bb6d348c4bfc80a89ca6 to your computer and use it in GitHub Desktop.
Merge all yaml locale files into one by locale
# to run in a Rails console !
def returning(value)
yield(value)
value
end
class Hash
def deep_reject_key!(key)
keys.each {|k| delete(k) if k == key || self[k] == self[key] }
values.each {|v| v.deep_reject_key!(key) if v.is_a? Hash }
self
end
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); puts ''
translations.deep_reject_key!('faker'); puts '' # remove faker keys !
f = File.open(Rails.root.join('config/locales/en.yml'),'w+')
f.write({ 'en' => translations['en'] }.to_yaml); puts ''
f.close
f = File.open(Rails.root.join('config/locales/fr.yml'),'w+')
f.write({ 'fr' => translations['fr'] }.to_yaml); puts ''
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment