AcceptHeaderLocaleResolver falls back to language-only match among its supported locales

Issue: SPR-16457
This commit is contained in:
Juergen Hoeller
2018-02-02 12:41:59 +01:00
parent c5a33d62dd
commit 4dc964544f
2 changed files with 29 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
/**
@@ -117,13 +118,25 @@ public class AcceptHeaderLocaleResolver implements LocaleResolver {
@Nullable
private Locale findSupportedLocale(HttpServletRequest request) {
Enumeration<Locale> requestLocales = request.getLocales();
List<Locale> supported = getSupportedLocales();
Locale languageMatch = null;
while (requestLocales.hasMoreElements()) {
Locale locale = requestLocales.nextElement();
if (getSupportedLocales().contains(locale)) {
if (supported.contains(locale)) {
// Full match: typically language + country
return locale;
}
else if (languageMatch == null) {
// Let's try to find a language-only match as a fallback
for (Locale candidate : supported) {
if (!StringUtils.hasLength(candidate.getCountry()) &&
candidate.getLanguage().equals(locale.getLanguage())) {
languageMatch = candidate;
}
}
}
}
return null;
return languageMatch;
}
@Override