Skip to content

Commit a3d199f

Browse files
committed
AcceptHeaderLocaleResolver returns default locale in case of no supported locale found
Issue: SPR-15426 (cherry picked from commit ea98ee8)
1 parent 597fe07 commit a3d199f

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,27 +95,31 @@ public Locale resolveLocale(HttpServletRequest request) {
9595
if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
9696
return defaultLocale;
9797
}
98-
Locale locale = request.getLocale();
99-
if (!isSupportedLocale(locale)) {
100-
locale = findSupportedLocale(request, locale);
98+
Locale requestLocale = request.getLocale();
99+
if (isSupportedLocale(requestLocale)) {
100+
return requestLocale;
101101
}
102-
return locale;
102+
Locale supportedLocale = findSupportedLocale(request);
103+
if (supportedLocale != null) {
104+
return supportedLocale;
105+
}
106+
return (defaultLocale != null ? defaultLocale : requestLocale);
103107
}
104108

105109
private boolean isSupportedLocale(Locale locale) {
106110
List<Locale> supportedLocales = getSupportedLocales();
107111
return (supportedLocales.isEmpty() || supportedLocales.contains(locale));
108112
}
109113

110-
private Locale findSupportedLocale(HttpServletRequest request, Locale fallback) {
114+
private Locale findSupportedLocale(HttpServletRequest request) {
111115
Enumeration<Locale> requestLocales = request.getLocales();
112116
while (requestLocales.hasMoreElements()) {
113117
Locale locale = requestLocales.nextElement();
114118
if (getSupportedLocales().contains(locale)) {
115119
return locale;
116120
}
117121
}
118-
return fallback;
122+
return null;
119123
}
120124

121125
@Override

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.servlet.i18n;
1718

1819
import java.util.Arrays;
@@ -24,15 +25,14 @@
2425

2526
import org.springframework.mock.web.test.MockHttpServletRequest;
2627

27-
import static java.util.Locale.CANADA;
28-
import static java.util.Locale.JAPANESE;
29-
import static java.util.Locale.UK;
30-
import static java.util.Locale.US;
31-
import static org.junit.Assert.assertEquals;
28+
import static java.util.Locale.*;
29+
import static org.junit.Assert.*;
3230

3331
/**
3432
* Unit tests for {@link AcceptHeaderLocaleResolver}.
33+
*
3534
* @author Rossen Stoyanchev
35+
* @author Juergen Hoeller
3636
*/
3737
public class AcceptHeaderLocaleResolverTests {
3838

@@ -56,6 +56,17 @@ public void resolvePreferredNotSupported() throws Exception {
5656
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
5757
assertEquals(US, this.resolver.resolveLocale(request(US, UK)));
5858
}
59+
@Test
60+
61+
public void resolvePreferredNotSupportedWithDefault() {
62+
this.resolver.setSupportedLocales(Arrays.asList(US, JAPAN));
63+
this.resolver.setDefaultLocale(Locale.JAPAN);
64+
65+
MockHttpServletRequest request = new MockHttpServletRequest();
66+
request.addHeader("Accept-Language", KOREA.toString());
67+
request.setPreferredLocales(Collections.singletonList(KOREA));
68+
assertEquals(Locale.JAPAN, this.resolver.resolveLocale(request));
69+
}
5970

6071
@Test
6172
public void defaultLocale() throws Exception {

0 commit comments

Comments
 (0)