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..
setDefaultLocale, setDefaultTimeZone
| Modifier and Type | Field and Description |
|---|---|
| private ResponseCookie | |
| public static final String | DEFAULT_COOKIE_NAME
The default cookie name used if none is explicitly set. |
| private Function | |
| private Function | |
| private boolean | |
| public static final String | LOCALE_REQUEST_ATTRIBUTE_NAME
The name of the request attribute that holds the |
| private static final Log | |
| private boolean | |
| public static final String | TIME_ZONE_REQUEST_ATTRIBUTE_NAME
The name of the request attribute that holds the |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected Locale | Returns: the default locale (nevernull)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 (ornull if none defined)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 correspondingLocale instancethe locale value to parse localeValue)Parse the given locale value coming from an incoming cookie. |
| public Locale | resolveLocale(HttpServletRequest
the request to resolve the locale for request)Overrides default org. Implements org. Default implementation of |
| public LocaleContext | resolveLocaleContext(final HttpServletRequest
the request to resolve the locale context for request)Implements org. Resolve the current locale context via the given request. |
| public void | |
| public void | |
| public void | |
| public void | setCookieMaxAge(Integer cookieMaxAge)
Deprecated
as of 6.0 in favor of
setCookieMaxAge(Duration)
Variant of |
| 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 | |
| public void | |
| public void | |
| 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 localeContext)null to clear the localeImplements org. 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 localethe locale to convert to a string locale)Render the given locale as a text value for inclusion in a cookie. |
| cookie | back to summary |
|---|---|
| private ResponseCookie cookie | |
| DEFAULT_COOKIE_NAME | back to summary |
|---|---|
| public static final String DEFAULT_COOKIE_NAME The default cookie name used if none is explicitly set. | |
| defaultLocaleFunction | back to summary |
|---|---|
| private Function<HttpServletRequest, Locale> defaultLocaleFunction | |
| defaultTimeZoneFunction | back to summary |
|---|---|
| private Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction | |
| languageTagCompliant | back to summary |
|---|---|
| private boolean languageTagCompliant | |
| LOCALE_REQUEST_ATTRIBUTE_NAME | back to summary |
|---|---|
| public static final String LOCALE_REQUEST_ATTRIBUTE_NAME The name of the request attribute that holds the Only used for overriding a cookie value if the locale has been changed in the course of the current request! Use | |
| logger | back to summary |
|---|---|
| private static final Log logger | |
| rejectInvalidCookies | back to summary |
|---|---|
| private boolean rejectInvalidCookies | |
| TIME_ZONE_REQUEST_ATTRIBUTE_NAME | back to summary |
|---|---|
| public static final String TIME_ZONE_REQUEST_ATTRIBUTE_NAME The name of the request attribute that holds the Only used for overriding a cookie value if the locale has been changed in the course of the current request! Use | |
| CookieLocaleResolver | back to summary |
|---|---|
| public CookieLocaleResolver(String cookieName) Constructor with a given cookie name.
| |
| CookieLocaleResolver | back to summary |
|---|---|
| public CookieLocaleResolver() Constructor with a default cookie name. | |
| determineDefaultLocale | back to summary |
|---|---|
| protected Locale determineDefaultLocale(HttpServletRequest request)
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
| |
| determineDefaultTimeZone | back to summary |
|---|---|
| protected TimeZone determineDefaultTimeZone(HttpServletRequest request)
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
| |
| isLanguageTagCompliant | back 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.
| |
| isRejectInvalidCookies | back to summary |
|---|---|
| public boolean isRejectInvalidCookies() Return whether to reject cookies with invalid content (e.g. invalid format).
| |
| parseLocaleCookieIfNecessary | back to summary |
|---|---|
| private void parseLocaleCookieIfNecessary(HttpServletRequest request) | |
| parseLocaleValue | back to summary |
|---|---|
| protected Locale parseLocaleValue(String localeValue) Parse the given locale value coming from an incoming cookie. The default implementation calls
| |
| resolveLocale | back to summary |
|---|---|
| public Locale resolveLocale(HttpServletRequest request) Overrides default org. Implements org. Doc from org. Default implementation of
| |
| resolveLocaleContext | back to summary |
|---|---|
| public LocaleContext resolveLocaleContext(final HttpServletRequest request) Implements org. Doc from org. Resolve the current locale context via the given request. This is primarily intended for framework-level processing; consider using
The returned context may be a
Custom resolver implementations may also return extra settings in the returned context, which again can be accessed through downcasting.
| |
| setCookieDomain | back to summary |
|---|---|
| public void setCookieDomain(String cookieDomain) Set the cookie "Domain" attribute. | |
| setCookieHttpOnly | back to summary |
|---|---|
| public void setCookieHttpOnly(boolean cookieHttpOnly) Add the "HttpOnly" attribute to the cookie. | |
| setCookieMaxAge | back 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. | |
| setCookieMaxAge | back to summary |
|---|---|
| public void setCookieMaxAge(Integer cookieMaxAge)
Deprecated as of 6.0 in favor of Variant of
| |
| setCookieName | back to summary |
|---|---|
| public void setCookieName(String cookieName)
Deprecated as of 6.0 in favor of Set the name of cookie created by this resolver.
| |
| setCookiePath | back to summary |
|---|---|
| public void setCookiePath(String cookiePath) Set the cookie "Path" attribute. By default, this is set to | |
| setCookieSameSite | back to summary |
|---|---|
| public void setCookieSameSite(String cookieSameSite) Add the "SameSite" attribute to the cookie. By default, this is set to | |
| setCookieSecure | back to summary |
|---|---|
| public void setCookieSecure(boolean cookieSecure) Add the "Secure" attribute to the cookie. | |
| setDefaultLocaleFunction | back 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
| |
| setDefaultTimeZoneFunction | back 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
| |
| setLanguageTagCompliant | back 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
| |
| setLocaleContext | back to summary |
|---|---|
| public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) Implements org. Doc from org. Set the current locale context to the given one, potentially including a locale with associated time zone information.
| |
| setRejectInvalidCookies | back to summary |
|---|---|
| public void setRejectInvalidCookies(boolean rejectInvalidCookies) Specify whether to reject cookies with invalid content (e.g. invalid format). The default is
| |
| toLocaleValue | back to summary |
|---|---|
| protected String toLocaleValue(Locale locale) Render the given locale as a text value for inclusion in a cookie. The default implementation calls
| |