AcceptHeaderLocaleResolver keeps language match among supported locales
Issue: SPR-16599
This commit is contained in:
@@ -98,38 +98,36 @@ public class AcceptHeaderLocaleResolver implements LocaleResolver {
|
||||
return defaultLocale;
|
||||
}
|
||||
Locale requestLocale = request.getLocale();
|
||||
if (isSupportedLocale(requestLocale)) {
|
||||
List<Locale> supportedLocales = getSupportedLocales();
|
||||
if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
|
||||
return requestLocale;
|
||||
}
|
||||
Locale supportedLocale = findSupportedLocale(request);
|
||||
Locale supportedLocale = findSupportedLocale(request, supportedLocales);
|
||||
if (supportedLocale != null) {
|
||||
return supportedLocale;
|
||||
}
|
||||
return (defaultLocale != null ? defaultLocale : requestLocale);
|
||||
}
|
||||
|
||||
private boolean isSupportedLocale(Locale locale) {
|
||||
List<Locale> supportedLocales = getSupportedLocales();
|
||||
return (supportedLocales.isEmpty() || supportedLocales.contains(locale));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Locale findSupportedLocale(HttpServletRequest request) {
|
||||
private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales) {
|
||||
Enumeration<Locale> requestLocales = request.getLocales();
|
||||
List<Locale> supported = getSupportedLocales();
|
||||
Locale languageMatch = null;
|
||||
while (requestLocales.hasMoreElements()) {
|
||||
Locale locale = requestLocales.nextElement();
|
||||
if (supported.contains(locale)) {
|
||||
// Full match: typically language + country
|
||||
return locale;
|
||||
if (supportedLocales.contains(locale)) {
|
||||
if (languageMatch == null || languageMatch.getLanguage().equals(locale.getLanguage())) {
|
||||
// Full match: language + country, possibly narrowed from earlier language-only match
|
||||
return locale;
|
||||
}
|
||||
}
|
||||
else if (languageMatch == null) {
|
||||
// Let's try to find a language-only match as a fallback
|
||||
for (Locale candidate : supported) {
|
||||
for (Locale candidate : supportedLocales) {
|
||||
if (!StringUtils.hasLength(candidate.getCountry()) &&
|
||||
candidate.getLanguage().equals(locale.getLanguage())) {
|
||||
languageMatch = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user