Skip to content

Commit c25f0fc

Browse files
committed
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.
1 parent d73ed95 commit c25f0fc

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

actionpack/lib/abstract_controller/translation.rb

+1-16
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,7 @@ def translate(key, **options)
2828
end
2929
end
3030

31-
if options[:raise].nil?
32-
options[:default] = [] unless options[:default]
33-
options[:default] << MISSING_TRANSLATION
34-
end
35-
36-
result = ActiveSupport::HtmlSafeTranslation.translate(key, **options)
37-
38-
if result == MISSING_TRANSLATION
39-
+"translation missing: #{key}"
40-
else
41-
result
42-
end
31+
ActiveSupport::HtmlSafeTranslation.translate(key, **options)
4332
end
4433
alias :t :translate
4534

@@ -48,9 +37,5 @@ def localize(object, **options)
4837
I18n.localize(object, **options)
4938
end
5039
alias :l :localize
51-
52-
private
53-
MISSING_TRANSLATION = -(2**60)
54-
private_constant :MISSING_TRANSLATION
5540
end
5641
end

actionpack/test/abstract/translation_test.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,19 @@ def test_translate_escapes_interpolations_in_translations_with_a_html_suffix
146146
def test_translate_marks_translation_with_missing_html_key_as_safe_html
147147
@controller.stub :action_name, :index do
148148
translation = @controller.t("<tag>.html")
149-
assert_equal "translation missing: <tag>.html", translation
150149
assert_equal false, translation.html_safe?
150+
assert_equal "Translation missing: en.<tag>.html", translation
151151
end
152152
end
153153
def test_translate_marks_translation_with_missing_nested_html_key_as_safe_html
154154
@controller.stub :action_name, :index do
155155
translation = @controller.t(".<tag>.html")
156-
assert_equal "translation missing: abstract_controller.testing.translation.index.<tag>.html", translation
157156
assert_equal false, translation.html_safe?
157+
assert_equal(<<~MSG.strip, translation)
158+
Translation missing. Options considered were:
159+
- en.abstract_controller.testing.translation.index.<tag>.html
160+
- en.abstract_controller.testing.translation.<tag>.html
161+
MSG
158162
end
159163
end
160164
end

activesupport/lib/active_support/html_safe_translation.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ module HtmlSafeTranslation # :nodoc:
77
def translate(key, **options)
88
if html_safe_translation_key?(key)
99
html_safe_options = html_escape_translation_options(options)
10-
translation = I18n.translate(key, **html_safe_options)
11-
html_safe_translation(translation)
10+
11+
exception = false
12+
exception_handler = ->(*args) do
13+
exception = true
14+
I18n.exception_handler.call(*args)
15+
end
16+
translation = I18n.translate(key, **html_safe_options, exception_handler: exception_handler)
17+
if exception
18+
translation
19+
else
20+
html_safe_translation(translation)
21+
end
1222
else
1323
I18n.translate(key, **options)
1424
end

0 commit comments

Comments
 (0)