Skip to content

Commit

Permalink
Added localized rescue (404.da.html) [#1835 state:committed]
Browse files Browse the repository at this point in the history
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
josevalim authored and dhh committed Feb 2, 2009
1 parent beca1f2 commit 2ecc678
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG
@@ -1,3 +1,8 @@
*Edge*

* Added localized rescue template when I18n.locale is set (ex: public/404.da.html) #1835 [José Valim]


*2.3.0 [RC1] (February 1st, 2009)*

* Make the form_for and fields_for helpers support the new Active Record nested update options. #1202 [Eloy Duran]
Expand Down
16 changes: 11 additions & 5 deletions actionpack/lib/action_controller/rescue.rb
Expand Up @@ -99,13 +99,19 @@ def rescue_action_in_public(exception) #:doc:

# Attempts to render a static error page based on the
# <tt>status_code</tt> thrown, or just return headers if no such file
# exists. For example, if a 500 error is being handled Rails will first
# attempt to render the file at <tt>public/500.html</tt>. If the file
# doesn't exist, the body of the response will be left empty.
# exists. At first, it will try to render a localized static page.
# For example, if a 500 error is being handled Rails and locale is :da,
# it will first attempt to render the file at <tt>public/500.da.html</tt>
# then attempt to render <tt>public/500.html</tt>. If none of them exist,
# the body of the response will be left empty.
def render_optional_error_file(status_code)
status = interpret_status(status_code)
path = "#{Rails.public_path}/#{status.to_s[0,3]}.html"
if File.exist?(path)
locale_path = "#{Rails.public_path}/#{status[0,3]}.#{I18n.locale}.html" if I18n.locale
path = "#{Rails.public_path}/#{status[0,3]}.html"

if locale_path && File.exist?(locale_path)
render :file => locale_path, :status => status, :content_type => Mime::HTML
elsif File.exist?(path)
render :file => path, :status => status, :content_type => Mime::HTML
else
head status
Expand Down
25 changes: 25 additions & 0 deletions actionpack/test/controller/rescue_test.rb
Expand Up @@ -199,6 +199,31 @@ def test_rescue_action_in_public_otherwise
end
end

def test_rescue_action_in_public_with_localized_error_file
# Reload and register danish language for testing
I18n.reload!
I18n.backend.store_translations 'da', {}

# Ensure original are still the same since we are reindexing view paths
assert_equal ORIGINAL_LOCALES, I18n.available_locales.map(&:to_s).sort

# Change locale
old_locale = I18n.locale
I18n.locale = :da

with_rails_root FIXTURE_PUBLIC do
with_all_requests_local false do
get :raises
end
end

assert_response :internal_server_error
body = File.read("#{FIXTURE_PUBLIC}/public/500.da.html")
assert_equal body, @response.body
ensure
I18n.locale = old_locale
end

def test_rescue_action_in_public_with_error_file
with_rails_root FIXTURE_PUBLIC do
with_all_requests_local false do
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/public/500.da.html
@@ -0,0 +1 @@
500 localized error fixture

0 comments on commit 2ecc678

Please sign in to comment.