Skip to content

Instantly share code, notes, and snippets.

@peter
Created January 13, 2010 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter/276115 to your computer and use it in GitHub Desktop.
Save peter/276115 to your computer and use it in GitHub Desktop.
# In lib/tasks/deploy.rake
namespace :deploy do
def write_error_page(status, locale = nil)
dest_filename = [status.to_s, locale, "html"].compact.join(".")
File.open(File.join(Rails.root, "public", dest_filename), "w") do |file|
path = File.join(Rails.root, "app", "views", "errors", "#{status}.rhtml")
file.print ERB.new(File.read(path)).result
end
end
def default_error_page_locale
:en
end
desc 'Write public/404.html and public/500.html error pages'
task :write_error_pages => :environment do
[500, 404].each do |status|
I18n.valid_locales.each do |locale| # I18n.available_locales except :root
I18n.with_locale locale do
write_error_page(status, locale)
# We need a default error page for when Rails doesn't work
write_error_page(status) if locale.to_sym == default_error_page_locale
end
end
end
end
end
# Two utility methods below that probably belong under config/initializers
module I18n
def self.with_locale(locale, &block)
orig_locale = self.locale
self.locale = locale
return_value = yield
self.locale = orig_locale
return_value
end
end
def I18n.valid_locales
I18n.available_locales.reject { |locale| locale == :root }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment