Skip to content

Commit

Permalink
Exclude I18n reserved keys from html escaping (#1953)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
nickcoyne and joelhawksley authored Jan 8, 2024
1 parent 1d19f88 commit 5a55ad5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Exclude html escaping of I18n reserved keys with `I18n::RESERVED_KEYS` rather than `I18n.reserved_keys_pattern`.

*Nick Coyne*

* Update CI configuration to use `Appraisal`.

*Hans Lemuet, Simon Fish*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ ViewComponents are Ruby objects, making it easy to follow (and enforce) code qua

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

<img src="https://avatars.githubusercontent.com/nickcoyne?s=64" alt="nickcoyne" width="32" />
<img src="https://avatars.githubusercontent.com/nachiket87?s=64" alt="nachiket87" width="32" />
<img src="https://avatars.githubusercontent.com/andrewjtait?s=64" alt="andrewjtait" width="32" />
<img src="https://avatars.githubusercontent.com/asgerb?s=64" alt="asgerb" width="32" />
Expand Down
3 changes: 1 addition & 2 deletions lib/view_component/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ def html_safe_translation(translation)
end

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

options[name] = ERB::Util.html_escape(value.to_s)
Expand Down
3 changes: 3 additions & 0 deletions test/sandbox/config/initializers/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

I18n.available_locales = [:en, :fr].freeze
8 changes: 8 additions & 0 deletions test/sandbox/test/translatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def test_translate_with_html_suffix_does_not_double_escape
)
end

def test_translate_with_html_suffix_applies_reserved_options
translation = translate(".hello_html", locale: :fr, raise: false)
assert_equal(
"Translation missing: fr.translatable_component.hello_html",
translation
)
end

def test_translate_uses_the_helper_when_no_sidecar_file_is_provided
# The cache needs to be kept clean for TranslatableComponent, otherwise it will rely on the
# already created i18n_backend.
Expand Down

0 comments on commit 5a55ad5

Please sign in to comment.