Skip to content

Commit 603a679

Browse files
committed
Don't html-escape the :count option to translate if it's a Numeric. Fixes rails#3685.
1 parent 86b5e81 commit 603a679

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

actionpack/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
persistent between requests so if you need to manipulate the environment
6363
for your test you need to do it before the cookie jar is created.
6464

65+
## Rails 3.1.3 (unreleased) ##
66+
67+
* Fix using `tranlate` helper with a html translation which uses the `:count` option for
68+
pluralization.
69+
70+
*Jon Leighton*
71+
6572
## Rails 3.1.2 (unreleased) ##
6673

6774
* Fix XSS security vulnerability in the `translate` helper method. When using interpolation

actionpack/lib/action_view/helpers/translation_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def translate(key, options = {})
4848
if html_safe_translation_key?(key)
4949
html_safe_options = options.dup
5050
options.except(*I18n::RESERVED_KEYS).each do |name, value|
51-
html_safe_options[name] = ERB::Util.html_escape(value.to_s)
51+
unless name == :count && value.is_a?(Numeric)
52+
html_safe_options[name] = ERB::Util.html_escape(value.to_s)
53+
end
5254
end
5355
translation = I18n.translate(scope_key_by_partial(key), html_safe_options)
5456

actionpack/test/template/translation_helper_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ def setup
1919
:hello_html => '<a>Hello World</a>',
2020
:interpolated_html => '<a>Hello %{word}</a>',
2121
:array_html => %w(foo bar),
22-
:array => %w(foo bar)
22+
:array => %w(foo bar),
23+
:count_html => {
24+
:one => '<a>One %{count}</a>',
25+
:other => '<a>Other %{count}</a>'
26+
}
2327
}
2428
)
2529
@view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
@@ -89,6 +93,12 @@ def test_translate_escapes_interpolations_in_translations_with_a_html_suffix
8993
assert_equal '<a>Hello &lt;World&gt;</a>', translate(:'translations.interpolated_html', :word => stub(:to_s => "<World>"))
9094
end
9195

96+
def test_translate_with_html_count
97+
assert_equal '<a>One 1</a>', translate(:'translations.count_html', :count => 1)
98+
assert_equal '<a>Other 2</a>', translate(:'translations.count_html', :count => 2)
99+
assert_equal '<a>Other &lt;One&gt;</a>', translate(:'translations.count_html', :count => '<One>')
100+
end
101+
92102
def test_translation_returning_an_array_ignores_html_suffix
93103
assert_equal ["foo", "bar"], translate(:'translations.array_html')
94104
end

0 commit comments

Comments
 (0)