Rails: How to make Date strftime aware of the default locale? - ruby-on-rails

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 %>

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

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

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

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"

Resources