52

I am building a blog app. I'd like to be able to pluralize the word "article" if more than one "post" is "published."

Like so: Available Articles or Available Article

This is what I have....

 Available <%=  pluralize @posts.published, "Article" %>:

I've tried

 Available <%=  pluralize @posts.published.count, "Article" %>:

and that works...but I don't want the number. It shouldn't read Available 5 Articles....it should have no number.

Raj
  • 22,346
  • 14
  • 99
  • 142
NothingToSeeHere
  • 2,253
  • 5
  • 25
  • 57

6 Answers6

93

I have been looking for the answer to this myself and wasn't satisfied with any of the existing ones. Here's the tidiest solution I found:

 Available <%=  "Article".pluralize(@posts.published.count) %>:

Documentation is here. Relevant bits:

Returns the plural form of the word in the string.

If the optional parameter count is specified,
the singular form will be returned if count == 1.
For any other value of count the plural will be returned.

  'post'.pluralize             # => "posts"
  'apple'.pluralize(1)         # => "apple"
  'apple'.pluralize(2)         # => "apples"
jessewmc
  • 1,084
  • 8
  • 8
13

You could use Rails Internationalization (I18n) to accomplish this. In your config/data/en.yml your translations would be something like this:

en:
  available_articles:
    zero: Available Article
    one: Available Article
    other: Available Articles

And in your view you should be able to get the translation like this:

<%= t(:available_articles, count: @posts.published.count) %> 
Jan Klimo
  • 4,643
  • 2
  • 36
  • 42
Jakob W
  • 3,347
  • 19
  • 27
  • Is there a benefit to doing this over Hetal Khunti's answer? I'd like to accept your answer, because I wanted an alternative to Hetal's...but if the alternative is less clean I'd like to know ( – NothingToSeeHere Dec 03 '14 at 15:13
  • 1
    Well, I'd say that this approach leads to a much more readable view, but the project I'm working on has everything localized so this is what I'm used to see. Another benefit of this way is that if you want to change the wording when the count is zero, that is fairly simple. – Jakob W Dec 03 '14 at 15:19
  • Great Jakob.. New thing to learn.. 1+ for this other solution `Rails Internationalization (I18n)` – Hetal Khunti Dec 04 '14 at 04:37
  • I have this in my view `<%= I18n.t('items', count: @size) %> ` and `en: items: zero: no items one: one item other: %{count} items` in my en.yml. I get an error **found character that cannot start any token while scanning for the next token at line 26 column 12>** – Suraj Jul 15 '15 at 13:11
  • 1
    @Suraj You can't start the translation with a '%' character, so just wrap it in quotes, i.e. `other: "%{count} items"` – Jaco Pretorius Feb 19 '16 at 14:38
  • Yeah, did it that way. Thanks – Suraj Feb 19 '16 at 14:40
1

Yes, I did that way I liked so much:

- if @post.comments.persisted.any?
    h4
      = t(:available_comments, count: @post.comments.count)
    = render @post.comments.persisted
  - else
    p
      | There are no comments for this post.
en:
  available_comments:
    one: "%{count} Comment"
    other: "%{count} Comments"

Thank's @Jakob W!

rld
  • 2,603
  • 2
  • 25
  • 39
0

You can use <%= @posts.published.count > 0 ? "Available Article".pluralize(@posts.published.count) : nil %>:

jljohnstone
  • 1,202
  • 1
  • 10
  • 14
  • That pluralizes regardless of whether or not the count is plural. – NothingToSeeHere Dec 02 '14 at 06:19
  • It's working for me. Verify that `@posts.published.count` gives you the number you expect. – jljohnstone Dec 02 '14 at 06:31
  • It works fine for a count of "1" or "many"...but if the count is "0" it pluralizes. – NothingToSeeHere Dec 02 '14 at 06:34
  • See my updates. I thought you did not want the count to be displayed. This assumes that if the count is 0 or less then nothing should be displayed. – jljohnstone Dec 02 '14 at 06:46
  • @NothingToSeeHere What do you want to display if the count is 0? – Jakob W Dec 02 '14 at 10:03
  • Ah....I see why this does this! I'd like it to say "Available Article" when the count is zero. I see jljohnstone was trying to help me with hiding it if it was zero...but that's not what I need. I should have mentioned that, as showing a singular word with a count of zero is a bit unusual. @JakobW – NothingToSeeHere Dec 02 '14 at 14:13
  • @NothingToSeeHere I posted an answer, please let me know if it doesn't work – Jakob W Dec 02 '14 at 14:36
0

I found this pluralize has one bug

"user".pluralize(1) => "user"
"user".pluralize(2) => "users"

but

"user".pluralize(0) => "users"
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-1

How about this simple logic? I guess you want to display the number of Article as well if not then simply remove <%= @posts.published.count %>

Available <%= @posts.published.count %> 
    <% if @posts.published.count > 1 %>
        Articles
    <% else %>
        Article
    <% end %>

OR

you can use ternary operator,

Available <%= @posts.published.count %> <%= if (@posts.published.count > 1) ? "Articles" : "Article" %>

Output:

=> Available 1 Article   # if there is only one article 
=> Available 2 Articles   # if there is more then 1 articles 
Hetal Khunti
  • 787
  • 1
  • 9
  • 23
  • Yes, I was using this...but I was wondering if there was a more elegant way to write it using rails helpers...etc. Thanks. If I don't that way, I'll accept your answer. – NothingToSeeHere Dec 02 '14 at 06:37
  • @NothingToSeeHere : Yes you can use ternary operator `?:` also for single line condition. `condition? true part : false part` – Hetal Khunti Dec 02 '14 at 06:56
  • This needs to be adjusted to account for the fact that English uses the plural form of nouns for zero number "Available 0 Articles". Easy enough to fix. `<% if count == 1 %>Article<% else %>Articles<% end %>` – Toby 1 Kenobi Sep 08 '16 at 04:05