Displaying the date pattern string in Rails i18n localization - ruby-on-rails

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

Related

How to format the default date for timeago_tag

I'm using the format below to display my time ago but need help understanding how to do the format for the default date format. Using rails-timeago gem:
=timeago_tag Time.zone.now, :nojs => true, :limit => 2.days.ago, lang: :en
print>> 2d
After that it does the yyyy-dd-mm format.
I would like to format the default date like May 22, 2014. There's a (:strftime,'%B %d, %Y') that I've used in other format to display how I wanted it but I'm not sure how the syntax works with the timeago_tag helper.
As we discussed in the comments, timeago_tag has a format option, so to get your date in the format "%B %d, %Y":
= timeago_tag Time.zone.now, nojs: true, limit: 2.days.ago, lang: :en, format: "%B %d, %Y"

rails localization of date format gives unexpected results [duplicate]

I've got a strange problem with date translations in my Ruby On Rails 3 application, and I really don't understand why...
Here are my en.yml and fr.yml :
fr:
date:
formats:
default: "%d/%m/%Y"
short: "%e %b"
long: "%e %B %Y"
time:
formats:
default: "%d %B %Y %H:%M:%S"
short: "%d %b %H:%M"
long: "%A %d %B %Y %H:%M"
am: 'am'
pm: 'pm'
en:
date:
formats:
default: "%Y-%m-%d"
long: "%B %d, %Y"
short: "%b %d"
time:
am: am
formats:
default: ! '%a, %d %b %Y %H:%M:%S %z'
long: ! '%B %d, %Y %H:%M'
short: ! '%d %b %H:%M'
pm: pm
This is not specific to a particuliar view, but for instance in one of my view :
<td><%=l job_application.created_at, :format => :default %></td>
I get those strange outputs :
With locale = :en
=> t, 30 o 2012 18:09:33 +0000
With locale = :fr
=> 30 o 2012 18:09:33
Where do these wrong "formats" come from ?
I'm using Rails 3.2.8 (with Postgresql / gem pg), and everything related to I18n works fine except for dates.
Thanks for any help !
I think I've finally figured this out, sorry for taking so long.
The Rails l helper just calls I18n.localize. If you trace through the I18n.localize code, you'll end up here:
format = format.to_s.gsub(/%[aAbBp]/) do |match|
case match
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
end
end
So the localize helper doesn't use strftime for the "stringy" parts of the date/time, it tries to do it by itself. Add the translations (as arrays in your YAML) for the month and day names as above and your localized dates and times should start working.
If you don't have those translation arrays in your YAML, then I18n.t(:"date.abbr_month_names") will give you strings like this:
"translation missing: en.date.abbr_month_names"
and then I18n.localize will end up doing silly things like this:
"translation missing: en.date.abbr_month_names"[10]
That will use String#[] instead of the expected Array#[] and you end up with random looking single character month and day names.
Where do these wrong "formats" come from ?
Because created_at is a DateTime, rails using time formats (not date).
https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml#L195
time:
am: am
formats:
default: ! '%a, %d %b %Y %H:%M:%S %z'
Type in your console
I18n.t(:"date")
to check if you are getting the translations you defined in your translation .yml file.
Compare the structure with the standard EN locale
I18n.t(:"date", locale:'en')
That made me notice I was declaring the date: attribute twice in my .yml, and the first part was being overwritten by the second declaration.
You should get the abbr_month_names that you declared when calling
I18n.t(:"date.abbr_month_names")
These are the ones that will be used when calling %b.
If not, check your locale .yml file to make sure they are properly declared, and not being declared twice.
You may also call I18n.locale to check if the .yml file you are editing is the one being used by rails

Change default date formatter in active admin

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"

Strange I18n date output with rails

I've got a strange problem with date translations in my Ruby On Rails 3 application, and I really don't understand why...
Here are my en.yml and fr.yml :
fr:
date:
formats:
default: "%d/%m/%Y"
short: "%e %b"
long: "%e %B %Y"
time:
formats:
default: "%d %B %Y %H:%M:%S"
short: "%d %b %H:%M"
long: "%A %d %B %Y %H:%M"
am: 'am'
pm: 'pm'
en:
date:
formats:
default: "%Y-%m-%d"
long: "%B %d, %Y"
short: "%b %d"
time:
am: am
formats:
default: ! '%a, %d %b %Y %H:%M:%S %z'
long: ! '%B %d, %Y %H:%M'
short: ! '%d %b %H:%M'
pm: pm
This is not specific to a particuliar view, but for instance in one of my view :
<td><%=l job_application.created_at, :format => :default %></td>
I get those strange outputs :
With locale = :en
=> t, 30 o 2012 18:09:33 +0000
With locale = :fr
=> 30 o 2012 18:09:33
Where do these wrong "formats" come from ?
I'm using Rails 3.2.8 (with Postgresql / gem pg), and everything related to I18n works fine except for dates.
Thanks for any help !
I think I've finally figured this out, sorry for taking so long.
The Rails l helper just calls I18n.localize. If you trace through the I18n.localize code, you'll end up here:
format = format.to_s.gsub(/%[aAbBp]/) do |match|
case match
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
end
end
So the localize helper doesn't use strftime for the "stringy" parts of the date/time, it tries to do it by itself. Add the translations (as arrays in your YAML) for the month and day names as above and your localized dates and times should start working.
If you don't have those translation arrays in your YAML, then I18n.t(:"date.abbr_month_names") will give you strings like this:
"translation missing: en.date.abbr_month_names"
and then I18n.localize will end up doing silly things like this:
"translation missing: en.date.abbr_month_names"[10]
That will use String#[] instead of the expected Array#[] and you end up with random looking single character month and day names.
Where do these wrong "formats" come from ?
Because created_at is a DateTime, rails using time formats (not date).
https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml#L195
time:
am: am
formats:
default: ! '%a, %d %b %Y %H:%M:%S %z'
Type in your console
I18n.t(:"date")
to check if you are getting the translations you defined in your translation .yml file.
Compare the structure with the standard EN locale
I18n.t(:"date", locale:'en')
That made me notice I was declaring the date: attribute twice in my .yml, and the first part was being overwritten by the second declaration.
You should get the abbr_month_names that you declared when calling
I18n.t(:"date.abbr_month_names")
These are the ones that will be used when calling %b.
If not, check your locale .yml file to make sure they are properly declared, and not being declared twice.
You may also call I18n.locale to check if the .yml file you are editing is the one being used by rails

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