Skip to content

Commit 5a55ad5

Browse files
Exclude I18n reserved keys from html escaping (ViewComponent#1953)
* Use RESERVED_KEYS constant directly, as the pattern only matches an interpolated string. * Simplify test, update changelog. * Add name to contributors * Add newline --------- Co-authored-by: Joel Hawksley <[email protected]>
1 parent 1d19f88 commit 5a55ad5

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Exclude html escaping of I18n reserved keys with `I18n::RESERVED_KEYS` rather than `I18n.reserved_keys_pattern`.
14+
15+
*Nick Coyne*
16+
1317
* Update CI configuration to use `Appraisal`.
1418

1519
*Hans Lemuet, Simon Fish*

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ ViewComponents are Ruby objects, making it easy to follow (and enforce) code qua
110110

111111
ViewComponent is built by over a hundred members of the community, including:
112112

113+
<img src="https://avatars.githubusercontent.com/nickcoyne?s=64" alt="nickcoyne" width="32" />
113114
<img src="https://avatars.githubusercontent.com/nachiket87?s=64" alt="nachiket87" width="32" />
114115
<img src="https://avatars.githubusercontent.com/andrewjtait?s=64" alt="andrewjtait" width="32" />
115116
<img src="https://avatars.githubusercontent.com/asgerb?s=64" alt="asgerb" width="32" />

lib/view_component/translatable.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ def html_safe_translation(translation)
138138
end
139139

140140
def html_escape_translation_options!(options)
141-
options.each do |name, value|
142-
next if ::I18n.reserved_keys_pattern.match?(name)
141+
options.except(*::I18n::RESERVED_KEYS).each do |name, value|
143142
next if name == :count && value.is_a?(Numeric)
144143

145144
options[name] = ERB::Util.html_escape(value.to_s)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
I18n.available_locales = [:en, :fr].freeze

test/sandbox/test/translatable_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def test_translate_with_html_suffix_does_not_double_escape
7575
)
7676
end
7777

78+
def test_translate_with_html_suffix_applies_reserved_options
79+
translation = translate(".hello_html", locale: :fr, raise: false)
80+
assert_equal(
81+
"Translation missing: fr.translatable_component.hello_html",
82+
translation
83+
)
84+
end
85+
7886
def test_translate_uses_the_helper_when_no_sidecar_file_is_provided
7987
# The cache needs to be kept clean for TranslatableComponent, otherwise it will rely on the
8088
# already created i18n_backend.

0 commit comments

Comments
 (0)