Rails:如何让 Date strftime 知道默认的语言环境? [英] Rails: How to make Date strftime aware of the default locale?
问题描述
我在 environment.rb
中将默认语言环境设置为 de
(德语).
I have my default locale set in the environment.rb
as de
(German).
我还看到了德语的所有错误消息,因此服务器选择了区域设置.但是当我尝试使用 strftime
打印日期时,如下所示:
I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime
like following:
some_date.strftime('%B, %y')
它以英语 (January, 11
) 打印,而不是预期的德语 (Januar, 11
).
It prints in English (January, 11
), and not the expected German (Januar, 11
).
如何根据默认语言环境打印日期?
How can I print the date according to the default locale?
推荐答案
使用 l
(localize
的别名)方法代替原始 strftime,如下所示:
Use the l
(alias for localize
) method instead of raw strftime, like this:
l(date, format: '%B %d, in the year %Y')
查看这里了解更多信息,希望对您有所帮助.
See here for more information, hope that helps.
您还可以定义命名"格式,其中一些(short
、long
)已经预定义.
You can also define 'named' formats, a couple of them (short
, long
) are already predefined.
这篇关于Rails:如何让 Date strftime 知道默认的语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!