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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. :) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Dec 13, 4:08 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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. :) > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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). On Dec 14, 2:24 pm, partydrone <partydr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? > > On Dec 13, 4:08 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > 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. :) > > > Best, > > -- > > Marnen Laibow-Koserhttp://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > > -- > > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 14, 10:11 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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> > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #968523:> On Dec 14, 10:11pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> 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 languagesGettext 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.> > FredBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.