From 896e25e994e272a1cb485d9a850501b488992cb2 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Sun, 14 Nov 2010 18:46:40 +0100 Subject: Make TranslationHelper#translate use the :rescue_format option in I18n 0.5.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't catch exceptions here. Instead only declare that we want exceptions to be rescued as :html, but also let users configure reactions to exceptions in I18n. Signed-off-by: José Valim --- .../test/fixtures/test/scoped_translation.erb | 1 - actionpack/test/fixtures/test/translation.erb | 1 - .../test/fixtures/translations/templates/array.erb | 1 + .../test/fixtures/translations/templates/found.erb | 1 + .../fixtures/translations/templates/missing.erb | 1 + .../test/template/translation_helper_test.rb | 67 +++++++++++++--------- 6 files changed, 43 insertions(+), 29 deletions(-) delete mode 100644 actionpack/test/fixtures/test/scoped_translation.erb delete mode 100644 actionpack/test/fixtures/test/translation.erb create mode 100644 actionpack/test/fixtures/translations/templates/array.erb create mode 100644 actionpack/test/fixtures/translations/templates/found.erb create mode 100644 actionpack/test/fixtures/translations/templates/missing.erb (limited to 'actionpack/test') diff --git a/actionpack/test/fixtures/test/scoped_translation.erb b/actionpack/test/fixtures/test/scoped_translation.erb deleted file mode 100644 index 3be63ab3cc..0000000000 --- a/actionpack/test/fixtures/test/scoped_translation.erb +++ /dev/null @@ -1 +0,0 @@ -<%= t('.foo.bar').join %> \ No newline at end of file diff --git a/actionpack/test/fixtures/test/translation.erb b/actionpack/test/fixtures/test/translation.erb deleted file mode 100644 index 81a837d1ff..0000000000 --- a/actionpack/test/fixtures/test/translation.erb +++ /dev/null @@ -1 +0,0 @@ -<%= t('.helper') %> \ No newline at end of file diff --git a/actionpack/test/fixtures/translations/templates/array.erb b/actionpack/test/fixtures/translations/templates/array.erb new file mode 100644 index 0000000000..d86045a172 --- /dev/null +++ b/actionpack/test/fixtures/translations/templates/array.erb @@ -0,0 +1 @@ +<%= t('.foo.bar') %> diff --git a/actionpack/test/fixtures/translations/templates/found.erb b/actionpack/test/fixtures/translations/templates/found.erb new file mode 100644 index 0000000000..080c9c0aee --- /dev/null +++ b/actionpack/test/fixtures/translations/templates/found.erb @@ -0,0 +1 @@ +<%= t('.foo') %> diff --git a/actionpack/test/fixtures/translations/templates/missing.erb b/actionpack/test/fixtures/translations/templates/missing.erb new file mode 100644 index 0000000000..0f3f17f8ef --- /dev/null +++ b/actionpack/test/fixtures/translations/templates/missing.erb @@ -0,0 +1 @@ +<%= t('.missing') %> diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index 763080550b..67d459136b 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -4,60 +4,73 @@ class TranslationHelperTest < ActiveSupport::TestCase include ActionView::Helpers::TagHelper include ActionView::Helpers::TranslationHelper - attr_reader :request + attr_reader :request, :view + def setup + I18n.backend.store_translations(:en, + :translations => { + :templates => { + :found => { :foo => 'Foo' }, + :array => { :foo => { :bar => 'Foo Bar' } } + }, + :foo => 'Foo', + :hello => 'Hello World', + :html => 'Hello World', + :hello_html => 'Hello World', + :array_html => %w(foo bar), + :array => %w(foo bar) + } + ) + @view = ::ActionView::Base.new(ActionController::Base.view_paths, {}) end - def test_delegates_to_i18n_setting_the_raise_option - I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true).returns("") + def test_delegates_to_i18n_setting_the_rescue_format_option_to_html + I18n.expects(:translate).with(:foo, :locale => 'en', :rescue_format => :html).returns("") translate :foo, :locale => 'en' end + def test_delegates_localize_to_i18n + @time = Time.utc(2008, 7, 8, 12, 18, 38) + I18n.expects(:localize).with(@time) + localize @time + end + def test_returns_missing_translation_message_wrapped_into_span - expected = 'en, foo' - assert_equal expected, translate(:foo) + expected = 'Missing' + assert_equal expected, translate(:"translations.missing") end def test_translation_returning_an_array - I18n.expects(:translate).with(:foo, :raise => true).returns(["foo", "bar"]) - assert_equal ["foo", "bar"], translate(:foo) + expected = %w(foo bar) + assert_equal expected, translate(:"translations.array") end - def test_delegates_localize_to_i18n - @time = Time.utc(2008, 7, 8, 12, 18, 38) - I18n.expects(:localize).with(@time) - localize @time + def test_finds_translation_scoped_by_partial + assert_equal 'Foo', view.render(:file => 'translations/templates/found').strip end - def test_scoping_by_partial - I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper") - @view = ::ActionView::Base.new(ActionController::Base.view_paths, {}) - assert_equal "helper", @view.render(:file => "test/translation") + def test_finds_array_of_translations_scoped_by_partial + assert_equal 'Foo Bar', @view.render(:file => 'translations/templates/array').strip end - def test_scoping_by_partial_of_an_array - I18n.expects(:translate).with("test.scoped_translation.foo.bar", :raise => true).returns(["foo", "bar"]) - @view = ::ActionView::Base.new(ActionController::Base.view_paths, {}) - assert_equal "foobar", @view.render(:file => "test/scoped_translation") + def test_missing_translation_scoped_by_partial + expected = 'Missing' + assert_equal expected, view.render(:file => 'translations/templates/missing').strip 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? + assert_equal false, translate(:'translations.hello').html_safe? end def test_translate_marks_translations_named_html_as_safe_html - I18n.expects(:translate).with("html", :raise => true).returns("Hello World") - assert translate("html").html_safe? + assert translate(:'translations.html').html_safe? end def test_translate_marks_translations_with_a_html_suffix_as_safe_html - I18n.expects(:translate).with("hello_html", :raise => true).returns("Hello World") - assert translate("hello_html").html_safe? + assert translate(:'translations.hello_html').html_safe? 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) + assert_equal ["foo", "bar"], translate(:'translations.array_html') end end -- cgit v1.2.3