19

Without translation, this would get me today's day name:

Date.today.strftime("%A")

How would I localize it?

I.e. "Mardi" if I18n.locale is set to fr.

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

3 Answers3

38

You probably have in your locale file(s) the following:

# example with fr
fr:
  date:
    day_names: [Dimanche, Lundi, Mardi, Mercredi, Jeudi, Vendredi, Samedi]
#               ^^^^^^^^ a week starts with a Sunday, not a Monday

In order to get today's name, you could do:

week_day = Date.today.wday # Returns the day of week (0-6, Sunday is zero)
I18n.t('date.day_names')[week_day]

or eventually

I18n.l(Date.today, format: '%A')
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
10
l Date.today, format: "%A"

Will work if you have the day_names in your translation file.

zwippie
  • 15,050
  • 3
  • 39
  • 54
0

if you use rails-i18n, you will have the day names and month names already translated, e.g.:

I18n.l(value, format: "le %A %e %B à %-Hh%M")
# le Dimanche 19 Juillet à 21h00
Dorian
  • 7,749
  • 4
  • 38
  • 57