在Play Framework 2.2中使用区域设置进行日期格式设置

pla*_*ade 4 java date string-formatting playframework playframework-2.2

从我在自动生成的application.conf文件中看到的内容,Play Framework 2.2中的日期/时间根据date.format该文件中的定义进行格式化.例如,我已经定义了

date.format=yyyy-MM-dd
date.format.dk=d. MMMM yyyy
Run Code Online (Sandbox Code Playgroud)

但是,在Scala模板中打印日期时,框架似乎忽略了这些值.线程提供了一个解决方案,其中一个人将模式直接输入到模板中myDate.format("yyyy-MM-dd").(如果使用Jodatime,我想这就是myDate.toDate().format("yyyy-MM-dd")因为类没有format()定义DateTime.)但是这不仅会强制每次显示日期时重复模式,它也会忽略当前的语言环境.

那么,针对不同的语言环境,在Play Framework 2.2.x中格式化日期和时间的预期方式是什么?

joh*_*ren 7

简而言之,如果您想对语言环境进行硬编码使用JodaTime:

@(date: org.joda.DateTime)
@import java.util.Locale
@date.format("yyyy-MMM-dd", new Locale("sv", "SE"))
Run Code Online (Sandbox Code Playgroud)

如果要使用从浏览器lang头中选择的区域设置(您还需要隐式地向模板请求):

@(date: org.joda.DateTime)(implicit lang: play.api.i18n.Lang)
@date.format("yyyy-MMM-dd", lang.toLocale)
Run Code Online (Sandbox Code Playgroud)

我写了一篇关于此的详细博客文章(因为我已经多次看过这个问题):

https://markatta.com/codemonkey/blog/2013/10/14/formatted-localized-dates-in-playframework-2/