简体   繁体   中英

Use of abbr_month_names (and similars) with I18n.l()

I'm switching to rails and it becomes sometimes a bit strange. After installing a es.yml file I am trying to output a DATETIME column of my database. If I do this:

= l(a.created_at, :format => :short)

It's ok. It prints a nice date in spanish format. But I am confuse about using abbr_month_names in this way. I have tried in a lot of different ways —believe me, I am around four hours with this issue— but I coudn't find a way to output just the month name in short —lets say "OCT.

I had a look on much sites about i18n and date formats, but articles ends always with the :format => :short example and doesn't cares about other ways to display dates (well, someone also explain how to use the :long format...)

A snippet from the German de.yml locale:

de:
  date:
    abbr_month_names:
    - 
    - Jan
    - Feb
    - Mär

You can directly access the locales by Rails' translate function:

I18n.t :abbr_month_names, :scope => :date // returns ['', 'Jan', 'Feb', 'Mär']

Because abbr_month_names consists of multiple entries (starting with hyphens) it will return an array. For example you could get the current abbreviated month by:

(I18n.t :abbr_month_names, :scope => :date)[Time.now.month]

You can use a.created_at.strftime('%b') this link shows all possible datetime parts:

http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime

In your YAML file enter :

es:
  time:
    formats:
      abbr_month: "%b"

and then in your view you should be able to use this :

= l(a.created_at, :format => :abbr_month)

Taken from here : http://guides.rubyonrails.org/i18n.html#adding-date-time-formats

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM