From 603a679e87d0317957eb44ce3928d593a0c50bb5 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sat, 19 Nov 2011 13:19:20 +0000 Subject: Don't html-escape the :count option to translate if it's a Numeric. Fixes #3685. --- actionpack/CHANGELOG.md | 7 +++++++ actionpack/lib/action_view/helpers/translation_helper.rb | 4 +++- actionpack/test/template/translation_helper_test.rb | 12 +++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 9d847c763b..dc52262503 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -62,6 +62,13 @@ persistent between requests so if you need to manipulate the environment for your test you need to do it before the cookie jar is created. +## Rails 3.1.3 (unreleased) ## + +* Fix using `tranlate` helper with a html translation which uses the `:count` option for + pluralization. + + *Jon Leighton* + ## Rails 3.1.2 (unreleased) ## * Fix XSS security vulnerability in the `translate` helper method. When using interpolation diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index 42726f888c..cc74eff53a 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -48,7 +48,9 @@ module ActionView if html_safe_translation_key?(key) html_safe_options = options.dup options.except(*I18n::RESERVED_KEYS).each do |name, value| - html_safe_options[name] = ERB::Util.html_escape(value.to_s) + unless name == :count && value.is_a?(Numeric) + html_safe_options[name] = ERB::Util.html_escape(value.to_s) + end end translation = I18n.translate(scope_key_by_partial(key), html_safe_options) diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index cabb29cfad..397de9c2ce 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -19,7 +19,11 @@ class TranslationHelperTest < ActiveSupport::TestCase :hello_html => 'Hello World', :interpolated_html => 'Hello %{word}', :array_html => %w(foo bar), - :array => %w(foo bar) + :array => %w(foo bar), + :count_html => { + :one => 'One %{count}', + :other => 'Other %{count}' + } } ) @view = ::ActionView::Base.new(ActionController::Base.view_paths, {}) @@ -89,6 +93,12 @@ class TranslationHelperTest < ActiveSupport::TestCase assert_equal 'Hello <World>', translate(:'translations.interpolated_html', :word => stub(:to_s => "")) end + def test_translate_with_html_count + assert_equal 'One 1', translate(:'translations.count_html', :count => 1) + assert_equal 'Other 2', translate(:'translations.count_html', :count => 2) + assert_equal 'Other <One>', translate(:'translations.count_html', :count => '') + end + def test_translation_returning_an_array_ignores_html_suffix assert_equal ["foo", "bar"], translate(:'translations.array_html') end -- cgit v1.2.3