Merge pull request #17676 from tigrish/fix_custom_i18n_exception_handler_regression

Fix I18n regression introduced by #13832
This commit is contained in:
Rafael Mendonça França 2015-01-05 14:35:50 -03:00
parent 19b9086585
commit 761f6cde7d
2 changed files with 32 additions and 6 deletions

View File

@ -39,12 +39,14 @@ module ActionView
remaining_defaults = Array(options.delete(:default))
options[:default] = remaining_defaults.shift if remaining_defaults.first.kind_of? String
# If the user has specified rescue_format then pass it all through, otherwise use
# raise and do the work ourselves
options[:raise] ||= ActionView::Base.raise_on_missing_translations
raise_error = options[:raise] || options.key?(:rescue_format)
unless raise_error
# If the user has explicitly decided to NOT raise errors, pass that option to I18n.
# Otherwise, tell I18n to raise an exception, which we rescue further in this method.
# Note: `raise_error` refers to us re-raising the error in this method. I18n is forced to raise by default.
if options[:raise] == false || (options.key?(:rescue_format) && options[:rescue_format].nil?)
raise_error = false
options[:raise] = false
else
raise_error = options[:raise] || options[:rescue_format] || ActionView::Base.raise_on_missing_translations
options[:raise] = true
end

View File

@ -1,5 +1,13 @@
require 'abstract_unit'
module I18n
class CustomExceptionHandler
def self.call(exception, locale, key, options)
'from CustomExceptionHandler'
end
end
end
class TranslationHelperTest < ActiveSupport::TestCase
include ActionView::Helpers::TranslationHelper
@ -68,6 +76,22 @@ class TranslationHelperTest < ActiveSupport::TestCase
end
end
def test_uses_custom_exception_handler_when_specified
old_exception_handler = I18n.exception_handler
I18n.exception_handler = I18n::CustomExceptionHandler
assert_equal 'from CustomExceptionHandler', translate(:"translations.missing", raise: false)
ensure
I18n.exception_handler = old_exception_handler
end
def test_uses_custom_exception_handler_when_specified_for_html
old_exception_handler = I18n.exception_handler
I18n.exception_handler = I18n::CustomExceptionHandler
assert_equal 'from CustomExceptionHandler', translate(:"translations.missing_html", raise: false)
ensure
I18n.exception_handler = old_exception_handler
end
def test_i18n_translate_defaults_to_nil_rescue_format
expected = 'translation missing: en.translations.missing'
assert_equal expected, I18n.translate(:"translations.missing")