Change default date formatter in active admin - ruby-on-rails

In ActiveAdmin dates are printed in the, supposedly American, format %B %d, %Y %H:%M. E.g. March 19, 2013 13:25
However, my "default" frontend prints this using the default Rails (ISO) format, 2013-03-07 14:12:31 UTC, as seen when dropping a <%= Date.new %> anywhere in a view.
What is defining the format for the ActiveAdmin dates?
Where or how can this be changed, if possible, simply following a global Rails wide i18n setting.

please, ensure you have next lines in your config/locales/en.yml
en:
date:
formats:
long: "%Y-%m-%d"
time:
formats:
long: "%Y-%m-%d %H:%M:%S"
Also if you want to change Filter default date formats
Try this Active Admin date filter date format customisation

What is defining the format for the ActiveAdmin dates?
From the Localize Format For Dates and Times section of the configuration docs:
Active Admin sets :long as default localize format for dates and times.
Source: General Configuration - Localize Format For Dates and Times.
Where or how can this be changed, if possible, simply following a global Rails wide i18n setting.
From ActiveAdmin v1.0.0.pre2 you can override the default localize_format configuration to use a different format:
ActiveAdmin.setup do |config|
config.localize_format = :short
end
Alternatively you can update the :long format in your locales configuration as suggested by Fivell:
en:
date:
formats:
long: "%Y-%m-%d"
time:
formats:
long: "%Y-%m-%d %H:%M:%S"

Related

Displaying the date pattern string in Rails i18n localization

I'd like to get the template string of the localized date format in Rails. What I'm going for:
date_in_us = get_date_string(:en) # 'mm/dd/yyyy'
date_in_gb = get_date_string(:en-gb) # 'dd/mm/yyyy'
So to be clear I'm not trying to localize a real date, I'm trying to get the date format string so I could display it as a placeholder in a text field.
Everything I've searched for on the internet keeps bringing me back to actually localizing a date. :-/
That won't work because that's not how the format is specified. For English, this is how the date formats are specified:
formats:
default: "%Y-%m-%d"
long: "%B %d, %Y"
short: "%b %d"
Here are the docs for these percent placeholders in case you're curious.
To solve your problem, I'd create a date, localize it, and replace the parts:
date = Date.new(2000, 12, 31)
I18n.l(date).sub('2000', 'yyyy').sub('12', 'mm').sub('31', 'dd')
# => "yyyy-mm-dd"
Note that this might not work if the locale uses a 2 digit year format. Let's try it for some locales (using the default from rails-i18n):
def get_date_string(locale = I18n.current)
date = Date.new(2000, 12, 31)
I18n.l(date, locale: locale)
.sub('2000', 'yyyy')
.sub('12', 'mm')
.sub('31', 'dd')
end
formats = %i[en en-US en-GB es de fr pt].map do |locale|
[locale, get_date_string(locale)]
end.to_h
formats will be:
{
:en=>"yyyy-mm-dd",
:"en-US"=>"mm-dd-yyyy",
:"en-GB"=>"dd-mm-yyyy",
:es=>"dd/mm/yyyy",
:de=>"dd.mm.yyyy",
:fr=>"dd/mm/yyyy",
:pt=>"dd/mm/yyyy"
}
By default all translations should be placed inside the config/locales directory, divided into files.
Below is a sample en.yml with date patterns.
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
long: "%B %d, %Y"
So, all of the following equivalent lookups will return the :short date format "%b %d":
I18n.t 'date.formats.short'
I18n.t 'formats.short', scope: :date
I18n.t :short, scope: 'date.formats'
I18n.t :short, scope: [:date, :formats]
Please check this guide on how to i18n localization works in Ruby on Rails

My locale is set, I can see € instead of $, but time still displays English months

I am doing a Rails app in French. I downloaded a fr.yml stored in config/locales and added config.i18n.default_locale = :fr to my application.rb. The .yml contains days and months.
Messages in pages generated by scaffold are in French, € is displayed instead of $, but it keep using English names for months with strftime (a Time function).
Why?
<%= I18n.localize(Time.zone.now, :format => :short) %>
You can add more formats to your fr.yml
date:
formats:
default: "%d.%m.%Y"
numeric: "%d.%m.%Y"
short: "%e %b"
long: "%e %B %Y, %A"
only_day: "%e"
The strftime function isn't hooked into Rails - it's just a member of the Ruby Time class. Try using Rails localization instead - that should do what you're expecting.
Hope that helps!

Formatting Date Output Hash

The output from my hash is giving me the following date time:
16 May 11 13:12:14 +0000
How do I go about formatting this into something sensible?
Thanks
I'll assume you are wanting this output in a view... First, you can create some formats:
# /config/locales/en.yml:
en:
date:
formats:
full: "%b %d, %Y"
time:
formats:
full: "%B %d, %Y at %I:%M%p"
Then you can display a datetime in a view using the l method with specified format:
<%= l #something.updated_at, :format => :full %>
This would display something like the following:
May 16, 2011 at 01:12pm
More on the i18n formats
List of available directives

AM/PM not uppercase when using I18n.l with %p in Rails

I'm having an issue which I can't seem to figure out. I'm trying to format a date using a custom format I've defined in my en.yml file:
en:
hello: "Hello world"
time:
formats:
history_table: "%m/%d/%Y %I:%M:%S %p %Z"
This is being called using the 'l' helper:
l version.created_at, :format => :history_table
For some reason this is displaying the AM/PM in lowercase, instead of in uppercase as should be the case with %p.
I've played around in the console a bit, and it seems like it's a difference in behavior between the localization function and strftime:
ruby-1.9.2-p180 :043 > I18n.l user.updated_at, :format => "%m/%d/%Y %I:%M:%S %p %Z"
=> "03/23/2011 01:52:16 am UTC"
ruby-1.9.2-p180 :044 > user.updated_at.strftime("%m/%d/%Y %I:%M:%S %p %Z")
=> "03/23/2011 01:52:16 AM UTC"
Am I doing something wrong? Is this a bug? Any guidance is greatly appreciated as my forehead is sore from banging it against the wall.
Edit:
This has been solved(ish).
Looking at the default activesupport localization, there isn't any differentiation between %p and %P.
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml
I over-rode the localization in my local en.yml file to use uppercase. I would really have liked to see Rails support both options however.
Looking in the source for active support, I found the following under english localization:
time:
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
short: "%d %b %H:%M"
long: "%B %d, %Y %H:%M"
am: "am"
pm: "pm"
In other words, there is no distinction between %P and %p built-in to localization as there is in strftime. This unfortunately means that in individual custom formats it is not possible to choose between upper and lower case representations, but it is possible to define which you would like globally by over-riding the default formats in your own en.yml file. For example, here's my updated en.yml file that now causes the output of upper-case AM/PM.
en:
hello: "Hello world"
time:
formats:
history_table: "%m/%d/%Y %I:%M:%S %p %Z"
am: "AM"
pm: "PM"

Rails: How to make Date strftime aware of the default locale?

I have my default locale set in the environment.rb as de (German).
I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following:
some_date.strftime('%B, %y')
It prints in English (January, 11), and not the expected German (Januar, 11).
How can I print the date according to the default locale?
Use the l (alias for localize) method instead of raw strftime, like this:
l(date, format: '%B %d, in the year %Y')
See here for more information.
You can also define 'named' formats, a couple of them (short, long) are already predefined.
you can also make it shorter:
l(some_date, :format => '%d %B %Y')
In es.yml put:
es:
date:
formats:
default: "%d / %m / %Y"
In index.html.erb put:
<%= l somemodel.datefield %>

Resources