Rails:如何使Date strftime知道默认语言环境? [英] Rails: How to make Date strftime aware of the default locale?

查看:95
本文介绍了Rails:如何使Date strftime知道默认语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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.

您还可以定义命名"格式,其中一些(shortlong)已经预定义.

You can also define 'named' formats, a couple of them (short, long) are already predefined.

这篇关于Rails:如何使Date strftime知道默认语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆