Skip to content

Commit 0a7a53a

Browse files
committed
Polish contribution
See spring-projectsgh-27609
1 parent 864dcf6 commit 0a7a53a

File tree

7 files changed

+55
-35
lines changed

7 files changed

+55
-35
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
* Extension of {@link LocaleResolver} that adds support for a rich locale context
3030
* (potentially including locale and time zone information).
3131
*
32-
* <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale},
33-
* delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
32+
* <p>Also provides {@code default} implementations of {@link #resolveLocale} and
33+
* {@link #setLocale} which delegate to {@link #resolveLocaleContext} and
34+
* {@link #setLocaleContext}, respectively.
3435
*
3536
* @author Juergen Hoeller
37+
* @author Sam Brannen
3638
* @since 4.0
3739
* @see org.springframework.context.i18n.LocaleContext
3840
* @see org.springframework.context.i18n.TimeZoneAwareLocaleContext
@@ -77,12 +79,32 @@ public interface LocaleContextResolver extends LocaleResolver {
7779
void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
7880
@Nullable LocaleContext localeContext);
7981

82+
/**
83+
* Default implementation of {@link LocaleResolver#resolveLocale(HttpServletRequest)}
84+
* that delegates to {@link #resolveLocaleContext(HttpServletRequest)}, falling
85+
* back to {@link HttpServletRequest#getLocale()} if necessary.
86+
* @param request the request to resolve the locale for
87+
* @return the current locale (never {@code null})
88+
* @since 6.0
89+
*/
8090
@Override
8191
default Locale resolveLocale(HttpServletRequest request) {
8292
Locale locale = resolveLocaleContext(request).getLocale();
8393
return (locale != null ? locale : request.getLocale());
8494
}
8595

96+
/**
97+
* Default implementation of {@link LocaleResolver#setLocale(HttpServletRequest,
98+
* HttpServletResponse, Locale)} that delegates to
99+
* {@link #setLocaleContext(HttpServletRequest, HttpServletResponse, LocaleContext)},
100+
* using a {@link SimpleLocaleContext}.
101+
* @param request the request to be used for locale modification
102+
* @param response the response to be used for locale modification
103+
* @param locale the new locale, or {@code null} to clear the locale
104+
* @throws UnsupportedOperationException if the LocaleResolver implementation
105+
* does not support dynamic changing of the locale
106+
* @since 6.0
107+
*/
86108
@Override
87109
default void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
88110
setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
/**
2525
* Abstract base class for {@link LocaleContextResolver} implementations.
2626
*
27-
* <p>Provides support for a {@linkplain #setDefaultLocale(Locale) default locale}
28-
* and a {@linkplain #setDefaultTimeZone(TimeZone) default time zone}.
27+
* <p>Provides support for a {@linkplain #setDefaultLocale(java.util.Locale) default
28+
* locale} and a {@linkplain #setDefaultTimeZone(TimeZone) default time zone}.
2929
*
3030
* @author Juergen Hoeller
3131
* @since 4.0

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Juergen Hoeller
5353
* @author Jean-Pierre Pawlak
5454
* @author Vedran Pavic
55+
* @author Sam Brannen
5556
* @since 27.02.2003
5657
* @see #setDefaultLocale
5758
* @see #setDefaultTimeZone
@@ -98,10 +99,7 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
9899

99100
private Function<HttpServletRequest, Locale> defaultLocaleFunction = request -> {
100101
Locale defaultLocale = getDefaultLocale();
101-
if (defaultLocale == null) {
102-
defaultLocale = request.getLocale();
103-
}
104-
return defaultLocale;
102+
return (defaultLocale != null ? defaultLocale : request.getLocale());
105103
};
106104

107105
private Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction = request -> getDefaultTimeZone();
@@ -199,9 +197,11 @@ protected TimeZone getDefaultTimeZone() {
199197

200198
/**
201199
* Set the function used to determine the default locale for the given request,
202-
* called if no {@link Locale} session attribute has been found.
203-
* <p>The default implementation returns the specified default locale,
204-
* if any, else falls back to the request's accept-header locale.
200+
* called if no locale cookie has been found.
201+
* <p>The default implementation returns the configured
202+
* {@linkplain #setDefaultLocale(Locale) default locale}, if any, and otherwise
203+
* falls back to the request's {@code Accept-Language} header locale or the
204+
* default locale for the server.
205205
* @param defaultLocaleFunction the function used to determine the default locale
206206
* @since 6.0
207207
* @see #setDefaultLocale
@@ -214,8 +214,8 @@ public void setDefaultLocaleFunction(Function<HttpServletRequest, Locale> defaul
214214

215215
/**
216216
* Set the function used to determine the default time zone for the given request,
217-
* called if no {@link TimeZone} session attribute has been found.
218-
* <p>The default implementation returns the specified default time zone,
217+
* called if no locale cookie has been found.
218+
* <p>The default implementation returns the configured default time zone,
219219
* if any, or {@code null} otherwise.
220220
* @param defaultTimeZoneFunction the function used to determine the default time zone
221221
* @since 6.0
@@ -321,8 +321,8 @@ public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletRe
321321
TimeZone timeZone = null;
322322
if (localeContext != null) {
323323
locale = localeContext.getLocale();
324-
if (localeContext instanceof TimeZoneAwareLocaleContext) {
325-
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
324+
if (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAwareLocaleContext) {
325+
timeZone = timeZoneAwareLocaleContext.getTimeZone();
326326
}
327327
addCookie(response,
328328
(locale != null ? toLocaleValue(locale) : "-") + (timeZone != null ? '/' + timeZone.getID() : ""));

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ public class SessionLocaleResolver extends AbstractLocaleContextResolver {
9090

9191
private Function<HttpServletRequest, Locale> defaultLocaleFunction = request -> {
9292
Locale defaultLocale = getDefaultLocale();
93-
if (defaultLocale == null) {
94-
defaultLocale = request.getLocale();
95-
}
96-
return defaultLocale;
93+
return (defaultLocale != null ? defaultLocale : request.getLocale());
9794
};
9895

9996
private Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction = request -> getDefaultTimeZone();
@@ -121,8 +118,10 @@ public void setTimeZoneAttributeName(String timeZoneAttributeName) {
121118
/**
122119
* Set the function used to determine the default locale for the given request,
123120
* called if no {@link Locale} session attribute has been found.
124-
* <p>The default implementation returns the specified default locale,
125-
* if any, else falls back to the request's accept-header locale.
121+
* <p>The default implementation returns the configured
122+
* {@linkplain #setDefaultLocale(Locale) default locale}, if any, and otherwise
123+
* falls back to the request's {@code Accept-Language} header locale or the
124+
* default locale for the server.
126125
* @param defaultLocaleFunction the function used to determine the default locale
127126
* @since 6.0
128127
* @see #setDefaultLocale
@@ -136,7 +135,7 @@ public void setDefaultLocaleFunction(Function<HttpServletRequest, Locale> defaul
136135
/**
137136
* Set the function used to determine the default time zone for the given request,
138137
* called if no {@link TimeZone} session attribute has been found.
139-
* <p>The default implementation returns the specified default time zone,
138+
* <p>The default implementation returns the configured default time zone,
140139
* if any, or {@code null} otherwise.
141140
* @param defaultTimeZoneFunction the function used to determine the default time zone
142141
* @since 6.0
@@ -163,7 +162,7 @@ public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
163162
public Locale getLocale() {
164163
Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
165164
if (locale == null) {
166-
locale = SessionLocaleResolver.this.defaultLocaleFunction.apply(request);
165+
locale = defaultLocaleFunction.apply(request);
167166
}
168167
return locale;
169168
}
@@ -172,7 +171,7 @@ public Locale getLocale() {
172171
public TimeZone getTimeZone() {
173172
TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
174173
if (timeZone == null) {
175-
timeZone = SessionLocaleResolver.this.defaultTimeZoneFunction.apply(request);
174+
timeZone = defaultTimeZoneFunction.apply(request);
176175
}
177176
return timeZone;
178177
}
@@ -187,8 +186,8 @@ public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletRe
187186
TimeZone timeZone = null;
188187
if (localeContext != null) {
189188
locale = localeContext.getLocale();
190-
if (localeContext instanceof TimeZoneAwareLocaleContext) {
191-
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
189+
if (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAwareLocaleContext) {
190+
timeZone = timeZoneAwareLocaleContext.getTimeZone();
192191
}
193192
}
194193
WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);

spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ public static Locale getLocale(HttpServletRequest request) {
171171
@Nullable
172172
public static TimeZone getTimeZone(HttpServletRequest request) {
173173
LocaleResolver localeResolver = getLocaleResolver(request);
174-
if (localeResolver instanceof LocaleContextResolver) {
175-
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
176-
if (localeContext instanceof TimeZoneAwareLocaleContext) {
177-
return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
174+
if (localeResolver instanceof LocaleContextResolver localeContextResolver) {
175+
LocaleContext localeContext = localeContextResolver.resolveLocaleContext(request);
176+
if (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAwareLocaleContext) {
177+
return timeZoneAwareLocaleContext.getTimeZone();
178178
}
179179
}
180180
return null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void setLocaleContextToNullWithDefault() {
412412
}
413413

414414
@Test
415-
void testCustomDefaultLocaleFunction() {
415+
void customDefaultLocaleFunction() {
416416
request.addPreferredLocale(Locale.TAIWAN);
417417

418418
resolver.setDefaultLocaleFunction(request -> Locale.GERMAN);
@@ -421,7 +421,7 @@ void testCustomDefaultLocaleFunction() {
421421
}
422422

423423
@Test
424-
void testCustomDefaultTimeZoneFunction() {
424+
void customDefaultTimeZoneFunction() {
425425
request.addPreferredLocale(Locale.TAIWAN);
426426

427427
resolver.setDefaultTimeZoneFunction(request -> TimeZone.getTimeZone("GMT+1"));

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,16 @@ void setLocaleToNullLocale() throws Exception {
9898
}
9999

100100
@Test
101-
void testCustomDefaultLocaleFunction() {
101+
void customDefaultLocaleFunction() {
102102
request.addPreferredLocale(Locale.TAIWAN);
103103

104-
SessionLocaleResolver resolver = new SessionLocaleResolver();
105104
resolver.setDefaultLocaleFunction(request -> Locale.GERMAN);
106105

107106
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
108107
}
109108

110109
@Test
111-
void testCustomDefaultTimeZoneFunction() {
110+
void customDefaultTimeZoneFunction() {
112111
request.addPreferredLocale(Locale.TAIWAN);
113112

114113
resolver.setDefaultTimeZoneFunction(request -> TimeZone.getTimeZone("GMT+1"));

0 commit comments

Comments
 (0)