Created
December 28, 2010 08:50
-
-
Save aulizko/757067 to your computer and use it in GitHub Desktop.
Exports $app/config/locale/$locale.yml contents to mongodb database
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
# usage: | |
# bundle exec rake locale:file RAILS_ENV=production | |
# if you want to export a different locale (not en.yml), provide locale option, as follows: | |
# bundle exec rake locale:file RAILS_ENV=production locale=ru | |
require 'mongo-i18n' | |
def write_to_database(sc, path, value) | |
key = path.join('.') | |
sc[key] = value.to_json | |
end | |
# traverse through hash | |
def traverse(sc, obj, path) | |
case obj | |
when Hash | |
obj.each do |k,v| | |
traverse(sc, v, path + [k]) | |
end | |
when Array | |
obj.each {|v| traverse(sc, v) } | |
else # end values | |
write_to_database(sc, path, obj) | |
end | |
end | |
namespace :locale do | |
desc <<-eos | |
Exports $app/config/locale/$locale.yml contents to mongodb database. | |
If locale is not specified, default (en) locale file will be exported. | |
eos | |
task :file do | |
locale = ENV['locale'] || "en" | |
environment = ENV['RAILS_ENV'] || "development" | |
# I keep mongodb connection descriptor in config/mongodb.yml | |
config = YAML::load(File.read(Rails.root.join('config/mongodb.yml'))) | |
collection = Mongo::Connection.new[config["development"]["database"]].collection('i18n') | |
store = MongoI18n::Store.new(collection) | |
dump = YAML::load(File.open(Rails.root.join("config","locales", "#{locale}.yml"))) | |
traverse(store, dump, []) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment