Pluralizing a single word is simple:
pluralize(@total_users, "user")
But what if I want to print "There is/are N user/users":
There are 0 users
There is 1 user
There are 2 users
, i.e., how to pluralize a sentence?
Pluralizing a single word is simple:
pluralize(@total_users, "user")
But what if I want to print "There is/are N user/users":
There are 0 users
There is 1 user
There are 2 users
, i.e., how to pluralize a sentence?
I think the first part of Martin Gordon's answer is pretty good.
Alternatively, it's kind of messy but you can always just write the logic yourself:
You can add a custom inflection for it. By default, Rails will add an
inflections.rb
toconfig/initializers
. There you can add:You will then be able to use
pluralize(@total_users, "is")
to return is/are using the same rules as user/users.EDIT: You clarified the question on how to pluralize a sentence. This is much more difficult to do generically, but if you want to do it, you'll have to dive into NLP.
As the comment suggests, you could do something with I18n if you just want to do it with a few sentences, you could build something like this:
And in
config/locales/en.yml
:This is probably best covered by the Rails i18n pluralization features. Adapted from http://guides.rubyonrails.org/i18n.html#pluralization