2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2008-07-06 13:00:55 -04:00
|
|
|
|
2014-11-19 09:08:56 -05:00
|
|
|
module I18n
|
|
|
|
class CustomExceptionHandler
|
|
|
|
def self.call(exception, locale, key, options)
|
2016-08-06 12:50:17 -04:00
|
|
|
"from CustomExceptionHandler"
|
2014-11-19 09:08:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-26 14:43:35 -05:00
|
|
|
class TranslationHelperTest < ActiveSupport::TestCase
|
2008-07-06 13:00:55 -04:00
|
|
|
include ActionView::Helpers::TranslationHelper
|
2010-01-26 14:43:35 -05:00
|
|
|
|
2010-11-14 12:46:40 -05:00
|
|
|
attr_reader :request, :view
|
|
|
|
|
2014-06-14 04:32:36 -04:00
|
|
|
setup do
|
2010-11-14 12:46:40 -05:00
|
|
|
I18n.backend.store_translations(:en,
|
|
|
|
:translations => {
|
|
|
|
:templates => {
|
2016-08-06 12:50:17 -04:00
|
|
|
:found => { :foo => "Foo" },
|
|
|
|
:array => { :foo => { :bar => "Foo Bar" } },
|
|
|
|
:default => { :foo => "Foo" }
|
2010-11-14 12:46:40 -05:00
|
|
|
},
|
2016-08-06 12:50:17 -04:00
|
|
|
:foo => "Foo",
|
|
|
|
:hello => "<a>Hello World</a>",
|
|
|
|
:html => "<a>Hello World</a>",
|
|
|
|
:hello_html => "<a>Hello World</a>",
|
|
|
|
:interpolated_html => "<a>Hello %{word}</a>",
|
2010-11-14 12:46:40 -05:00
|
|
|
:array_html => %w(foo bar),
|
2011-11-19 08:19:20 -05:00
|
|
|
:array => %w(foo bar),
|
|
|
|
:count_html => {
|
2016-08-06 12:50:17 -04:00
|
|
|
:one => "<a>One %{count}</a>",
|
|
|
|
:other => "<a>Other %{count}</a>"
|
2011-11-19 08:19:20 -05:00
|
|
|
}
|
2010-11-14 12:46:40 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
@view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2014-06-14 04:32:36 -04:00
|
|
|
teardown do
|
|
|
|
I18n.backend.reload!
|
|
|
|
end
|
|
|
|
|
2015-05-04 17:46:25 -04:00
|
|
|
def test_delegates_setting_to_i18n
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_called_with(I18n, :translate, [:foo, :locale => "en", :raise => true], returns: "") do
|
|
|
|
translate :foo, :locale => "en"
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-11-14 12:46:40 -05:00
|
|
|
def test_delegates_localize_to_i18n
|
|
|
|
@time = Time.utc(2008, 7, 8, 12, 18, 38)
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called_with(I18n, :localize, [@time]) do
|
|
|
|
localize @time
|
|
|
|
end
|
2010-11-14 12:46:40 -05:00
|
|
|
end
|
|
|
|
|
2015-12-30 00:12:50 -05:00
|
|
|
def test_returns_missing_translation_message_without_span_wrap
|
2015-12-01 10:49:44 -05:00
|
|
|
old_value = ActionView::Base.debug_missing_translation
|
|
|
|
ActionView::Base.debug_missing_translation = false
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
expected = "translation missing: en.translations.missing"
|
2015-12-01 10:49:44 -05:00
|
|
|
assert_equal expected, translate(:"translations.missing")
|
|
|
|
ensure
|
|
|
|
ActionView::Base.debug_missing_translation = old_value
|
|
|
|
end
|
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_returns_missing_translation_message_wrapped_into_span
|
2010-11-14 12:46:40 -05:00
|
|
|
expected = '<span class="translation_missing" title="translation missing: en.translations.missing">Missing</span>'
|
|
|
|
assert_equal expected, translate(:"translations.missing")
|
2011-06-14 20:20:50 -04:00
|
|
|
assert_equal true, translate(:"translations.missing").html_safe?
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
2015-09-15 04:20:10 -04:00
|
|
|
def test_returns_missing_translation_message_with_unescaped_interpolation
|
|
|
|
expected = '<span class="translation_missing" title="translation missing: en.translations.missing, name: Kir, year: 2015, vulnerable: &quot; onclick=&quot;alert()&quot;">Missing</span>'
|
|
|
|
assert_equal expected, translate(:"translations.missing", name: "Kir", year: "2015", vulnerable: %{" onclick="alert()"})
|
|
|
|
assert translate(:"translations.missing").html_safe?
|
|
|
|
end
|
|
|
|
|
2015-10-28 16:52:33 -04:00
|
|
|
def test_returns_missing_translation_message_does_filters_out_i18n_options
|
|
|
|
expected = '<span class="translation_missing" title="translation missing: en.translations.missing, year: 2015">Missing</span>'
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal expected, translate(:"translations.missing", year: "2015", default: [])
|
2015-10-28 16:52:33 -04:00
|
|
|
|
|
|
|
expected = '<span class="translation_missing" title="translation missing: en.scoped.translations.missing, year: 2015">Missing</span>'
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal expected, translate(:"translations.missing", year: "2015", scope: %i(scoped))
|
2015-10-28 16:52:33 -04:00
|
|
|
end
|
|
|
|
|
2014-01-26 17:05:34 -05:00
|
|
|
def test_raises_missing_translation_message_with_raise_config_option
|
|
|
|
ActionView::Base.raise_on_missing_translations = true
|
|
|
|
|
|
|
|
assert_raise(I18n::MissingTranslationData) do
|
|
|
|
translate("translations.missing")
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActionView::Base.raise_on_missing_translations = false
|
|
|
|
end
|
|
|
|
|
2013-12-03 22:48:21 -05:00
|
|
|
def test_raises_missing_translation_message_with_raise_option
|
|
|
|
assert_raise(I18n::MissingTranslationData) do
|
|
|
|
translate(:"translations.missing", :raise => true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-19 09:08:56 -05:00
|
|
|
def test_uses_custom_exception_handler_when_specified
|
2015-01-05 12:39:52 -05:00
|
|
|
old_exception_handler = I18n.exception_handler
|
2014-11-19 09:08:56 -05:00
|
|
|
I18n.exception_handler = I18n::CustomExceptionHandler
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "from CustomExceptionHandler", translate(:"translations.missing", raise: false)
|
2015-01-05 12:39:52 -05:00
|
|
|
ensure
|
|
|
|
I18n.exception_handler = old_exception_handler
|
2014-11-19 09:08:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uses_custom_exception_handler_when_specified_for_html
|
2015-01-05 12:39:52 -05:00
|
|
|
old_exception_handler = I18n.exception_handler
|
2014-11-19 09:08:56 -05:00
|
|
|
I18n.exception_handler = I18n::CustomExceptionHandler
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "from CustomExceptionHandler", translate(:"translations.missing_html", raise: false)
|
2015-01-05 12:39:52 -05:00
|
|
|
ensure
|
|
|
|
I18n.exception_handler = old_exception_handler
|
2014-11-19 09:08:56 -05:00
|
|
|
end
|
|
|
|
|
2010-05-24 23:16:50 -04:00
|
|
|
def test_translation_returning_an_array
|
2010-11-14 12:46:40 -05:00
|
|
|
expected = %w(foo bar)
|
|
|
|
assert_equal expected, translate(:"translations.array")
|
2010-05-24 23:16:50 -04:00
|
|
|
end
|
|
|
|
|
2010-11-14 12:46:40 -05:00
|
|
|
def test_finds_translation_scoped_by_partial
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Foo", view.render(:file => "translations/templates/found").strip
|
2008-07-08 06:37:49 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-11-14 12:46:40 -05:00
|
|
|
def test_finds_array_of_translations_scoped_by_partial
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Foo Bar", @view.render(:file => "translations/templates/array").strip
|
2009-02-10 06:57:12 -05:00
|
|
|
end
|
2010-03-16 19:12:04 -04:00
|
|
|
|
2012-04-30 02:44:39 -04:00
|
|
|
def test_default_lookup_scoped_by_partial
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Foo", view.render(:file => "translations/templates/default").strip
|
2012-04-30 02:44:39 -04:00
|
|
|
end
|
|
|
|
|
2010-11-14 12:46:40 -05:00
|
|
|
def test_missing_translation_scoped_by_partial
|
|
|
|
expected = '<span class="translation_missing" title="translation missing: en.translations.templates.missing.missing">Missing</span>'
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal expected, view.render(:file => "translations/templates/missing").strip
|
2010-03-16 19:12:04 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-04-09 21:01:32 -04:00
|
|
|
def test_translate_does_not_mark_plain_text_as_safe_html
|
2010-11-14 12:46:40 -05:00
|
|
|
assert_equal false, translate(:'translations.hello').html_safe?
|
2010-04-09 21:01:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_translate_marks_translations_named_html_as_safe_html
|
2010-11-14 12:46:40 -05:00
|
|
|
assert translate(:'translations.html').html_safe?
|
2010-04-09 21:01:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_translate_marks_translations_with_a_html_suffix_as_safe_html
|
2010-11-14 12:46:40 -05:00
|
|
|
assert translate(:'translations.hello_html').html_safe?
|
2010-04-09 21:01:32 -04:00
|
|
|
end
|
2010-05-24 23:16:50 -04:00
|
|
|
|
2011-11-17 10:29:55 -05:00
|
|
|
def test_translate_escapes_interpolations_in_translations_with_a_html_suffix
|
2015-08-22 00:03:53 -04:00
|
|
|
word_struct = Struct.new(:to_s)
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "<a>Hello <World></a>", translate(:'translations.interpolated_html', :word => "<World>")
|
|
|
|
assert_equal "<a>Hello <World></a>", translate(:'translations.interpolated_html', :word => word_struct.new("<World>"))
|
2011-11-17 10:29:55 -05:00
|
|
|
end
|
|
|
|
|
2011-11-19 08:19:20 -05:00
|
|
|
def test_translate_with_html_count
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "<a>One 1</a>", translate(:'translations.count_html', :count => 1)
|
|
|
|
assert_equal "<a>Other 2</a>", translate(:'translations.count_html', :count => 2)
|
|
|
|
assert_equal "<a>Other <One></a>", translate(:'translations.count_html', :count => "<One>")
|
2011-11-19 08:19:20 -05:00
|
|
|
end
|
|
|
|
|
2010-05-24 23:16:50 -04:00
|
|
|
def test_translation_returning_an_array_ignores_html_suffix
|
2010-11-14 12:46:40 -05:00
|
|
|
assert_equal ["foo", "bar"], translate(:'translations.array_html')
|
2010-05-24 23:16:50 -04:00
|
|
|
end
|
2012-04-30 02:44:39 -04:00
|
|
|
|
|
|
|
def test_translate_with_default_named_html
|
|
|
|
translation = translate(:'translations.missing', :default => :'translations.hello_html')
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "<a>Hello World</a>", translation
|
2012-05-22 02:56:23 -04:00
|
|
|
assert_equal true, translation.html_safe?
|
2012-04-30 02:44:39 -04:00
|
|
|
end
|
|
|
|
|
2015-05-04 16:26:07 -04:00
|
|
|
def test_translate_with_missing_default
|
|
|
|
translation = translate(:'translations.missing', :default => :'translations.missing_html')
|
|
|
|
expected = '<span class="translation_missing" title="translation missing: en.translations.missing_html">Missing Html</span>'
|
|
|
|
assert_equal expected, translation
|
|
|
|
assert_equal true, translation.html_safe?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_translate_with_missing_default_and_raise_option
|
|
|
|
assert_raise(I18n::MissingTranslationData) do
|
|
|
|
translate(:'translations.missing', :default => :'translations.missing_html', :raise => true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-30 02:44:39 -04:00
|
|
|
def test_translate_with_two_defaults_named_html
|
|
|
|
translation = translate(:'translations.missing', :default => [:'translations.missing_html', :'translations.hello_html'])
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "<a>Hello World</a>", translation
|
2012-05-22 02:56:23 -04:00
|
|
|
assert_equal true, translation.html_safe?
|
2012-04-30 02:44:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_translate_with_last_default_named_html
|
|
|
|
translation = translate(:'translations.missing', :default => [:'translations.missing', :'translations.hello_html'])
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "<a>Hello World</a>", translation
|
2012-05-22 02:56:23 -04:00
|
|
|
assert_equal true, translation.html_safe?
|
|
|
|
end
|
|
|
|
|
2015-01-02 18:07:01 -05:00
|
|
|
def test_translate_with_last_default_not_named_html
|
|
|
|
translation = translate(:'translations.missing', :default => [:'translations.missing_html', :'translations.foo'])
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Foo", translation
|
2015-01-02 18:07:01 -05:00
|
|
|
assert_equal false, translation.html_safe?
|
|
|
|
end
|
|
|
|
|
2012-05-22 02:56:23 -04:00
|
|
|
def test_translate_with_string_default
|
2016-08-06 12:50:17 -04:00
|
|
|
translation = translate(:'translations.missing', default: "A Generic String")
|
|
|
|
assert_equal "A Generic String", translation
|
2012-05-22 02:56:23 -04:00
|
|
|
end
|
|
|
|
|
2015-02-26 16:18:58 -05:00
|
|
|
def test_translate_with_object_default
|
|
|
|
translation = translate(:'translations.missing', default: 123)
|
|
|
|
assert_equal 123, translation
|
|
|
|
end
|
|
|
|
|
2012-05-22 02:56:23 -04:00
|
|
|
def test_translate_with_array_of_string_defaults
|
2016-08-06 12:50:17 -04:00
|
|
|
translation = translate(:'translations.missing', default: ["A Generic String", "Second generic string"])
|
|
|
|
assert_equal "A Generic String", translation
|
2012-04-30 02:44:39 -04:00
|
|
|
end
|
2014-05-12 11:20:51 -04:00
|
|
|
|
2015-03-20 16:58:54 -04:00
|
|
|
def test_translate_with_array_of_defaults_with_nil
|
2016-08-06 12:50:17 -04:00
|
|
|
translation = translate(:'translations.missing', default: [:'also_missing', nil, "A Generic String"])
|
|
|
|
assert_equal "A Generic String", translation
|
2015-03-20 16:58:54 -04:00
|
|
|
end
|
|
|
|
|
2015-04-04 12:46:45 -04:00
|
|
|
def test_translate_with_array_of_array_default
|
|
|
|
translation = translate(:'translations.missing', default: [[]])
|
|
|
|
assert_equal [], translation
|
|
|
|
end
|
|
|
|
|
2014-05-13 11:09:11 -04:00
|
|
|
def test_translate_does_not_change_options
|
2014-05-12 11:20:51 -04:00
|
|
|
options = {}
|
|
|
|
translate(:'translations.missing', options)
|
2014-05-13 11:09:11 -04:00
|
|
|
assert_equal({}, options)
|
2014-05-12 11:20:51 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|