Skip to content

Instantly share code, notes, and snippets.

@rtlong
Forked from aulizko/locale.rb
Created March 5, 2012 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtlong/1976194 to your computer and use it in GitHub Desktop.
Save rtlong/1976194 to your computer and use it in GitHub Desktop.
Exports $app/config/locale/$locale.yml contents to mongodb database
# 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