Rails i18n.default_locale and date format ... don't seem to act as default

I want Rails (3.2) to use the American date format on 03/14/2012, unless I say otherwise.

I have I18n gem installed, the config/locales/en-US.yml file is uploaded (and modified) to have the default format as default: ! '%m/%d/%Y' config/locales/en-US.yml default: ! '%m/%d/%Y' , set my default locale application.rb as config.i18n.default_locale = "en-US" and restart.

When dates are displayed (for example, a simple view), they still have the format 2012-03-14. If I use the I18n.l method, the date is displayed as desired, 03/14/2012. Thus, localization works through class I18n.

I assume I was expecting a default value, "this is the one to use unless you are otherwise told to localize or translate." Apparently I expected that I was wrong :-)

So, further digging detection I can change the default values ​​for the date and time in the initializer, for example config/initializers/date_formats.rb , for example.

 Date::DATE_FORMATS[:default]="%m/%d/%Y" Time::DATE_FORMATS[:default]="%m/%d/%Y %H:%M" 

It looks like what I want. A few troubling messages suggest that this will depend on how the dates are stored in the database, but my tests (using PostgreSQL) suggest that this is not a problem.

So (rant), why the hell shouldn't all applications follow the standard locale without packing every date on the surface of the earth with the help of l and t helpers?

And (actual question) will I harm myself or other people by changing the default date and time formats for my application in the initializer?

+4
source share
1 answer

I had the same problem and this stone helped me https://github.com/jeremyevans/ruby-american_date

Just add it to your gemfile, no settings are required

+2
source

Source: https://habr.com/ru/post/1401222/


All Articles