Top Description Fields Constructors Methods
org.springframework.web.servlet.i18n

public Class CookieLocaleResolver

extends AbstractLocaleContextResolver
Class Inheritance
Imports
java.time.Duration, java.util.Locale, .TimeZone, java.util.function.Function, jakarta.servlet.http.Cookie, .HttpServletRequest, .HttpServletResponse, org.apache.commons.logging.Log, .LogFactory, org.springframework.context.i18n.LocaleContext, .TimeZoneAwareLocaleContext, org.springframework.http.HttpHeaders, .ResponseCookie, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils, org.springframework.web.servlet.LocaleResolver, org.springframework.web.util.WebUtils

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

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.

Authors
Juergen Hoeller, Jean-Pierre Pawlak, Vedran Pavic, Sam Brannen
Since
27.02.2003
See Also
setDefaultLocale, setDefaultTimeZone

Field Summary

Modifier and TypeField and Description
private ResponseCookie
private Function<HttpServletRequest, Locale>
private Function<HttpServletRequest, TimeZone>
private boolean
public static final String
LOCALE_REQUEST_ATTRIBUTE_NAME

The name of the request attribute that holds the Locale.

private static final Log
private boolean
public static final String
TIME_ZONE_REQUEST_ATTRIBUTE_NAME

The name of the request attribute that holds the TimeZone.

Constructor Summary

AccessConstructor and Description
public
CookieLocaleResolver(String cookieName)

Constructor with a given cookie name.

public

Method Summary

Modifier and TypeMethod and Description
protected Locale

Returns:

the default locale (never null)
determineDefaultLocale
(HttpServletRequest
the request to resolve the locale for
request
)
Deprecated since 6.0. as of 6.0, in favor of setDefaultLocaleFunction(Function)

Determine the default locale for the given request, called if no locale cookie has been found.

protected TimeZone

Returns:

the default time zone (or null if none defined)
determineDefaultTimeZone
(HttpServletRequest
the request to resolve the time zone for
request
)
Deprecated since 6.0. as of 6.0, in favor of setDefaultTimeZoneFunction(Function)

Determine the default time zone for the given request, called if no locale cookie has been found.

public boolean
isLanguageTagCompliant()

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

public boolean
isRejectInvalidCookies()

Return whether to reject cookies with invalid content (e.g. invalid format).

private void
protected Locale

Returns:

the corresponding Locale instance
parseLocaleValue
(String
the locale value to parse
localeValue
)

Parse the given locale value coming from an incoming cookie.

public Locale
public LocaleContext
resolveLocaleContext(final HttpServletRequest
the request to resolve the locale context for
request
)

Implements org.springframework.web.servlet.LocaleContextResolver.resolveLocaleContext.

Resolve the current locale context via the given request.

public void
setCookieDomain(String cookieDomain)

Set the cookie "Domain" attribute.

public void
setCookieHttpOnly(boolean cookieHttpOnly)

Add the "HttpOnly" attribute to the cookie.

public void
setCookieMaxAge(Duration cookieMaxAge)

Set the cookie "Max-Age" attribute.

public void
setCookieMaxAge(Integer cookieMaxAge)
Deprecated as of 6.0 in favor of setCookieMaxAge(Duration)

Variant of setCookieMaxAge(Duration) with a value in seconds.

public void
setCookieName(String
the cookie name
cookieName
)
Deprecated as of 6.0 in favor of CookieLocaleResolver(String)

Set the name of cookie created by this resolver.

public void
setCookiePath(String cookiePath)

Set the cookie "Path" attribute.

public void
setCookieSameSite(String cookieSameSite)

Add the "SameSite" attribute to the cookie.

public void
setCookieSecure(boolean cookieSecure)

Add the "Secure" attribute to the cookie.

public void
setDefaultLocaleFunction(Function<HttpServletRequest, Locale>
the function used to determine the default locale
defaultLocaleFunction
)

Set the function used to determine the default locale for the given request, called if no locale cookie has been found.

public void
setDefaultTimeZoneFunction(Function<HttpServletRequest, TimeZone>
the function used to determine the default time zone
defaultTimeZoneFunction
)

Set the function used to determine the default time zone for the given request, called if no locale cookie has been found.

public void
setLanguageTagCompliant(boolean languageTagCompliant)

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

public void
setLocaleContext(HttpServletRequest
the request to be used for locale modification
request
,
HttpServletResponse
the response to be used for locale modification
response
,
LocaleContext
the new locale context, or null to clear the locale
localeContext
)

Implements org.springframework.web.servlet.LocaleContextResolver.setLocaleContext.

Set the current locale context to the given one, potentially including a locale with associated time zone information.

public void
setRejectInvalidCookies(boolean rejectInvalidCookies)

Specify whether to reject cookies with invalid content (e.g. invalid format).

protected String

Returns:

a String representation for the given locale
toLocaleValue
(Locale
the locale to convert to a string
locale
)

Render the given locale as a text value for inclusion in a cookie.

Inherited from org.springframework.web.servlet.i18n.AbstractLocaleContextResolver:
getDefaultTimeZonesetDefaultTimeZone

Field Detail

defaultLocaleFunctionback to summary
private Function<HttpServletRequest, Locale> defaultLocaleFunction
defaultTimeZoneFunctionback to summary
private Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction
languageTagCompliantback to summary
private boolean languageTagCompliant
LOCALE_REQUEST_ATTRIBUTE_NAMEback to summary
public static final String LOCALE_REQUEST_ATTRIBUTE_NAME

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.

See Also
org.springframework.web.servlet.support.RequestContext#getLocale, org.springframework.web.servlet.support.RequestContextUtils#getLocale
loggerback to summary
private static final Log logger
rejectInvalidCookiesback to summary
private boolean rejectInvalidCookies
TIME_ZONE_REQUEST_ATTRIBUTE_NAMEback to summary
public static final String TIME_ZONE_REQUEST_ATTRIBUTE_NAME

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.

See Also
org.springframework.web.servlet.support.RequestContext#getTimeZone, org.springframework.web.servlet.support.RequestContextUtils#getTimeZone

Constructor Detail

CookieLocaleResolverback to summary
public CookieLocaleResolver(String cookieName)

Constructor with a given cookie name.

Since
6.0
CookieLocaleResolverback to summary
public CookieLocaleResolver()

Constructor with a default cookie name.

Method Detail

determineDefaultLocaleback to summary
protected Locale determineDefaultLocale(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, in favor of setDefaultLocaleFunction(Function)

Determine the default locale for the given request, called if no locale cookie has been found.

The default implementation returns the configured default locale, if any, and otherwise falls back to the request's Accept-Language header locale or the default locale for the server.

Parameters
request:HttpServletRequest

the request to resolve the locale for

Returns:Locale

the default locale (never null)

Annotations
@Deprecated
since:6.0
See Also
setDefaultLocale, jakarta.servlet.http.HttpServletRequest#getLocale()
determineDefaultTimeZoneback to summary
protected TimeZone determineDefaultTimeZone(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, in favor of setDefaultTimeZoneFunction(Function)

Determine the default time zone for the given request, called if no locale cookie has been found.

The default implementation returns the configured default time zone, if any, or null otherwise.

Parameters
request:HttpServletRequest

the request to resolve the time zone for

Returns:TimeZone

the default time zone (or null if none defined)

Annotations
@Deprecated
since:6.0
@Nullable
See Also
setDefaultTimeZone
isLanguageTagCompliantback to summary
public boolean isLanguageTagCompliant()

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

Since
4.3
isRejectInvalidCookiesback to summary
public boolean isRejectInvalidCookies()

Return whether to reject cookies with invalid content (e.g. invalid format).

Since
5.1.7
parseLocaleCookieIfNecessaryback to summary
private void parseLocaleCookieIfNecessary(HttpServletRequest request)
parseLocaleValueback to summary
protected Locale parseLocaleValue(String localeValue)

Parse the given locale value coming from an incoming cookie.

The default implementation calls StringUtils#parseLocale(String), accepting the Locale#toString format as well as BCP 47 language tags.

Parameters
localeValue:String

the locale value to parse

Returns:Locale

the corresponding Locale instance

Annotations
@Nullable
Since
4.3
See Also
StringUtils#parseLocale(String)
resolveLocaleback to summary
public Locale resolveLocale(HttpServletRequest request)

Overrides default org.springframework.web.servlet.LocaleContextResolver.resolveLocale.

Implements org.springframework.web.servlet.LocaleResolver.resolveLocale.

Doc from org.springframework.web.servlet.LocaleContextResolver.resolveLocale.

Default implementation of LocaleResolver#resolveLocale(HttpServletRequest) that delegates to resolveLocaleContext(HttpServletRequest), falling back to HttpServletRequest#getLocale() if necessary.

Parameters
request:HttpServletRequest

the request to resolve the locale for

Returns:Locale

the current locale (never null)

Annotations
@Override
resolveLocaleContextback to summary
public LocaleContext resolveLocaleContext(final HttpServletRequest request)

Implements org.springframework.web.servlet.LocaleContextResolver.resolveLocaleContext.

Doc from org.springframework.web.servlet.LocaleContextResolver.resolveLocaleContext.

Resolve the current locale context via the given request.

This is primarily intended for framework-level processing; consider using org.springframework.web.servlet.support.RequestContextUtils or org.springframework.web.servlet.support.RequestContext for application-level access to the current locale and/or time zone.

The returned context may be a org.springframework.context.i18n.TimeZoneAwareLocaleContext, containing a locale with associated time zone information. Simply apply an instanceof check and downcast accordingly.

Custom resolver implementations may also return extra settings in the returned context, which again can be accessed through downcasting.

Parameters
request:HttpServletRequest

the request to resolve the locale context for

Returns:LocaleContext

the current locale context (never null

Annotations
@Override
setCookieDomainback to summary
public void setCookieDomain(String cookieDomain)

Set the cookie "Domain" attribute.

See Also
org.springframework.http.ResponseCookie.ResponseCookieBuilder#domain(String)
setCookieHttpOnlyback to summary
public void setCookieHttpOnly(boolean cookieHttpOnly)

Add the "HttpOnly" attribute to the cookie.

See Also
org.springframework.http.ResponseCookie.ResponseCookieBuilder#httpOnly(boolean)
setCookieMaxAgeback to summary
public void setCookieMaxAge(Duration cookieMaxAge)

Set the cookie "Max-Age" attribute.

By default, this is set to -1 in which case the cookie persists until browser shutdown.

Since
6.0
See Also
org.springframework.http.ResponseCookie.ResponseCookieBuilder#maxAge(Duration)
setCookieMaxAgeback to summary
public void setCookieMaxAge(Integer cookieMaxAge)

Deprecated

as of 6.0 in favor of setCookieMaxAge(Duration)

Variant of setCookieMaxAge(Duration) with a value in seconds.

Annotations
@Deprecated
setCookieNameback to summary
public void setCookieName(String cookieName)

Deprecated

as of 6.0 in favor of CookieLocaleResolver(String)

Set the name of cookie created by this resolver.

Parameters
cookieName:String

the cookie name

Annotations
@Deprecated
setCookiePathback to summary
public void setCookiePath(String cookiePath)

Set the cookie "Path" attribute.

By default, this is set to "/".

See Also
org.springframework.http.ResponseCookie.ResponseCookieBuilder#path(String)
setCookieSameSiteback to summary
public void setCookieSameSite(String cookieSameSite)

Add the "SameSite" attribute to the cookie.

By default, this is set to "Lax".

Since
6.0
See Also
org.springframework.http.ResponseCookie.ResponseCookieBuilder#sameSite(String)
setCookieSecureback to summary
public void setCookieSecure(boolean cookieSecure)

Add the "Secure" attribute to the cookie.

See Also
org.springframework.http.ResponseCookie.ResponseCookieBuilder#secure(boolean)
setDefaultLocaleFunctionback to summary
public void setDefaultLocaleFunction(Function<HttpServletRequest, Locale> defaultLocaleFunction)

Set the function used to determine the default locale for the given request, called if no locale cookie has been found.

The default implementation returns the configured default locale, if any, and otherwise falls back to the request's Accept-Language header locale or the default locale for the server.

Parameters
defaultLocaleFunction:Function<HttpServletRequest, Locale>

the function used to determine the default locale

Since
6.0
See Also
setDefaultLocale, jakarta.servlet.http.HttpServletRequest#getLocale()
setDefaultTimeZoneFunctionback to summary
public void setDefaultTimeZoneFunction(Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction)

Set the function used to determine the default time zone for the given request, called if no locale cookie has been found.

The default implementation returns the configured default time zone, if any, or null otherwise.

Parameters
defaultTimeZoneFunction:Function<HttpServletRequest, TimeZone>

the function used to determine the default time zone

Since
6.0
See Also
setDefaultTimeZone
setLanguageTagCompliantback to summary
public void setLanguageTagCompliant(boolean languageTagCompliant)

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 true, as of 5.1. Switch this to false for rendering Java's legacy locale specification format. For parsing, this resolver leniently accepts the legacy Locale#toString format as well as BCP 47 language tags in any case.

Since
4.3
See Also
parseLocaleValue(String), toLocaleValue(Locale), Locale#forLanguageTag(String), Locale#toLanguageTag()
setLocaleContextback to summary
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext)

Implements org.springframework.web.servlet.LocaleContextResolver.setLocaleContext.

Doc from org.springframework.web.servlet.LocaleContextResolver.setLocaleContext.

Set the current locale context to the given one, potentially including a locale with associated time zone information.

Parameters
request:HttpServletRequest

the request to be used for locale modification

response:HttpServletResponse

the response to be used for locale modification

localeContext:LocaleContext

the new locale context, or null to clear the locale

Annotations
@Override
setRejectInvalidCookiesback to summary
public void setRejectInvalidCookies(boolean rejectInvalidCookies)

Specify whether to reject cookies with invalid content (e.g. invalid format).

The default is true. Turn this off for lenient handling of parse failures, falling back to the default locale and time zone in such a case.

Since
5.1.7
See Also
setDefaultLocale, setDefaultTimeZone, setDefaultLocaleFunction(Function), setDefaultTimeZoneFunction(Function)
toLocaleValueback to summary
protected String toLocaleValue(Locale locale)

Render the given locale as a text value for inclusion in a cookie.

The default implementation calls Locale#toString() or Locale#toLanguageTag(), depending on the "languageTagCompliant" configuration property.

Parameters
locale:Locale

the locale to convert to a string

Returns:String

a String representation for the given locale

Since
4.3
See Also
isLanguageTagCompliant()