Created
July 16, 2010 20:31
-
-
Save tokumine/478876 to your computer and use it in GitHub Desktop.
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
# | |
# Shuffle locale YML files into 1 aggregate locale file | |
# if anything bad happens, back out and revert to last created yml | |
# | |
# Change en.yml to your base locale | |
# | |
namespace :i18n do | |
desc "Build en.yml file from separated translation files" | |
task :rebuild do | |
locale_files = Dir[File.join(RAILS_ROOT, 'config', 'locales', 'views', '**', '*.{rb,yml}')] | |
en_file = File.join(RAILS_ROOT, 'config', 'locales', 'en.yml') | |
en_file_old = File.join(RAILS_ROOT, 'config', 'locales', 'en_old.yml') | |
# move existing en.yml file to one side if it exists | |
if FileTest.exists? en_file | |
FileUtils.mv en_file, en_file_old, :force => true | |
end | |
begin | |
# aggregate the locales | |
agg_locs = YAML.load_file(locale_files.pop) | |
locale_files.each do |loc_yml| | |
agg_locs['en'].merge! YAML.load_file(loc_yml)['en'] # SEE IF WE CAN GET MERGE TO THROW EXCEPTION IF DUP | |
end | |
# write it back to a yml | |
File.open( en_file, 'w' ) do |out| | |
YAML.dump( agg_locs, out ) | |
end | |
rescue Exception => e | |
FileUtils.mv en_file_old, en_file, :force => true | |
ensure | |
FileUtils.rm en_file_old, :force => true | |
end | |
# done? | |
puts "linked all #{locale_files.size+1} locale files to #{en_file}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest to use 'deep_merge!' instead of 'merge!' to merge recursively all your yaml files.
http://www.misuse.org/science/2008/05/19/deep_merge-ruby-recursive-merging-for-hashes/
-Olivier Grimard