Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude I18n reserved keys from html escaping #1953

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading