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

CookieLocaleResolver

open class CookieLocaleResolver : CookieGenerator, LocaleContextResolver

LocaleResolver implementation that uses a cookie sent back to the user in case of a custom setting, with a fallback to the specified default locale or the request's accept-header locale.

This is particularly useful for stateless applications without user sessions. The cookie may optionally contain an associated time zone value 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.

Author
Juergen Hoeller

Author
Jean-Pierre Pawlak

Since
27.02.2003

See Also
#setDefaultLocale#setDefaultTimeZone

Constructors

<init>

CookieLocaleResolver()

Create a new instance of the CookieLocaleResolver class using the default cookie name.

Properties

DEFAULT_COOKIE_NAME

static val DEFAULT_COOKIE_NAME: String

The default cookie name used if none is explicitly set.

LOCALE_REQUEST_ATTRIBUTE_NAME

static val LOCALE_REQUEST_ATTRIBUTE_NAME: String

The name of the request attribute that holds the Locale.

Only used for overriding a cookie value if the locale has been changed in the course of the current request!

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

TIME_ZONE_REQUEST_ATTRIBUTE_NAME

static val TIME_ZONE_REQUEST_ATTRIBUTE_NAME: String

The name of the request attribute that holds the TimeZone.

Only used for overriding a cookie value if the locale has been changed in the course of the current request!

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

Inherited Properties

DEFAULT_COOKIE_PATH

static val DEFAULT_COOKIE_PATH: String

Default path that cookies will be visible to: "/", i.e. the entire server.

Functions

isLanguageTagCompliant

open fun isLanguageTagCompliant(): Boolean

Return whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format.

resolveLocale

open fun resolveLocale(request: HttpServletRequest): Locale

resolveLocaleContext

open fun resolveLocaleContext(request: HttpServletRequest): LocaleContext

setDefaultLocale

open fun setDefaultLocale(defaultLocale: Locale): Unit

Set a fixed Locale that this resolver will return if no cookie found.

setDefaultTimeZone

open fun setDefaultTimeZone(defaultTimeZone: TimeZone): Unit

Set a fixed TimeZone that this resolver will return if no cookie found.

setLanguageTagCompliant

open fun setLanguageTagCompliant(languageTagCompliant: Boolean): Unit

Specify whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format. The default is false.

setLocale

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

setLocaleContext

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

Inherited Functions

addCookie

open fun addCookie(response: HttpServletResponse, cookieValue: String): Unit

Add a cookie with the given value to the response, using the cookie descriptor settings of this generator.

Delegates to #createCookie for cookie creation.

getCookieDomain

open fun getCookieDomain(): String

Return the domain for cookies created by this generator, if any.

getCookieMaxAge

open fun getCookieMaxAge(): Int

Return the maximum age for cookies created by this generator.

getCookieName

open fun getCookieName(): String

Return the given name for cookies created by this generator.

getCookiePath

open fun getCookiePath(): String

Return the path for cookies created by this generator.

isCookieHttpOnly

open fun isCookieHttpOnly(): Boolean

Return whether the cookie is supposed to be marked with the "HttpOnly" attribute.

isCookieSecure

open fun isCookieSecure(): Boolean

Return whether the cookie should only be sent using a secure protocol, such as HTTPS (SSL).

removeCookie

open fun removeCookie(response: HttpServletResponse): Unit

Remove the cookie that this generator describes from the response. Will generate a cookie with empty value and max age 0.

Delegates to #createCookie for cookie creation.

setCookieDomain

open fun setCookieDomain(cookieDomain: String): Unit

Use the given domain for cookies created by this generator. The cookie is only visible to servers in this domain.

setCookieHttpOnly

open fun setCookieHttpOnly(cookieHttpOnly: Boolean): Unit

Set whether the cookie is supposed to be marked with the "HttpOnly" attribute.

Default is "false".

setCookieMaxAge

open fun setCookieMaxAge(cookieMaxAge: Int): Unit

Use the given maximum age (in seconds) for cookies created by this generator. Useful special value: -1 ... not persistent, deleted when client shuts down.

Default is no specific maximum age at all, using the Servlet container's default.

setCookieName

open fun setCookieName(cookieName: String): Unit

Use the given name for cookies created by this generator.

setCookiePath

open fun setCookiePath(cookiePath: String): Unit

Use the given path for cookies created by this generator. The cookie is only visible to URLs in this path and below.

setCookieSecure

open fun setCookieSecure(cookieSecure: Boolean): Unit

Set whether the cookie should only be sent using a secure protocol, such as HTTPS (SSL). This is an indication to the receiving browser, not processed by the HTTP server itself.

Default is "false".