Skip to content

Commit cec36fe

Browse files
committed
Configurable locale/timezone attribute name for SessionLocaleResolver
Issue: SPR-15450
1 parent dc279d8 commit cec36fe

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

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

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 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.
@@ -79,9 +79,35 @@ public class SessionLocaleResolver extends AbstractLocaleContextResolver {
7979
public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".TIME_ZONE";
8080

8181

82+
private String localeAttributeName = LOCALE_SESSION_ATTRIBUTE_NAME;
83+
84+
private String timeZoneAttributeName = TIME_ZONE_SESSION_ATTRIBUTE_NAME;
85+
86+
87+
/**
88+
* Specify the name of the corresponding attribute in the {@code HttpSession},
89+
* holding the current {@link Locale} value.
90+
* <p>The default is an internal {@link #LOCALE_SESSION_ATTRIBUTE_NAME}.
91+
* @since 4.3.8
92+
*/
93+
public void setLocaleAttributeName(String localeAttributeName) {
94+
this.localeAttributeName = localeAttributeName;
95+
}
96+
97+
/**
98+
* Specify the name of the corresponding attribute in the {@code HttpSession},
99+
* holding the current {@link TimeZone} value.
100+
* <p>The default is an internal {@link #TIME_ZONE_SESSION_ATTRIBUTE_NAME}.
101+
* @since 4.3.8
102+
*/
103+
public void setTimeZoneAttributeName(String timeZoneAttributeName) {
104+
this.timeZoneAttributeName = timeZoneAttributeName;
105+
}
106+
107+
82108
@Override
83109
public Locale resolveLocale(HttpServletRequest request) {
84-
Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
110+
Locale locale = (Locale) WebUtils.getSessionAttribute(request, this.localeAttributeName);
85111
if (locale == null) {
86112
locale = determineDefaultLocale(request);
87113
}
@@ -93,15 +119,15 @@ public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
93119
return new TimeZoneAwareLocaleContext() {
94120
@Override
95121
public Locale getLocale() {
96-
Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
122+
Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
97123
if (locale == null) {
98124
locale = determineDefaultLocale(request);
99125
}
100126
return locale;
101127
}
102128
@Override
103129
public TimeZone getTimeZone() {
104-
TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME);
130+
TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
105131
if (timeZone == null) {
106132
timeZone = determineDefaultTimeZone(request);
107133
}
@@ -120,8 +146,8 @@ public void setLocaleContext(HttpServletRequest request, HttpServletResponse res
120146
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
121147
}
122148
}
123-
WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
124-
WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
149+
WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
150+
WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
125151
}
126152

127153

0 commit comments

Comments
 (0)