mirror of https://github.com/rails/rails
Merge pull request #19102 from ulissesalmeida/fix-regression-default-translation
Fix regression when passing a value different of String.
This commit is contained in:
commit
85c49f5a64
|
@ -1,3 +1,8 @@
|
|||
* Fixed the translation helper method to accept different default values types
|
||||
besides String.
|
||||
|
||||
*Ulisses Almeida*
|
||||
|
||||
* Collection rendering automatically caches and fetches multiple partials.
|
||||
|
||||
Collections rendered as:
|
||||
|
|
|
@ -37,8 +37,12 @@ module ActionView
|
|||
# you know what kind of output to expect when you call translate in a template.
|
||||
def translate(key, options = {})
|
||||
options = options.dup
|
||||
has_default = options.has_key?(:default)
|
||||
remaining_defaults = Array(options.delete(:default))
|
||||
options[:default] = remaining_defaults.shift if remaining_defaults.first.kind_of? String
|
||||
|
||||
if has_default && !remaining_defaults.first.kind_of?(Symbol)
|
||||
options[:default] = remaining_defaults.shift
|
||||
end
|
||||
|
||||
# If the user has explicitly decided to NOT raise errors, pass that option to I18n.
|
||||
# Otherwise, tell I18n to raise an exception, which we rescue further in this method.
|
||||
|
|
|
@ -180,6 +180,11 @@ class TranslationHelperTest < ActiveSupport::TestCase
|
|||
assert_equal 'A Generic String', translation
|
||||
end
|
||||
|
||||
def test_translate_with_object_default
|
||||
translation = translate(:'translations.missing', default: 123)
|
||||
assert_equal 123, translation
|
||||
end
|
||||
|
||||
def test_translate_with_array_of_string_defaults
|
||||
translation = translate(:'translations.missing', default: ['A Generic String', 'Second generic string'])
|
||||
assert_equal 'A Generic String', translation
|
||||
|
|
Loading…
Reference in New Issue