Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 957
pzin Avatar asked Oct 08 '12 18:10

pzin


2 Answers

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]
like image 97
mrinck Avatar answered Sep 26 '22 20:09

mrinck


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

like image 31
iouri Avatar answered Sep 24 '22 20:09

iouri