带 I18n 的日期名称
Day name with I18n
如果没有翻译,这会得到我今天的名字:
Date.today.strftime("%A")
我如何本地化它?
即"Mardi" 如果 I18n.locale
设置为 fr
。
l Date.today, format: "%A"
如果您的翻译文件中有 day_names
,就可以使用。
您的语言环境文件中可能包含以下内容:
# example with fr
fr:
date:
day_names: [Dimanche, Lundi, Mardi, Mercredi, Jeudi, Vendredi, Samedi]
# ^^^^^^^^ a week starts with a Sunday, not a Monday
为了得到今天的名字,你可以这样做:
week_day = Date.today.wday # Returns the day of week (0-6, Sunday is zero)
I18n.t('date.day_names')[week_day]
或最终
I18n.l(Date.today, format: '%A')
如果您使用 rails-i18n
,您将得到已翻译的日期名称和月份名称,例如:
I18n.l(value, format: "le %A %e %B à %-Hh%M")
# le Dimanche 19 Juillet à 21h00
如果没有翻译,这会得到我今天的名字:
Date.today.strftime("%A")
我如何本地化它?
即"Mardi" 如果 I18n.locale
设置为 fr
。
l Date.today, format: "%A"
如果您的翻译文件中有 day_names
,就可以使用。
您的语言环境文件中可能包含以下内容:
# example with fr
fr:
date:
day_names: [Dimanche, Lundi, Mardi, Mercredi, Jeudi, Vendredi, Samedi]
# ^^^^^^^^ a week starts with a Sunday, not a Monday
为了得到今天的名字,你可以这样做:
week_day = Date.today.wday # Returns the day of week (0-6, Sunday is zero)
I18n.t('date.day_names')[week_day]
或最终
I18n.l(Date.today, format: '%A')
如果您使用 rails-i18n
,您将得到已翻译的日期名称和月份名称,例如:
I18n.l(value, format: "le %A %e %B à %-Hh%M")
# le Dimanche 19 Juillet à 21h00