From 50f3754525c61e3ea84a407eb571617f2f39d6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 May 2010 23:36:43 +0200 Subject: [PATCH] Ensure translations work with symbols. --- .../lib/action_view/helpers/translation_helper.rb | 13 ++++++++----- actionpack/test/template/translation_helper_test.rb | 8 +++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index eb05a158c1792..60d0bb7cb0a9f 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -14,13 +14,14 @@ def translate(keys, options = {}) if keys.is_a?(Array) ActiveSupport::Deprecation.warn "Giving an array to translate is deprecated, please give a symbol or a string instead", caller end - options[:raise] = true - are_keys_a_string = keys.is_a?(String) + + options[:raise] = true + return_first = keys.is_a?(String) || keys.is_a?(Symbol) keys = scope_keys_by_partial(keys) translations = I18n.translate(keys, options) translations = html_safe_translation_keys(keys, Array.wrap(translations)) - are_keys_a_string ? translations.first : translations + return_first ? translations.first : translations rescue I18n::MissingTranslationData => e keys = I18n.send(:normalize_translation_keys, e.locale, e.key, e.options[:scope]) content_tag('span', keys.join(', '), :class => 'translation_missing') @@ -37,8 +38,10 @@ def localize(*args) private def scope_keys_by_partial(keys) Array.wrap(keys).map do |key| - if key.to_s.first == "." - template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s + key = key.to_s + + if key.first == "." + template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key else key end diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index 5d12bcc025426..4887c663d83d2 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -9,7 +9,7 @@ def setup end def test_delegates_to_i18n_setting_the_raise_option - I18n.expects(:translate).with([:foo], :locale => 'en', :raise => true).returns([""]) + I18n.expects(:translate).with(['foo'], :locale => 'en', :raise => true).returns([""]) translate :foo, :locale => 'en' end @@ -55,6 +55,12 @@ def test_scoping_by_partial_of_an_array end end + def test_translate_works_with_symbols + I18n.expects(:translate).with(["hello"], :raise => true).returns(["Hello World"]) + assert_equal "Hello World", translate(:hello) + end + + def test_translate_does_not_mark_plain_text_as_safe_html I18n.expects(:translate).with(["hello"], :raise => true).returns(["Hello World"]) assert_equal false, translate("hello").html_safe?