Explanation how to use I18n.l @user.created_at, format: :default

社会主义新天地 提交于 2019-12-04 04:19:04

问题


When translating a date format with I18n.l or the helper ´l´ like this in the view:

l @user.created_at, format: :default

and in the corresponding sv.yml:

sv:
  time:
    formats:
      default: "%B"

then the output is a single letter m.

This problem and appearantly solution is documented here: Strange I18n date output with rails, but I still don't understand how to output the months in my locale.

The choosen answer explains "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."

Could someone please exemplify how such an array should be structured and look like?


回答1:


I added the following part to my YAML file:

en:
  datetime: &datetime
    month_names:
      [~, January, February, March, April, May, June, July, August, September, October, November, December]
    formats:
       default: "%d/%m/%Y"
       short: "%d/%m/%Y"
       day_month: "%d/%m"

  date:
    <<: *datetime

  time:
    <<: *datetime

The %B will give you the month name in full. If you want to use abbreviated month names, you can use %b, and define a similar array called abbr_month_names in your YAML file.



来源:https://stackoverflow.com/questions/20837767/explanation-how-to-use-i18n-l-user-created-at-format-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!