Skip to content

Fix pluralization of uncountables when given a locale #29097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

* Fix Inflector#apply_inflections to use a locale for uncountables

*Eilis Hamilton*

* Fix implicit coercion calculations with scalars and durations

Previously calculations where the scalar is first would be converted to a duration
Expand Down
15 changes: 9 additions & 6 deletions activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Inflector
# pluralize('CamelOctopus') # => "CamelOctopi"
# pluralize('ley', :es) # => "leyes"
def pluralize(word, locale = :en)
apply_inflections(word, inflections(locale).plurals)
apply_inflections(word, inflections(locale).plurals, locale)
end

# The reverse of #pluralize, returns the singular form of a word in a
Expand All @@ -45,7 +45,7 @@ def pluralize(word, locale = :en)
# singularize('CamelOctopi') # => "CamelOctopus"
# singularize('leyes', :es) # => "ley"
def singularize(word, locale = :en)
apply_inflections(word, inflections(locale).singulars)
apply_inflections(word, inflections(locale).singulars, locale)
end

# Converts strings to UpperCamelCase.
Expand Down Expand Up @@ -387,12 +387,15 @@ def const_regexp(camel_cased_word)

# Applies inflection rules for +singularize+ and +pluralize+.
#
# apply_inflections('post', inflections.plurals) # => "posts"
# apply_inflections('posts', inflections.singulars) # => "post"
def apply_inflections(word, rules)
# If passed an optional +locale+ parameter, the uncountables will be
# found for that locale.
#
# apply_inflections('post', inflections.plurals, :en) # => "posts"
# apply_inflections('posts', inflections.singulars, :en) # => "post"
def apply_inflections(word, rules, locale = :en)
result = word.to_s.dup

if word.empty? || inflections.uncountables.uncountable?(result)
if word.empty? || inflections(locale).uncountables.uncountable?(result)
result
else
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/inflector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def test_inflector_locality
inflect.singular(/es$/, "")

inflect.irregular("el", "los")

inflect.uncountable("agua")
end

assert_equal("hijos", "hijo".pluralize(:es))
Expand All @@ -432,12 +434,17 @@ def test_inflector_locality
assert_equal("los", "el".pluralize(:es))
assert_equal("els", "el".pluralize)

assert_equal("agua", "agua".pluralize(:es))
assert_equal("aguas", "agua".pluralize)

ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear }

assert ActiveSupport::Inflector.inflections(:es).plurals.empty?
assert ActiveSupport::Inflector.inflections(:es).singulars.empty?
assert ActiveSupport::Inflector.inflections(:es).uncountables.empty?
assert !ActiveSupport::Inflector.inflections.plurals.empty?
assert !ActiveSupport::Inflector.inflections.singulars.empty?
assert !ActiveSupport::Inflector.inflections.uncountables.empty?
end

def test_clear_all
Expand Down