Spring mvc get default locale from request if locale is invalid

Viewed 2924

I've got i18n configured in my Spring MVC app (localeInterceptor etc).

Two languages supported (en & pl). PL is default one. I18n works of course.

My case is to get current locale and pass it to model. That's easy, but when I pass invalid locale argument (like localhost:8080/?language=asd) I get "asd" instead of "pl" which is default.

It is worth to mention that whole i18n works perfectly, I mean messages_pl.properties are used in this case. I tried following methods with no success:

@RequestMapping(...)
public String home(Locale locale,..)

localeResolver.resolveLocale(request);
RequestContextUtils.getLocale(request);
LocaleContextHolder.getLocale();

every of these gives me a "asd" instead of "pl".

Atm I've got a bit messy workaround:

if (!supportedLocaleService.isLocaleSupported(locale)) {//myservice
    locale = new Locale("pl");
}
2 Answers
Related