I18n localization time format for spanish translation

半城伤御伤魂 提交于 2019-12-08 11:29:54

问题


I am using I18n for localizing a rails application, and i came across a situation which involves time format using I18n.

the date and time format displaying is as follows: "Tuesday, July 11, 2017" for time : 2017-07-11 12:30:00 +0530

for localizing i am using en.yml and es.yml with both time formats as shown below:

 en:
  time:
   formats:
     long: '%A, %B %d, %Y'
     short: '%b %d, %Y'

and in model i am using I18n.l(raw_date.to_time, format: :long), this working fine for english with result "Tuesday, July 11, 2017", but for spanish its returning like this: "a, t 11, 2017"

what is the datetime format for spanish to get the same result as english. any help would be appreciated :)


回答1:


Looking at the rails-i18n spanish file, I've pulled a couple of their translations over and everything seems to be working as expected (I saw the same issue until I added these translations).

config/locales/es.yml

es:
  date:
    day_names:
      - domingo
      - lunes
      - martes
      - miércoles
      - jueves
      - viernes
      - sábado
    month_names:
      -
      - enero
      - febrero
      - marzo
      - abril
      - mayo
      - junio
      - julio
      - agosto
      - septiembre
      - octubre
      - noviembre
      - diciembre
  time:
    formats:
      long: '%A, %B %d, %Y'
      short: '%b %d, %Y'

and then in the console:

I18n.locale = :es
I18n.l(Date.today.to_time, format: :long) # => "viernes, julio 21, 2017"

I recommend taking a look at the rest of their file (or even pulling in the entire thing) if you're doing a lot of translations with rails built in stuff.

Notes:

  • I don't speak Spanish, so I just assume that their file is alright
  • There are a fair number of other date translations you may need if you are using a different format


来源:https://stackoverflow.com/questions/45235207/i18n-localization-time-format-for-spanish-translation

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