How can I use proper word after number, depending on that number. For example I have message that says:
for 23 monthes, or it can be 21 month. So I have to inflect depending on number
How can I use proper word after number, depending on that number. For example I have message that says:
for 23 monthes, or it can be 21 month. So I have to inflect depending on number
If you're in a Rails view, you can say:
pluralize(n, 'month')
If you're just in plain Ruby, it might depend on the pluralization rule:
"#{n} month#{'s' unless n == 1}"
"#{n} part#{n == 1 ? 'y' : 'ies'}"
"#{n} fish"
-Rob
Rob Biedenharn Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab@GaslightSoftware.com http://GaslightSoftware.com/
You can also use String.pluralize "#{n} {n == 1 ? 'month' : 'month'.pluralize}"
True, but my point was that you may not even need the help from ActiveSupport for something simple (i.e., you are dealing with a known rather than an arbitrary noun).
You can even do things like:
"#{n} #{n == 1 ? 'error prevents' : 'errors prevent'} the saving of the record."
which tends to get very little help from the Inflector.
-Rob
Rob Biedenharn Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab@GaslightSoftware.com http://GaslightSoftware.com/
Rob Biedenharn wrote in post #957481:
I believe the proper Rails way to do it is the following:
# view.html.erb
t(:month_count, :count => month_number)
# locale/en.yml
en: month_count: one: '1 month' other: '%{count} months'
Tim Shaffer wrote in post #957518:
> If you're in a Rails view, you can say:
> pluralize(n, 'month')
[...]
...but you probably shouldn't if you're going to internationalize the application; different languages have different pluralization rules. Any good I18N library should have a generalized pluralization function (for example, there's n_ in fast_gettext).
I believe the proper Rails way to do it is the following:
# view.html.erb
t(:month_count, :count => month_number)
Only if you're using Rails' dreadful I18N. I stay as far away from that as I can.
# locale/en.yml
en: month_count: one: '1 month' other: '%{count} months'
Best,