5

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

pzin
  • 4,200
  • 2
  • 28
  • 49

3 Answers3

11

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]
mrinck
  • 126
  • 1
  • 5
5

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

iouri
  • 2,919
  • 1
  • 14
  • 11
  • Thank you **iouri**, but that isn't working for a spanish output. Was looking how to implement it with I18N l. – pzin Oct 08 '12 at 19:29
  • 4
    Just found out a way. If I do this `l(a.created_at, :format => '%b')` it works nice! Anyway, seeing that this was more luck than knowledge, if anyone could confirm that this is the best way to do it...? – pzin Oct 08 '12 at 19:33
  • @pzin Thanks a lot!! This is just what I was looking for!! – Heldraug Oct 09 '15 at 01:33
  • @pzin It does appear that `%b` works, but not `%^b` to capitalize. – theblang Jan 12 '16 at 21:59
0

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

tigrish
  • 2,488
  • 18
  • 21
  • I figure that out in the time I was trying and did it again now seeing your answer. But, as before, I still get the error `translation missing: es.time.formats.abbr_month`. I was looking in my YML file for indentation errors, it's all fine; two spaces always —even compared it with other language files on githun to get sure. The weird thing is I created just a simple es.yml, just with es->time->formats->abbr_month and date formats like :short, :long even others were working! Then I deleted the whole YML file, reload rails server and the spanish output was still working —:short, :default, etc... – pzin Oct 09 '12 at 04:21
  • [...] so it's really a crazy thing. I got it the work with :format => %b, but I think making it so all the time break a bit the DRY logic of my app. – pzin Oct 09 '12 at 04:23