mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #29097 from EilisHamilton/fix_uncountable_pluralization_locale
Fix pluralization of uncountables when given a locale
This commit is contained in:
commit
352865d0f8
3 changed files with 20 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* `#singularize` and `#pluralize` now respect uncountables for the specified locale.
|
||||||
|
|
||||||
|
*Eilis Hamilton*
|
||||||
|
|
||||||
* Add ActiveSupport::CurrentAttributes to provide a thread-isolated attributes singleton.
|
* Add ActiveSupport::CurrentAttributes to provide a thread-isolated attributes singleton.
|
||||||
Primary use case is keeping all the per-request attributes easily available to the whole system.
|
Primary use case is keeping all the per-request attributes easily available to the whole system.
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ module ActiveSupport
|
||||||
# pluralize('CamelOctopus') # => "CamelOctopi"
|
# pluralize('CamelOctopus') # => "CamelOctopi"
|
||||||
# pluralize('ley', :es) # => "leyes"
|
# pluralize('ley', :es) # => "leyes"
|
||||||
def pluralize(word, locale = :en)
|
def pluralize(word, locale = :en)
|
||||||
apply_inflections(word, inflections(locale).plurals)
|
apply_inflections(word, inflections(locale).plurals, locale)
|
||||||
end
|
end
|
||||||
|
|
||||||
# The reverse of #pluralize, returns the singular form of a word in a
|
# The reverse of #pluralize, returns the singular form of a word in a
|
||||||
|
@ -45,7 +45,7 @@ module ActiveSupport
|
||||||
# singularize('CamelOctopi') # => "CamelOctopus"
|
# singularize('CamelOctopi') # => "CamelOctopus"
|
||||||
# singularize('leyes', :es) # => "ley"
|
# singularize('leyes', :es) # => "ley"
|
||||||
def singularize(word, locale = :en)
|
def singularize(word, locale = :en)
|
||||||
apply_inflections(word, inflections(locale).singulars)
|
apply_inflections(word, inflections(locale).singulars, locale)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts strings to UpperCamelCase.
|
# Converts strings to UpperCamelCase.
|
||||||
|
@ -387,12 +387,15 @@ module ActiveSupport
|
||||||
|
|
||||||
# Applies inflection rules for +singularize+ and +pluralize+.
|
# Applies inflection rules for +singularize+ and +pluralize+.
|
||||||
#
|
#
|
||||||
# apply_inflections('post', inflections.plurals) # => "posts"
|
# If passed an optional +locale+ parameter, the uncountables will be
|
||||||
# apply_inflections('posts', inflections.singulars) # => "post"
|
# found for that locale.
|
||||||
def apply_inflections(word, rules)
|
#
|
||||||
|
# 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
|
result = word.to_s.dup
|
||||||
|
|
||||||
if word.empty? || inflections.uncountables.uncountable?(result)
|
if word.empty? || inflections(locale).uncountables.uncountable?(result)
|
||||||
result
|
result
|
||||||
else
|
else
|
||||||
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
||||||
|
|
|
@ -420,6 +420,8 @@ class InflectorTest < ActiveSupport::TestCase
|
||||||
inflect.singular(/es$/, "")
|
inflect.singular(/es$/, "")
|
||||||
|
|
||||||
inflect.irregular("el", "los")
|
inflect.irregular("el", "los")
|
||||||
|
|
||||||
|
inflect.uncountable("agua")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal("hijos", "hijo".pluralize(:es))
|
assert_equal("hijos", "hijo".pluralize(:es))
|
||||||
|
@ -432,12 +434,17 @@ class InflectorTest < ActiveSupport::TestCase
|
||||||
assert_equal("los", "el".pluralize(:es))
|
assert_equal("los", "el".pluralize(:es))
|
||||||
assert_equal("els", "el".pluralize)
|
assert_equal("els", "el".pluralize)
|
||||||
|
|
||||||
|
assert_equal("agua", "agua".pluralize(:es))
|
||||||
|
assert_equal("aguas", "agua".pluralize)
|
||||||
|
|
||||||
ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear }
|
ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear }
|
||||||
|
|
||||||
assert ActiveSupport::Inflector.inflections(:es).plurals.empty?
|
assert ActiveSupport::Inflector.inflections(:es).plurals.empty?
|
||||||
assert ActiveSupport::Inflector.inflections(:es).singulars.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.plurals.empty?
|
||||||
assert !ActiveSupport::Inflector.inflections.singulars.empty?
|
assert !ActiveSupport::Inflector.inflections.singulars.empty?
|
||||||
|
assert !ActiveSupport::Inflector.inflections.uncountables.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_clear_all
|
def test_clear_all
|
||||||
|
|
Loading…
Reference in a new issue