mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #11790 from printercu/patch-3
ActionController#translate supports symbols
This commit is contained in:
commit
76f6524538
3 changed files with 39 additions and 14 deletions
|
@ -1,3 +1,8 @@
|
|||
* ActionController#translate supports symbols as shortcuts.
|
||||
When shortcut is given it also lookups without action name.
|
||||
|
||||
*Max Melentiev*
|
||||
|
||||
* Expand `ActionController::ConditionalGet#fresh_when` and `stale?` to also
|
||||
accept a collection of records as the first argument, so that the
|
||||
following code can be written in a shorter form.
|
||||
|
|
|
@ -8,14 +8,15 @@ module AbstractController
|
|||
# <tt>I18n.translate("people.index.foo")</tt>. 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] == '.')
|
||||
key = "#{ controller_path.tr('/', '.') }.#{ action_name }#{ key }"
|
||||
args[0] = key
|
||||
def translate(key, options = {})
|
||||
if key.to_s.first == '.'
|
||||
path = controller_path.tr('/', '.')
|
||||
defaults = [:"#{path}#{key}"]
|
||||
defaults << options[:default] if options[:default]
|
||||
options[:default] = defaults
|
||||
key = "#{path}.#{action_name}#{key}"
|
||||
end
|
||||
|
||||
I18n.translate(*args)
|
||||
I18n.translate(key, options)
|
||||
end
|
||||
alias :t :translate
|
||||
|
||||
|
|
|
@ -9,6 +9,22 @@ 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',
|
||||
},
|
||||
no_action: 'no_action_tr',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
@controller.stubs(action_name: :index)
|
||||
end
|
||||
|
||||
def test_action_controller_base_responds_to_translate
|
||||
|
@ -28,16 +44,19 @@ 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
|
||||
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
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue