0
votes

So i'm using I18n Indonesia localize.

I have a id.yml on config/locales.

I want store value of created_at (datetime) to code (string) and Month translation to my locale before save..

self.code = account.created_at.strftime("%d%B%Y").to_s

I'm trying to see that the translation works in Rails Console.

So I tried:

irb(main):003:0> account = Account.last.created_at
   ←[1m←[36mAccount Load (35.0ms)←[0m  ←[1mSELECT "public"."accounts".* FROM "pub
lic"."accounts" ORDER BY "public"."accounts"."id" DESC LIMIT 1←[0m
=> Sat, 16 Mar 2013 10:07:37 UTC +00:00
irb(main):004:0> account.strftime("%d%B%Y").to_s
=> "16March2013"
irb(main):007:0> I18n.default_locale
=> :id
irb(main):008:0> I18n.t account.strftime("%d%B%Y").to_s
=> "translation missing: id.16March2013"
irb(main):006:0> I18n.l account.strftime("%d%B%Y").to_s
#<Class:0x6fc52c8>: Object must be a Date, DateTime or Time object. "16March2013
" given.

How can I localize month value?

Thank's for your help!

1
store all of your date times in the database as utc - then display them using the I18n.localize to format and offset to local timezone - i.e. I18n.l(account.created_at, format: :short) you can add your own custom formats besides long and short - see also guides.rubyonrails.org/i18n.html#adding-date-time-formats and see also this id.yml locales file - github.com/svenfuchs/rails-i18n/blob/master/rails/locale/id.yml - house9
i will not display that.. but I18n localize Activerecord Model value, I want store value of created_at (datetime) to code (string) and Month translation to my locale on before_save.. - rails_id

1 Answers

4
votes

According to the rails guide date should not be localized with the #translate method (I18n.t) but with the #localize method (I18n.l).

I also recommend you to use the rails-i18n gem which contain common translations such as month names for instance.

Let me know if you still can't manage to find a solution.