Skip to content

Commit 28bc4f8

Browse files
authored
Extract common behavior of html safe translation to a module
This module will be a private module in Active Support, this way if we need to change the behavior of translate in controllers or views don't forget to change in the other one.
1 parent a5247bb commit 28bc4f8

File tree

3 files changed

+50
-55
lines changed

3 files changed

+50
-55
lines changed
Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "active_support/html_safe_translation"
4+
35
module AbstractController
46
module Translation
57
mattr_accessor :raise_on_missing_translations, default: false
@@ -23,19 +25,7 @@ def translate(key, **options)
2325

2426
i18n_raise = options.fetch(:raise, self.raise_on_missing_translations)
2527

26-
if html_safe_translation_key?(key)
27-
html_safe_options = options.dup
28-
options.except(*I18n::RESERVED_KEYS).each do |name, value|
29-
unless name == :count && value.is_a?(Numeric)
30-
html_safe_options[name] = ERB::Util.html_escape(value.to_s)
31-
end
32-
end
33-
translation = I18n.translate(key, **html_safe_options, raise: i18n_raise)
34-
35-
translation.respond_to?(:html_safe) ? translation.html_safe : translation
36-
else
37-
I18n.translate(key, **options, raise: i18n_raise)
38-
end
28+
ActiveSupport::HtmlSafeTranslation.translate(key, **options, raise: i18n_raise)
3929
end
4030
alias :t :translate
4131

@@ -44,10 +34,5 @@ def localize(object, **options)
4434
I18n.localize(object, **options)
4535
end
4636
alias :l :localize
47-
48-
private
49-
def html_safe_translation_key?(key)
50-
/(\b|_|\.)html$/.match?(key.to_s)
51-
end
5237
end
5338
end

actionview/lib/action_view/helpers/translation_helper.rb

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "action_view/helpers/tag_helper"
4+
require "active_support/html_safe_translation"
45

56
module ActionView
67
# = Action View Translation Helpers
@@ -84,14 +85,9 @@ def translate(key, **options)
8485

8586
key = scope_key_by_partial(key)
8687

87-
if html_safe_translation_key?(key)
88-
html_safe_options ||= html_escape_translation_options(options)
89-
translated = I18n.translate(key, **html_safe_options, default: default)
90-
break html_safe_translation(translated) unless translated.equal?(MISSING_TRANSLATION)
91-
else
92-
translated = I18n.translate(key, **options, default: default)
93-
break translated unless translated.equal?(MISSING_TRANSLATION)
94-
end
88+
translated = ActiveSupport::HtmlSafeTranslation.translate(key, **options, default: default)
89+
90+
break translated unless translated.equal?(MISSING_TRANSLATION)
9591

9692
if alternatives.present? && !alternatives.first.is_a?(Symbol)
9793
break alternatives.first && I18n.translate(**options, default: alternatives)
@@ -126,10 +122,6 @@ def localize(object, **options)
126122
NO_DEFAULT = [].freeze
127123
private_constant :NO_DEFAULT
128124

129-
def self.i18n_option?(name)
130-
(@i18n_option_names ||= I18n::RESERVED_KEYS.to_set).include?(name)
131-
end
132-
133125
def scope_key_by_partial(key)
134126
if key&.start_with?(".")
135127
if @virtual_path
@@ -144,31 +136,6 @@ def scope_key_by_partial(key)
144136
end
145137
end
146138

147-
def html_escape_translation_options(options)
148-
return options if options.empty?
149-
html_safe_options = options.dup
150-
151-
options.each do |name, value|
152-
unless TranslationHelper.i18n_option?(name) || (name == :count && value.is_a?(Numeric))
153-
html_safe_options[name] = ERB::Util.html_escape(value.to_s)
154-
end
155-
end
156-
157-
html_safe_options
158-
end
159-
160-
def html_safe_translation_key?(key)
161-
/(?:_|\b)html\z/.match?(key)
162-
end
163-
164-
def html_safe_translation(translation)
165-
if translation.respond_to?(:map)
166-
translation.map { |element| element.respond_to?(:html_safe) ? element.html_safe : element }
167-
else
168-
translation.respond_to?(:html_safe) ? translation.html_safe : translation
169-
end
170-
end
171-
172139
def missing_translation(key, options)
173140
keys = I18n.normalize_keys(options[:locale] || I18n.locale, key, options[:scope])
174141

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveSupport
4+
module HtmlSafeTranslation # :nodoc:
5+
extend self
6+
7+
def translate(key, **options)
8+
if html_safe_translation_key?(key)
9+
html_safe_options = html_escape_translation_options(options)
10+
translation = I18n.translate(key, **html_safe_options)
11+
html_safe_translation(translation)
12+
else
13+
I18n.translate(key, **options)
14+
end
15+
end
16+
17+
private
18+
def html_safe_translation_key?(key)
19+
/(?:_|\b)html\z/.match?(key)
20+
end
21+
22+
def html_escape_translation_options(options)
23+
options.each do |name, value|
24+
unless i18n_option?(name) || (name == :count && value.is_a?(Numeric))
25+
options[name] = ERB::Util.html_escape(value.to_s)
26+
end
27+
end
28+
end
29+
30+
def i18n_option?(name)
31+
(@i18n_option_names ||= I18n::RESERVED_KEYS.to_set).include?(name)
32+
end
33+
34+
35+
def html_safe_translation(translation)
36+
if translation.respond_to?(:map)
37+
translation.map { |element| element.respond_to?(:html_safe) ? element.html_safe : element }
38+
else
39+
translation.respond_to?(:html_safe) ? translation.html_safe : translation
40+
end
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)