根据我在自动生成的 application.conf
文件中看到的内容,Play Framework 2.2 中的日期/时间根据 date.format
中的定义进行格式化那个文件。例如,我定义了
date.format=yyyy-MM-dd
date.format.dk=d. MMMM yyyy
但是,在 Scala 模板中打印日期时,框架似乎忽略了这些值。 This thread 提供了一种解决方案,其中将模式作为 myDate.format("yyyy-MM-dd")
直接输入到模板中。 (如果使用 Jodatime,我猜这会变成 myDate.toDate().format("yyyy-MM-dd")
因为 上没有定义
类。)但这不仅会在每次显示日期时强制重复该模式,还会忽略当前的语言环境。format()
>DateTime
那么在 Play Framework 2.2.x 中针对不同语言环境格式化日期和时间的预期方式是什么?
最佳答案
简而言之,如果您想对区域设置进行硬编码并使用 JodaTime:
@(date: org.joda.DateTime)
@import java.util.Locale
@date.format("yyyy-MMM-dd", new Locale("sv", "SE"))
如果你想使用从浏览器 lang header 中选择的语言环境(你还需要隐式请求到你的模板):
@(date: org.joda.DateTime)(implicit lang: play.api.i18n.Lang)
@date.format("yyyy-MMM-dd", lang.toLocale)
我写了一篇关于这个的详细博客条目(因为我已经多次看到这个问题):
https://markatta.com/codemonkey/blog/2013/10/14/formatted-localized-dates-in-playframework-2/
关于java - Play Framework 2.2 中使用区域设置的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19335705/