From 40a8130bc5f00f0d77b152e75484f175d70cabdd Mon Sep 17 00:00:00 2001 From: printercu Date: Wed, 7 Aug 2013 12:33:28 +0400 Subject: ActionController#translate supports symbols Made it similar to views helper. --- actionpack/CHANGELOG.md | 4 ++++ actionpack/lib/abstract_controller/translation.rb | 9 +++----- actionpack/test/abstract/translation_test.rb | 27 ++++++++++++++++++----- 3 files changed, 28 insertions(+), 12 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 8b78e0f801..7102cbd734 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,7 @@ +* ActionController#translate supports symbols. + + *Max Melentiev* + * Introduce `BasicRendering` which is the most basic rendering implementation. It allows to `render :text` and `render :nothing` without depending on Action View. diff --git a/actionpack/lib/abstract_controller/translation.rb b/actionpack/lib/abstract_controller/translation.rb index 02028d8e05..ea2551cb70 100644 --- a/actionpack/lib/abstract_controller/translation.rb +++ b/actionpack/lib/abstract_controller/translation.rb @@ -8,14 +8,11 @@ module AbstractController # I18n.translate("people.index.foo"). This makes it less repetitive # to translate many keys within the same controller / action and gives you a # simple framework for scoping them consistently. - def translate(*args) - key = args.first - if key.is_a?(String) && (key[0] == '.') + def translate(key, options = {}) + if key.to_s.first == '.' key = "#{ controller_path.tr('/', '.') }.#{ action_name }#{ key }" - args[0] = key end - - I18n.translate(*args) + I18n.translate(key, options) end alias :t :translate diff --git a/actionpack/test/abstract/translation_test.rb b/actionpack/test/abstract/translation_test.rb index 4fdc480b43..d43a445535 100644 --- a/actionpack/test/abstract/translation_test.rb +++ b/actionpack/test/abstract/translation_test.rb @@ -9,6 +9,20 @@ module AbstractController class TranslationControllerTest < ActiveSupport::TestCase def setup @controller = TranslationController.new + I18n.backend.store_translations(:en, { + one: { + two: 'bar', + }, + abstract_controller: { + testing: { + translation: { + index: { + foo: 'bar', + }, + }, + }, + }, + }) end def test_action_controller_base_responds_to_translate @@ -28,16 +42,17 @@ module AbstractController end def test_lazy_lookup - expected = 'bar' @controller.stubs(action_name: :index) - I18n.stubs(:translate).with('abstract_controller.testing.translation.index.foo').returns(expected) - assert_equal expected, @controller.t('.foo') + assert_equal 'bar', @controller.t('.foo') + end + + def test_lazy_lookup_with_symbol + @controller.stubs(action_name: :index) + assert_equal 'bar', @controller.t(:'.foo') end def test_default_translation - key, expected = 'one.two', 'bar' - I18n.stubs(:translate).with(key).returns(expected) - assert_equal expected, @controller.t(key) + assert_equal 'bar', @controller.t('one.two') end def test_localize -- cgit v1.2.3 From fde7344542a76e7f1f95a2265ac5480a1ef9aeed Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Tue, 22 Oct 2013 18:14:50 +0400 Subject: ActionController#translate also lookups shortcut without action name --- actionpack/CHANGELOG.md | 3 ++- actionpack/lib/abstract_controller/translation.rb | 6 +++++- actionpack/test/abstract/translation_test.rb | 8 ++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 7102cbd734..9f7c21b177 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,4 +1,5 @@ -* ActionController#translate supports symbols. +* ActionController#translate supports symbols as shortcuts. + When shortcut is given it also lookups without action name. *Max Melentiev* diff --git a/actionpack/lib/abstract_controller/translation.rb b/actionpack/lib/abstract_controller/translation.rb index ea2551cb70..56b8ce895e 100644 --- a/actionpack/lib/abstract_controller/translation.rb +++ b/actionpack/lib/abstract_controller/translation.rb @@ -10,7 +10,11 @@ module AbstractController # simple framework for scoping them consistently. def translate(key, options = {}) if key.to_s.first == '.' - key = "#{ controller_path.tr('/', '.') }.#{ action_name }#{ key }" + path = controller_path.tr('/', '.') + defaults = [:"#{path}#{key}"] + defaults << options[:default] if options[:default] + options[:default] = defaults + key = "#{path}.#{action_name}#{key}" end I18n.translate(key, options) end diff --git a/actionpack/test/abstract/translation_test.rb b/actionpack/test/abstract/translation_test.rb index d43a445535..8289252dfc 100644 --- a/actionpack/test/abstract/translation_test.rb +++ b/actionpack/test/abstract/translation_test.rb @@ -19,10 +19,12 @@ module AbstractController index: { foo: 'bar', }, + no_action: 'no_action_tr', }, }, }, }) + @controller.stubs(action_name: :index) end def test_action_controller_base_responds_to_translate @@ -42,15 +44,17 @@ module AbstractController end def test_lazy_lookup - @controller.stubs(action_name: :index) assert_equal 'bar', @controller.t('.foo') end def test_lazy_lookup_with_symbol - @controller.stubs(action_name: :index) assert_equal 'bar', @controller.t(:'.foo') end + def test_lazy_lookup_fallback + assert_equal 'no_action_tr', @controller.t(:'.no_action') + end + def test_default_translation assert_equal 'bar', @controller.t('one.two') end -- cgit v1.2.3