Chaining Spring CookieLocaleResolver and AcceptHeaderLocaleResolver
It looks like CookieLocaleResolver
does exactly what you want as long as you don't set its defaultLocale
.
If you want something different (for example, fallback to defaultLocale
when neither cookie nor Accept
header was found), you can override its determineDefaultLocale()
accordingly.
Example cookie locale resolver that fallback first to Accept-Language
header and only then to defaultLocale
:
public class CookieThenAcceptHeaderLocaleResolver extends CookieLocaleResolver {
@Override
protected Locale determineDefaultLocale(HttpServletRequest request) {
String acceptLanguage = request.getHeader("Accept-Language");
if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {
return super.determineDefaultLocale(request);
}
return request.getLocale();
}
}