Respect raise_on_missing_ in controller

Previously raise_on_missing_translations was not being respected in a
controller. This commit brings back the correct behaviour.
This commit is contained in:
John Hawthorn 2024-02-21 13:18:58 -08:00
parent 939742d69e
commit 0f870c4354
3 changed files with 19 additions and 20 deletions

View File

@ -30,18 +30,7 @@ module AbstractController
end end
end end
if options[:raise].nil? ActiveSupport::HtmlSafeTranslation.translate(key, **options)
options[:default] = [] unless options[:default]
options[:default] << MISSING_TRANSLATION
end
result = ActiveSupport::HtmlSafeTranslation.translate(key, **options)
if result == MISSING_TRANSLATION
+"translation missing: #{key}"
else
result
end
end end
alias :t :translate alias :t :translate
@ -50,9 +39,5 @@ module AbstractController
I18n.localize(object, **options) I18n.localize(object, **options)
end end
alias :l :localize alias :l :localize
private
MISSING_TRANSLATION = -(2**60)
private_constant :MISSING_TRANSLATION
end end
end end

View File

@ -146,15 +146,19 @@ module AbstractController
def test_translate_marks_translation_with_missing_html_key_as_safe_html def test_translate_marks_translation_with_missing_html_key_as_safe_html
@controller.stub :action_name, :index do @controller.stub :action_name, :index do
translation = @controller.t("<tag>.html") translation = @controller.t("<tag>.html")
assert_equal "translation missing: <tag>.html", translation
assert_equal false, translation.html_safe? assert_equal false, translation.html_safe?
assert_equal "Translation missing: en.<tag>.html", translation
end end
end end
def test_translate_marks_translation_with_missing_nested_html_key_as_safe_html def test_translate_marks_translation_with_missing_nested_html_key_as_safe_html
@controller.stub :action_name, :index do @controller.stub :action_name, :index do
translation = @controller.t(".<tag>.html") translation = @controller.t(".<tag>.html")
assert_equal "translation missing: abstract_controller.testing.translation.index.<tag>.html", translation
assert_equal false, translation.html_safe? assert_equal false, translation.html_safe?
assert_equal(<<~MSG.strip, translation)
Translation missing. Options considered were:
- en.abstract_controller.testing.translation.index.<tag>.html
- en.abstract_controller.testing.translation.<tag>.html
MSG
end end
end end
end end

View File

@ -7,8 +7,18 @@ module ActiveSupport
def translate(key, **options) def translate(key, **options)
if html_safe_translation_key?(key) if html_safe_translation_key?(key)
html_safe_options = html_escape_translation_options(options) html_safe_options = html_escape_translation_options(options)
translation = I18n.translate(key, **html_safe_options)
html_safe_translation(translation) exception = false
exception_handler = ->(*args) do
exception = true
I18n.exception_handler.call(*args)
end
translation = I18n.translate(key, **html_safe_options, exception_handler: exception_handler)
if exception
translation
else
html_safe_translation(translation)
end
else else
I18n.translate(key, **options) I18n.translate(key, **options)
end end