aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Melentiev <melentievm@gmail.com>2013-10-22 18:14:50 +0400
committerMax Melentiev <melentievm@gmail.com>2013-10-22 18:17:05 +0400
commitfde7344542a76e7f1f95a2265ac5480a1ef9aeed (patch)
tree6a15a5fd7f393ca112dfc8ec1549641623dcdbac
parent40a8130bc5f00f0d77b152e75484f175d70cabdd (diff)
downloadrails-fde7344542a76e7f1f95a2265ac5480a1ef9aeed.tar.gz
rails-fde7344542a76e7f1f95a2265ac5480a1ef9aeed.tar.bz2
rails-fde7344542a76e7f1f95a2265ac5480a1ef9aeed.zip
ActionController#translate also lookups shortcut without action name
-rw-r--r--actionpack/CHANGELOG.md3
-rw-r--r--actionpack/lib/abstract_controller/translation.rb6
-rw-r--r--actionpack/test/abstract/translation_test.rb8
3 files changed, 13 insertions, 4 deletions
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