Localized Month Name from Integer

I have an integer. I want to convert that into the name of a month.
With Ruby, I can just do this:

and get "August".

So, how do I localize the month name? When I try:

  l("date.month_names[8]")

I get an error:

  I18n::ArgumentError: Object must be a Date, DateTime or Time object.
"date.month_names[8]" given.

What am I missing?

partydrone wrote in post #968169:

I have an integer. I want to convert that into the name of a month.
With Ruby, I can just do this:

  Date::MONTHNAMES[8]

and get "August".

So, how do I localize the month name? When I try:

  l("date.month_names[8]")

I get an error:

  I18n::ArgumentError: Object must be a Date, DateTime or Time object.
"date.month_names[8]" given.

What am I missing?

"date.month_names[8]" is a string literal. That's probably not what you
wanted. :slight_smile:

Best,

l(:month_names[month], :scope => [:date]) doesn't work, either.

So, I ask again: how do I convert an integer into a localized month
name?

I thought there would be a simple way to do it with I18n. Not the
case?

OK. After much trial and error, here is the code that works:

t("date.month_names")[8]

Basically, call the I18n.translate method passing the date.month_names
key which return an array of month names, then access the index for
the desired month. (The first slot in the array is nil so indexes
match names correctly).

partydrone wrote in post #968419:

OK. After much trial and error, here is the code that works:

t("date.month_names")[8]

Basically, call the I18n.translate method passing the date.month_names
key which return an array of month names, then access the index for
the desired month. (The first slot in the array is nil so indexes
match names correctly).

Interesting. Note that with Gettext-type I18n (e.g. with fast_gettext),
this would be much simpler, just _(Date::MONTHNAMES[8]), since Gettext
uses the translatable string itself as a key rather than Rails' weird
symbolic keys. In general, I highly recommend using Gettext rather than
Rails' default backend.

Best,

Interesting. Note that with Gettext-type I18n (e.g. with fast_gettext),
this would be much simpler, just _(Date::MONTHNAMES[8]), since Gettext
uses the translatable string itself as a key rather than Rails' weird
symbolic keys. In general, I highly recommend using Gettext rather than
Rails' default backend.

I had translatable strings as keys (a long time ago in a completely
different software environment) and it ended up causing quite a
hassle, when one word (or phrase) in the english text needed to be
mapped to two (or more) different words for some languages

Fred

Frederick Cheung wrote in post #968523:

Interesting. Note that with Gettext-type I18n (e.g. with fast_gettext),
this would be much simpler, just _(Date::MONTHNAMES[8]), since Gettext
uses the translatable string itself as a key rather than Rails' weird
symbolic keys. In general, I highly recommend using Gettext rather than
Rails' default backend.

I had translatable strings as keys (a long time ago in a completely
different software environment) and it ended up causing quite a
hassle, when one word (or phrase) in the english text needed to be
mapped to two (or more) different words for some languages

Gettext has ways to disambiguate. Anyway, if you structure your
translatable strings properly (e.g. by using entire phrases as units),
this shouldn't be an issue. Rails' symbolic keys are IMHO a mistake.

Fred

Best,