mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20813 from noniq/locale-argument-for-pluralize-helper
Allow `pluralize` helper to take a locale.
This commit is contained in:
commit
87b4f3105f
2 changed files with 22 additions and 2 deletions
|
@ -206,6 +206,11 @@ module ActionView
|
|||
# +plural+ is supplied, it will use that when count is > 1, otherwise
|
||||
# it will use the Inflector to determine the plural form.
|
||||
#
|
||||
# If passed an optional +locale:+ parameter, the word will be pluralized
|
||||
# using rules defined for that language (you must define your own
|
||||
# inflection rules for languages other than English). See
|
||||
# ActiveSupport::Inflector.pluralize
|
||||
#
|
||||
# pluralize(1, 'person')
|
||||
# # => 1 person
|
||||
#
|
||||
|
@ -217,11 +222,14 @@ module ActionView
|
|||
#
|
||||
# pluralize(0, 'person')
|
||||
# # => 0 people
|
||||
def pluralize(count, singular, plural = nil)
|
||||
#
|
||||
# pluralize(2, 'Person', locale: :de)
|
||||
# # => 2 Personen
|
||||
def pluralize(count, singular, plural = nil, locale: nil)
|
||||
word = if (count == 1 || count =~ /^1(\.0+)?$/)
|
||||
singular
|
||||
else
|
||||
plural || singular.pluralize
|
||||
plural || singular.pluralize(locale)
|
||||
end
|
||||
|
||||
"#{count || 0} #{word}"
|
||||
|
|
|
@ -383,6 +383,18 @@ class TextHelperTest < ActionView::TestCase
|
|||
assert_equal("12 berries", pluralize(12, "berry"))
|
||||
end
|
||||
|
||||
def test_pluralization_with_locale
|
||||
ActiveSupport::Inflector.inflections(:de) do |inflect|
|
||||
inflect.plural(/(person)$/i, '\1en')
|
||||
inflect.singular(/(person)en$/i, '\1')
|
||||
end
|
||||
|
||||
assert_equal("2 People", pluralize(2, "Person", locale: :en))
|
||||
assert_equal("2 Personen", pluralize(2, "Person", locale: :de))
|
||||
|
||||
ActiveSupport::Inflector.inflections(:de).clear
|
||||
end
|
||||
|
||||
def test_cycle_class
|
||||
value = Cycle.new("one", 2, "3")
|
||||
assert_equal("one", value.to_s)
|
||||
|
|
Loading…
Reference in a new issue