Skip to content

Commit

Permalink
Respect raise_on_missing_ in controller
Browse files Browse the repository at this point in the history
Previously raise_on_missing_translations was not being respected in a
controller. This commit brings back the correct behaviour.
  • Loading branch information
jhawthorn committed Feb 21, 2024
1 parent d73ed95 commit c25f0fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
17 changes: 1 addition & 16 deletions actionpack/lib/abstract_controller/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,7 @@ def translate(key, **options)
end
end

if options[:raise].nil?
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
ActiveSupport::HtmlSafeTranslation.translate(key, **options)
end
alias :t :translate

Expand All @@ -48,9 +37,5 @@ def localize(object, **options)
I18n.localize(object, **options)
end
alias :l :localize

private
MISSING_TRANSLATION = -(2**60)
private_constant :MISSING_TRANSLATION
end
end
8 changes: 6 additions & 2 deletions actionpack/test/abstract/translation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,19 @@ def test_translate_escapes_interpolations_in_translations_with_a_html_suffix
def test_translate_marks_translation_with_missing_html_key_as_safe_html
@controller.stub :action_name, :index do
translation = @controller.t("<tag>.html")
assert_equal "translation missing: <tag>.html", translation
assert_equal false, translation.html_safe?
assert_equal "Translation missing: en.<tag>.html", translation
end
end
def test_translate_marks_translation_with_missing_nested_html_key_as_safe_html
@controller.stub :action_name, :index do
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(<<~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
Expand Down
14 changes: 12 additions & 2 deletions activesupport/lib/active_support/html_safe_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ module HtmlSafeTranslation # :nodoc:
def translate(key, **options)
if html_safe_translation_key?(key)
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
I18n.translate(key, **options)
end
Expand Down

0 comments on commit c25f0fc

Please sign in to comment.