提交 f53a6d81 编写于 作者: J Jeremy Kemper

i18n: t() handles single keys returning an Array, also

上级 d8d38bed
...@@ -18,7 +18,11 @@ module TranslationHelper ...@@ -18,7 +18,11 @@ module TranslationHelper
def translate(key, options = {}) def translate(key, options = {})
translation = I18n.translate(scope_key_by_partial(key), options.merge!(:raise => true)) translation = I18n.translate(scope_key_by_partial(key), options.merge!(:raise => true))
html_safe_translation_key?(key) ? translation.html_safe : translation if html_safe_translation_key?(key) && translation.respond_to?(:html_safe)
translation.html_safe
else
translation
end
rescue I18n::MissingTranslationData => e rescue I18n::MissingTranslationData => e
keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope]) keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
content_tag('span', keys.join(', '), :class => 'translation_missing') content_tag('span', keys.join(', '), :class => 'translation_missing')
...@@ -32,7 +36,6 @@ def localize(*args) ...@@ -32,7 +36,6 @@ def localize(*args)
alias :l :localize alias :l :localize
private private
def scope_key_by_partial(key) def scope_key_by_partial(key)
if key.to_s.first == "." if key.to_s.first == "."
if @_virtual_path if @_virtual_path
......
...@@ -18,6 +18,11 @@ def test_returns_missing_translation_message_wrapped_into_span ...@@ -18,6 +18,11 @@ def test_returns_missing_translation_message_wrapped_into_span
assert_equal expected, translate(:foo) assert_equal expected, translate(:foo)
end end
def test_translation_returning_an_array
I18n.expects(:translate).with(:foo, :raise => true).returns(["foo", "bar"])
assert_equal ["foo", "bar"], translate(:foo)
end
def test_delegates_localize_to_i18n def test_delegates_localize_to_i18n
@time = Time.utc(2008, 7, 8, 12, 18, 38) @time = Time.utc(2008, 7, 8, 12, 18, 38)
I18n.expects(:localize).with(@time) I18n.expects(:localize).with(@time)
...@@ -50,4 +55,9 @@ def test_translate_marks_translations_with_a_html_suffix_as_safe_html ...@@ -50,4 +55,9 @@ def test_translate_marks_translations_with_a_html_suffix_as_safe_html
I18n.expects(:translate).with("hello_html", :raise => true).returns("<a>Hello World</a>") I18n.expects(:translate).with("hello_html", :raise => true).returns("<a>Hello World</a>")
assert translate("hello_html").html_safe? assert translate("hello_html").html_safe?
end end
def test_translation_returning_an_array_ignores_html_suffix
I18n.expects(:translate).with(:foo_html, :raise => true).returns(["foo", "bar"])
assert_equal ["foo", "bar"], translate(:foo_html)
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册