Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Day name with I18n

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.

like image 1000
Fellow Stranger Avatar asked Nov 12 '15 15:11

Fellow Stranger


2 Answers

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')
like image 58
MrYoshiji Avatar answered Nov 15 '22 12:11

MrYoshiji


l Date.today, format: "%A"

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

like image 27
zwippie Avatar answered Nov 15 '22 12:11

zwippie