Inflect word 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

On Oct 27, 2010, at 8:35 AM, Vitaliy Y. wrote:

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 B.
[email protected]main.invalid http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Oct 27, 2010, at 9:19 AM, Michael P. wrote:

“#{n} fish”

You can also use String.pluralize
“#{n} {n == 1 ? ‘month’ : ‘month’.pluralize}”

http://apidock.com/rails/ActiveSupport/Inflector/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 B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On 27 October 2010 14:07, Rob B. [email protected]
wrote:

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”

You can also use String.pluralize
“#{n} {n == 1 ? ‘month’ : ‘month’.pluralize}”

http://apidock.com/rails/ActiveSupport/Inflector/pluralize

On Oct 27, 10:40am, Marnen Laibow-Koser [email protected] wrote:

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)

locale/en.yml

en:
month_count:
one: ‘1 month’
other: ‘%{count} months’

Tim S. wrote in post #957518:

On Oct 27, 10:40am, Marnen Laibow-Koser [email protected] wrote:

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Rob B. wrote in post #957481:

On Oct 27, 2010, at 8:35 AM, Vitaliy Y. wrote:

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’)
[…]

…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).

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs