Strange I18n date output with rails

前端 未结 3 756
长情又很酷
长情又很酷 2020-12-20 15:10

I\'ve got a strange problem with date translations in my Ruby On Rails 3 application, and I really don\'t understand why...

Here are my en.yml and

相关标签:
3条回答
  • 2020-12-20 15:26

    Type in your console

    I18n.t(:"date") 
    

    to check if you are getting the translations you defined in your translation .yml file.

    Compare the structure with the standard EN locale

    I18n.t(:"date", locale:'en')
    

    That made me notice I was declaring the date: attribute twice in my .yml, and the first part was being overwritten by the second declaration.

    You should get the abbr_month_names that you declared when calling

    I18n.t(:"date.abbr_month_names")
    

    These are the ones that will be used when calling %b.

    If not, check your locale .yml file to make sure they are properly declared, and not being declared twice.

    You may also call I18n.locale to check if the .yml file you are editing is the one being used by rails

    0 讨论(0)
  • 2020-12-20 15:30

    I think I've finally figured this out, sorry for taking so long.

    The Rails l helper just calls I18n.localize. If you trace through the I18n.localize code, you'll end up here:

    format = format.to_s.gsub(/%[aAbBp]/) do |match|
      case match
      when '%a' then I18n.t(:"date.abbr_day_names",                  :locale => locale, :format => format)[object.wday]
      when '%A' then I18n.t(:"date.day_names",                       :locale => locale, :format => format)[object.wday]
      when '%b' then I18n.t(:"date.abbr_month_names",                :locale => locale, :format => format)[object.mon]
      when '%B' then I18n.t(:"date.month_names",                     :locale => locale, :format => format)[object.mon]
      when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
      end
    end
    

    So the localize helper doesn't use strftime for the "stringy" parts of the date/time, it tries to do it by itself. Add the translations (as arrays in your YAML) for the month and day names as above and your localized dates and times should start working.

    If you don't have those translation arrays in your YAML, then I18n.t(:"date.abbr_month_names") will give you strings like this:

    "translation missing: en.date.abbr_month_names"
    

    and then I18n.localize will end up doing silly things like this:

    "translation missing: en.date.abbr_month_names"[10]
    

    That will use String#[] instead of the expected Array#[] and you end up with random looking single character month and day names.

    0 讨论(0)
  • 2020-12-20 15:34

    Where do these wrong "formats" come from ?

    Because created_at is a DateTime, rails using time formats (not date).

    https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml#L195

    time:
      am: am
      formats:
        default: ! '%a, %d %b %Y %H:%M:%S %z'
    
    0 讨论(0)
提交回复
热议问题