Playframework 2.0.1DateFormatter接收系统区域设置 [英] Playframework 2.0.1DateFormatter receives system locale
问题描述
我在Play 2.0/Java中编写了一个自定义的DateFormatter,因为默认值似乎是i18n不知道的(实现细节与此处无关)
I wrote a custom DateFormatter in Play 2.0 / Java because the default one seems to be i18n-unaware (the implementation details are irrelevant here)
public class DateFormatter extends Formatters.SimpleFormatter<Date>
我的应用程序配置包含
application.langs="pt-br, en"
浏览器选项中定义的语言包含这两种(接受语言)
The languages defined in browser options contain those two (accept-language)
从逻辑上讲,Lang.preferred(List)返回pt-br作为首选语言,如
Logically, Lang.preferred(List) returns pt-br as preferred language like in
@Override
public Action onRequest(Request request, Method method) {
Lang preferred = Lang.preferred(request.acceptLanguages());
Logger.debug("Preferred language is " + preferred.toLocale());
return super.onRequest(request, method);
}
但是(可悲的是)
我的自定义DateFormatter在
the locale received by my custom DateFormatter in
@Override
public Date parse(String date, Locale locale) {
...
}
是系统(JVM)的语言环境(在美国),并且不请求首选的语言环境.
is system's (JVM) locale, en-US, and not request preferred one.
这正常吗?我在这里想念什么?
Is this normal ? What am I missing here ?
推荐答案
我认为您可以使用以下解决方法:
I think you can use this workaround:
对于每个请求,使用全局拦截器,您可以设置LocaleContextHolder设置您的请求的语言环境:
For each request, using the Global interceptor, you can set the LocaleContextHolder to set the Locale of your request:
public class Global extends GlobalSettings {
@Override
public Action onRequest(final Request request, Method actionMethod) {
LocaleContextHolder.setLocaleContext(new LocaleContext() {
public Locale getLocale() {
Lang preferred = Lang.preferred(request.acceptLanguages());
return preferred.toLocale();
}
});
return super.onRequest(request, actionMethod);
}
}
我没有测试它,但是值得一试:-)
I did not test it, but it's worth the shot :-)
这篇关于Playframework 2.0.1DateFormatter接收系统区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!