Skip to content

Commit

Permalink
Ensure translations work with symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 24, 2010
1 parent 4986d5e commit 50f3754
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 8 additions & 5 deletions actionpack/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -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')
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion actionpack/test/template/translation_helper_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 50f3754

Please sign in to comment.