Reputation: 14038
I want Rails (3.2) to use the American date format 03/14/2012 unless I say otherwise.
I have the I18n gem installed, downloaded (and modified) the config/locales/en-US.yml
file to have the default format as default: ! '%m/%d/%Y'
, set my application.rb
default locale as config.i18n.default_locale = "en-US"
, and restarted.
When dates are displayed (e.g. a simple view) they still have the format 2012-03-14. If I use the I18n.l
method, the date displays as desired, 03/14/2012. So localization is working through the I18n class.
I guess I was expecting the meaning of "default" to be, "this is the one to use if you're not otherwise told to localize or translate." Apparently I expected wrong :-)
So further digging revealed I could change the defaults for date and time in an initializer, such as config/initializers/date_formats.rb
, e.g.
Date::DATE_FORMATS[:default]="%m/%d/%Y"
Time::DATE_FORMATS[:default]="%m/%d/%Y %H:%M"
This appears to do what I want. Several alarming posts suggests that this will screw up how dates are stored in the database, but my tests (using PostgreSQL) suggest that this is not a problem.
So (rant) why the heck shouldn't all apps observe the default locale without wrapping every date on the face of the earth with l
and t
helpers?
And (actual question) will I cause permanent harm to myself or others by changing the default date and time formats for my application in an initializer?
Upvotes: 4
Views: 1742
Reputation: 976
I had the same problem and this gem helped me https://github.com/jeremyevans/ruby-american_date
Just add it to your Gemfile, no settings needed
Upvotes: 2