1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

ActionController#translate also lookups shortcut without action name

This commit is contained in:
Max Melentiev 2013-10-22 18:14:50 +04:00
parent 40a8130bc5
commit fde7344542
3 changed files with 13 additions and 4 deletions

View file

@ -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*

View file

@ -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

View file

@ -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