From e902c0f4fbaf05b3b01ee9adf532b464e31cdedb Mon Sep 17 00:00:00 2001 From: Nick Coyne Date: Fri, 5 Jan 2024 12:08:52 +1300 Subject: [PATCH 1/4] Use RESERVED_KEYS constant directly, as the pattern only matches an interpolated string. --- lib/view_component/translatable.rb | 3 +-- test/sandbox/config/initializers/i18n.rb | 1 + test/sandbox/test/translatable_test.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 test/sandbox/config/initializers/i18n.rb diff --git a/lib/view_component/translatable.rb b/lib/view_component/translatable.rb index 42e47eed3..422218207 100644 --- a/lib/view_component/translatable.rb +++ b/lib/view_component/translatable.rb @@ -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) diff --git a/test/sandbox/config/initializers/i18n.rb b/test/sandbox/config/initializers/i18n.rb new file mode 100644 index 000000000..e0826f2ca --- /dev/null +++ b/test/sandbox/config/initializers/i18n.rb @@ -0,0 +1 @@ +I18n.available_locales = [:en, :fr].freeze \ No newline at end of file diff --git a/test/sandbox/test/translatable_test.rb b/test/sandbox/test/translatable_test.rb index 7564d0c8a..46b5b7c5f 100644 --- a/test/sandbox/test/translatable_test.rb +++ b/test/sandbox/test/translatable_test.rb @@ -75,6 +75,14 @@ def test_translate_with_html_suffix_does_not_double_escape ) end + def test_translate_with_html_suffix_uses_fallback_for_missing_translations + translation = translate(".hello_html", locale: :fr, raise: false, fallback: true) + 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. From 7c86eb7b2849f12fc2d45d56988641d38b1da415 Mon Sep 17 00:00:00 2001 From: Nick Coyne Date: Fri, 5 Jan 2024 12:40:24 +1300 Subject: [PATCH 2/4] Simplify test, update changelog. --- docs/CHANGELOG.md | 4 ++++ test/sandbox/test/translatable_test.rb | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5f1b01e01..ad57e38dc 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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* diff --git a/test/sandbox/test/translatable_test.rb b/test/sandbox/test/translatable_test.rb index 46b5b7c5f..3d1b7129b 100644 --- a/test/sandbox/test/translatable_test.rb +++ b/test/sandbox/test/translatable_test.rb @@ -75,8 +75,8 @@ def test_translate_with_html_suffix_does_not_double_escape ) end - def test_translate_with_html_suffix_uses_fallback_for_missing_translations - translation = translate(".hello_html", locale: :fr, raise: false, fallback: true) + 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 From 39db054a32bee344491e8c56a5e34b1f89b3e329 Mon Sep 17 00:00:00 2001 From: Nick Coyne Date: Fri, 5 Jan 2024 12:43:16 +1300 Subject: [PATCH 3/4] Add name to contributors --- docs/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/index.md b/docs/index.md index 24f750a5c..d68fd89cd 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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: +nickcoyne nachiket87 andrewjtait asgerb From 4540863c398df9a1629823e54c2f3814536c56ae Mon Sep 17 00:00:00 2001 From: Nick Coyne Date: Fri, 5 Jan 2024 13:01:59 +1300 Subject: [PATCH 4/4] Add newline --- test/sandbox/config/initializers/i18n.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/sandbox/config/initializers/i18n.rb b/test/sandbox/config/initializers/i18n.rb index e0826f2ca..b99a2594c 100644 --- a/test/sandbox/config/initializers/i18n.rb +++ b/test/sandbox/config/initializers/i18n.rb @@ -1 +1,3 @@ -I18n.available_locales = [:en, :fr].freeze \ No newline at end of file +# frozen_string_literal: true + +I18n.available_locales = [:en, :fr].freeze