1

I am generating days like this:

(1..days_in_month(year, month)).each do |day|
  calendar << Day.new(day, day_from_date(format_day(day), month, year))
end

And then I access their names in the show page:

<% @calendar.each_with_index do |d, i| %>       
   <tr>
   <td><%= d.name %></td>
<% end %>

The days are in English despite having set the default language in French.

application.rb

config.i18n.default_locale = :fr

config/locales/fr.yml

fr:
  date:
   abbr_day_names:
  - dim
  - lun
  - mar
  - mer
  - jeu
  - ven
  etc.

Console output:

2.1.5 :001 > I18n.locale
=> :fr 

Why aren't the days translated in French?

EDIT:

Since it's a project I had to take over, I tried to look for a documentation for the Day class. I found a local documentation page that explains that Day "represents a day as a number and a name". It doesn't seem to be coming from an official documentation.

Grégoire Borel
  • 1,880
  • 3
  • 33
  • 55
  • You have to use `I18n.localize` to translate the dates (shortcut `l`, as for `t` and `translate`). Something like `<%= l(your_date) %>` – MrYoshiji Feb 25 '15 at 14:40
  • I'm getting the following error: `Object must be a Date, DateTime or Time object. # given.` I don't understand that error because my variable is only composed of `Days`. – Grégoire Borel Feb 25 '15 at 14:57
  • Is `Day` a custom class you create by yourself ? If not, can you link me the documentation about it ? I can't find it online :/ – MrYoshiji Feb 25 '15 at 15:05
  • Nope. See original post. I think I'm gonna have to manually translate the days. – Grégoire Borel Feb 25 '15 at 15:17

1 Answers1

1

You can eventually translate the day names like this:

<% @calendar.each_with_index do |d, i| %>       
   <tr>
   <td><%= t('date.abbr_day_names')[d.number] %></td>
<% end %>

This implies that the attribute number of the Day records represent the "day index" in a 7 days week.

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • Well it does, but it would have been better to have a "day index" on a given month. Here I'm only having "monday, tuesday, [...], saturday" and the rest doesn't appear. And it's the same days for every month, which is obviously incorrect. That was a good idead though! However I'm sure I'm gonna find a workaround with your solution. I'll update the topic asap. – Grégoire Borel Feb 25 '15 at 16:09
  • Does the `number` attribute holds the day's number in the month? Like today would be `Day @number=25 @name=Wednesday` ? Where do you store the month? – MrYoshiji Feb 25 '15 at 16:13
  • Yes it does. The month is stored in another variable, independent from the `Days`. – Grégoire Borel Feb 25 '15 at 16:16
  • 1
    You could use `Date.new(your_year, your_month, day.number)` to create a Date object (localizable with I18n) – MrYoshiji Feb 25 '15 at 16:19
  • Seems a bit overkill but it works! I just need to find the index of a month from its name. Thanks! – Grégoire Borel Feb 25 '15 at 16:29
  • I didn't start this project, and amending some of its code might cause some problems. However it is indeed not very optimized. – Grégoire Borel Feb 25 '15 at 16:44