spring-framework / org.springframework.web.servlet.i18n / SessionLocaleResolver

SessionLocaleResolver

open class SessionLocaleResolver : AbstractLocaleContextResolver

org.springframework.web.servlet.LocaleResolver implementation that uses a locale attribute in the user's session in case of a custom setting, with a fallback to the specified default locale or the request's accept-header locale.

This is most appropriate if the application needs user sessions anyway, i.e. when the HttpSession does not have to be created just for storing the user's locale. The session may optionally contain an associated time zone attribute as well; alternatively, you may specify a default time zone.

Custom controllers can override the user's locale and time zone by calling #setLocale(Context) on the resolver, e.g. responding to a locale change request. As a more convenient alternative, consider using org.springframework.web.servlet.support.RequestContext#changeLocale.

In contrast to CookieLocaleResolver, this strategy stores locally chosen locale settings in the Servlet container's HttpSession. As a consequence, those settings are just temporary for each session and therefore lost when each session terminates.

Note that there is no direct relationship with external session management mechanisms such as the "Spring Session" project. This LocaleResolver will simply evaluate and modify corresponding HttpSession attributes against the current HttpServletRequest.

Author
Juergen Hoeller

Since
27.02.2003

See Also
#setDefaultLocale#setDefaultTimeZone

Constructors

<init>

SessionLocaleResolver()

org.springframework.web.servlet.LocaleResolver implementation that uses a locale attribute in the user's session in case of a custom setting, with a fallback to the specified default locale or the request's accept-header locale.

This is most appropriate if the application needs user sessions anyway, i.e. when the HttpSession does not have to be created just for storing the user's locale. The session may optionally contain an associated time zone attribute as well; alternatively, you may specify a default time zone.

Custom controllers can override the user's locale and time zone by calling #setLocale(Context) on the resolver, e.g. responding to a locale change request. As a more convenient alternative, consider using org.springframework.web.servlet.support.RequestContext#changeLocale.

In contrast to CookieLocaleResolver, this strategy stores locally chosen locale settings in the Servlet container's HttpSession. As a consequence, those settings are just temporary for each session and therefore lost when each session terminates.

Note that there is no direct relationship with external session management mechanisms such as the "Spring Session" project. This LocaleResolver will simply evaluate and modify corresponding HttpSession attributes against the current HttpServletRequest.

Properties

LOCALE_SESSION_ATTRIBUTE_NAME

static val LOCALE_SESSION_ATTRIBUTE_NAME: String

Name of the session attribute that holds the Locale. Only used internally by this implementation.

Use RequestContext(Utils).getLocale() to retrieve the current locale in controllers or views.

TIME_ZONE_SESSION_ATTRIBUTE_NAME

static val TIME_ZONE_SESSION_ATTRIBUTE_NAME: String

Name of the session attribute that holds the TimeZone. Only used internally by this implementation.

Use RequestContext(Utils).getTimeZone() to retrieve the current time zone in controllers or views.

Functions

resolveLocale

open fun resolveLocale(request: HttpServletRequest): Locale

resolveLocaleContext

open fun resolveLocaleContext(request: HttpServletRequest): LocaleContext

setLocaleAttributeName

open fun setLocaleAttributeName(localeAttributeName: String): Unit

Specify the name of the corresponding attribute in the HttpSession, holding the current Locale value.

The default is an internal #LOCALE_SESSION_ATTRIBUTE_NAME.

setLocaleContext

open fun setLocaleContext(request: HttpServletRequest, response: HttpServletResponse, localeContext: LocaleContext): Unit

setTimeZoneAttributeName

open fun setTimeZoneAttributeName(timeZoneAttributeName: String): Unit

Specify the name of the corresponding attribute in the HttpSession, holding the current TimeZone value.

The default is an internal #TIME_ZONE_SESSION_ATTRIBUTE_NAME.

Inherited Functions

getDefaultTimeZone

open fun getDefaultTimeZone(): TimeZone

Return the default TimeZone that this resolver is supposed to fall back to, if any.

setDefaultTimeZone

open fun setDefaultTimeZone(defaultTimeZone: TimeZone): Unit

Set a default TimeZone that this resolver will return if no other time zone found.

setLocale

open fun setLocale(request: HttpServletRequest, response: HttpServletResponse, locale: Locale): Unit