Search code examples
ruby-on-railsrubyrails-i18ntime-format

I18n localization time format for spanish translation


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 :)


Solution

  • 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