Permalink
Browse files

Ensure translations work with symbols.

  • Loading branch information...
1 parent 4986d5e commit 50f3754525c61e3ea84a407eb571617f2f39d6fe @josevalim josevalim committed May 24, 2010
@@ -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
@@ -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?

0 comments on commit 50f3754

Please sign in to comment.