Customizing datetime format in en.yml in Rails 3

后端 未结 4 1782
北海茫月
北海茫月 2021-02-01 18:15

According to documentation at http://guides.rubyonrails.org/i18n.html#adding-date-time-formats, the best way to ask for specific formats for a date is to define them in /config/

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 18:52

    [several years later...]

    If you want to DRY up your date formats and/or you have legacy code with to_s all over the place and don't plan to internationalize your app, you can do this:

    I18n.t('date.formats').each do |key, value|
      Date::DATE_FORMATS[key] = value
    end
    

    Then just define your formats in config/locales/en.yml. Of course if you ever plan to translate your app, you'll need to change all occurrences of to_s to use the I18n.l method suggested below.

    You can also use this without Rails. Just add the following before the above code:

    require 'active_support/time'
    I18n.load_path << 'en.yml'
    

提交回复
热议问题