Skip to content

Commit 85c58ff

Browse files
authored
Merge pull request rails#51156 from jhawthorn/translation_exception
Respect raise_on_missing_translations in controller's translations
2 parents 939742d + 0f870c4 commit 85c58ff

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

actionpack/lib/abstract_controller/translation.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,7 @@ def translate(key, **options)
3030
end
3131
end
3232

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

@@ -50,9 +39,5 @@ def localize(object, **options)
5039
I18n.localize(object, **options)
5140
end
5241
alias :l :localize
53-
54-
private
55-
MISSING_TRANSLATION = -(2**60)
56-
private_constant :MISSING_TRANSLATION
5742
end
5843
end

actionpack/test/abstract/translation_test.rb

Lines changed: 6 additions & 2 deletions
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

Lines changed: 12 additions & 2 deletions
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)