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>
My application configuration contains
application.langs="pt-br, en"
The languages defined in browser options contain those two (accept-language)
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);
}
BUT (and sadly enough)
the locale received by my custom DateFormatter in
@Override
public Date parse(String date, Locale locale) {
...
}
is system's (JVM) locale, en-US, and not request preferred one.
Is this normal ? What am I missing here ?
Sadly the global override mentioned by nico ekito is not reliable solution in Play 2.2, probably because of threads. My experience is that the locale was sometimes improper and the formatter was working unpredictably (sometimes formatting in other language then set in context).
So basically the final solution of John Smith is much more reliable. Instead of using locale passed in the formatter method parameter, use the context locale there:
I think you can use this workaround:
For each request, using the Global interceptor, you can set the LocaleContextHolder to set the Locale of your request:
I did not test it, but it's worth the shot :-)