Browse Source

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

Issue: SPR-16457
pull/1659/merge
Juergen Hoeller 7 years ago
parent
commit
4dc964544f
  1. 19
      spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/AcceptHeaderLocaleResolver.java
  2. 18
      spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/AcceptHeaderLocaleResolverTests.java

19
spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/AcceptHeaderLocaleResolver.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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

18
spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/AcceptHeaderLocaleResolverTests.java

@ -1,5 +1,5 @@ @@ -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.
@ -36,28 +36,34 @@ import static org.junit.Assert.*; @@ -36,28 +36,34 @@ import static org.junit.Assert.*;
*/
public class AcceptHeaderLocaleResolverTests {
private AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
private final AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
@Test
public void resolve() throws Exception {
public void resolve() {
assertEquals(CANADA, this.resolver.resolveLocale(request(CANADA)));
assertEquals(US, this.resolver.resolveLocale(request(US, CANADA)));
}
@Test
public void resolvePreferredSupported() throws Exception {
public void resolvePreferredSupported() {
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
assertEquals(CANADA, this.resolver.resolveLocale(request(US, CANADA)));
}
@Test
public void resolvePreferredNotSupported() throws Exception {
public void resolvePreferredNotSupported() {
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
assertEquals(US, this.resolver.resolveLocale(request(US, UK)));
}
@Test
public void resolvePreferredAgainstLanguageOnly() {
this.resolver.setSupportedLocales(Collections.singletonList(ENGLISH));
assertEquals(ENGLISH, this.resolver.resolveLocale(request(GERMANY, US, UK)));
}
@Test
public void resolvePreferredNotSupportedWithDefault() {
this.resolver.setSupportedLocales(Arrays.asList(US, JAPAN));
this.resolver.setDefaultLocale(Locale.JAPAN);
@ -69,7 +75,7 @@ public class AcceptHeaderLocaleResolverTests { @@ -69,7 +75,7 @@ public class AcceptHeaderLocaleResolverTests {
}
@Test
public void defaultLocale() throws Exception {
public void defaultLocale() {
this.resolver.setDefaultLocale(JAPANESE);
MockHttpServletRequest request = new MockHttpServletRequest();
assertEquals(JAPANESE, this.resolver.resolveLocale(request));

Loading…
Cancel
Save