简体   繁体   English

带有 I18n 的日期名称

[英]Day name with I18n

Without translation, this would get me today's day name:如果没有翻译,这将得到我今天的名字:

Date.today.strftime("%A")

How would I localize it?我将如何本地化它?

Ie "Mardi" if I18n.locale is set to fr .即“狂欢节”,如果I18n.locale设置为fr

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')
l Date.today, format: "%A"

如果您的翻译文件中有day_names ,将起作用。

if you use rails-i18n , you will have the day names and month names already translated, eg:如果您使用rails-i18n ,您将已经翻译了日期名称和月份名称,例如:

I18n.l(value, format: "le %A %e %B à %-Hh%M")
# le Dimanche 19 Juillet à 21h00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM