Ruby-on-rails Rails:如何让 Date strftime 知道默认语言环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4592235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: How to make Date strftime aware of the default locale?
提问by rangalo
I have my default locale set in the environment.rbas de(German).
我的默认语言环境设置为environment.rbas de(德语)。
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 strftimelike following:
我还看到了德语的所有错误消息,因此服务器选择了语言环境。但是当我尝试使用strftime以下内容打印日期时:
some_date.strftime('%B, %y')
It prints in English (January, 11), and not the expected German (Januar, 11).
它以英语 ( January, 11)打印,而不是预期的德语 ( Januar, 11)。
How can I print the date according to the default locale?
如何根据默认语言环境打印日期?
回答by Milan Novota
Use the l(alias for localize) method instead of raw strftime, like this:
使用l(alias for localize) 方法代替原始 strftime,如下所示:
l(date, format: '%B %d, in the year %Y')
See herefor more information, hope that helps.
请参阅此处了解更多信息,希望对您有所帮助。
You can also define 'named' formats, a couple of them (short, long) are already predefined.
您还可以定义“命名”格式,其中一些 ( short, long) 已经预定义。
回答by kstarski
you can also make it shorter:
你也可以缩短它:
l(some_date, :format => '%d %B %Y')
回答by Daniel
In es.yml put:
在 es.yml 中输入:
es:
date:
formats:
default: "%d / %m / %Y"
In index.html.erb put:
在 index.html.erb 中:
<%= l somemodel.datefield %>

