Package org.springframework.web.servlet

# Interface LocaleContextResolver

All Superinterfaces:
  `LocaleResolver`

All Known Implementing Classes:
  `AbstractLocaleContextResolver`, `CookieLocaleResolver`, `FixedLocaleResolver`, `SessionLocaleResolver`

public interface LocaleContextResolver extends LocaleResolver
Extension of `LocaleResolver` that adds support for a rich locale context (potentially including locale and time zone information).

Also provides `default` implementations of `resolveLocale(jakarta.servlet.http.HttpServletRequest)` and `setLocale(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.util.Locale)` which delegate to `resolveLocaleContext(jakarta.servlet.http.HttpServletRequest)` and `setLocaleContext(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, org.springframework.context.i18n.LocaleContext)`, respectively.

Since:
  4.0
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  - `LocaleContext`
  - `TimeZoneAwareLocaleContext`
  - `LocaleContextHolder`
  - `RequestContext.getTimeZone()`
  - `RequestContextUtils.getTimeZone(jakarta.servlet.http.HttpServletRequest)`

## Method Summary

Modifier and Type
Method
Description
`default Locale`
`resolveLocale(HttpServletRequest request)`
Default implementation of `LocaleResolver.resolveLocale(HttpServletRequest)` that delegates to `resolveLocaleContext(HttpServletRequest)`, falling back to `ServletRequest.getLocale()` if necessary.
`org.springframework.context.i18n.LocaleContext`
`resolveLocaleContext(HttpServletRequest request)`
Resolve the current locale context via the given request.
`default void`
`setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)`
Default implementation of `LocaleResolver.setLocale(HttpServletRequest, HttpServletResponse, Locale)` that delegates to `setLocaleContext(HttpServletRequest, HttpServletResponse, LocaleContext)`, using a `SimpleLocaleContext`.
`void`
`setLocaleContext(HttpServletRequest request, HttpServletResponse response, org.springframework.context.i18n.LocaleContext localeContext)`
Set the current locale context to the given one, potentially including a locale with associated time zone information.

## Method Details

### resolveLocaleContext

org.springframework.context.i18n.LocaleContext resolveLocaleContext(HttpServletRequest request)
Resolve the current locale context via the given request.

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

The returned context may be a `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` - the request to resolve the locale context for
Returns:
  the current locale context (never `null`
See Also:
  - `resolveLocale(HttpServletRequest)`
  - `RequestContextUtils.getLocale(jakarta.servlet.http.HttpServletRequest)`
  - `RequestContextUtils.getTimeZone(jakarta.servlet.http.HttpServletRequest)`

### setLocaleContext

void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable org.springframework.context.i18n.LocaleContext localeContext)
Set the current locale context to the given one, potentially including a locale with associated time zone information.

Parameters:
  `request` - the request to be used for locale modification
  `response` - the response to be used for locale modification
  `localeContext` - the new locale context, or `null` to clear the locale
Throws:
  `UnsupportedOperationException` - if the LocaleResolver implementation does not support dynamic changing of the locale or time zone
See Also:
  - `setLocale(HttpServletRequest, HttpServletResponse, Locale)`
  - `SimpleLocaleContext`
  - `SimpleTimeZoneAwareLocaleContext`

### resolveLocale

default Locale resolveLocale(HttpServletRequest request)
Default implementation of `LocaleResolver.resolveLocale(HttpServletRequest)` that delegates to `resolveLocaleContext(HttpServletRequest)`, falling back to `ServletRequest.getLocale()` if necessary.

Specified by:
  `resolveLocale` in interface `LocaleResolver`
Parameters:
  `request` - the request to resolve the locale for
Returns:
  the current locale (never `null`)
Since:
  6.0

### setLocale

default void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale)
Default implementation of `LocaleResolver.setLocale(HttpServletRequest, HttpServletResponse, Locale)` that delegates to `setLocaleContext(HttpServletRequest, HttpServletResponse, LocaleContext)`, using a `SimpleLocaleContext`.

Specified by:
  `setLocale` in interface `LocaleResolver`
Parameters:
  `request` - the request to be used for locale modification
  `response` - the response to be used for locale modification
  `locale` - the new locale, or `null` to clear the locale
Throws:
  `UnsupportedOperationException` - if the LocaleResolver implementation does not support dynamic changing of the locale
Since:
  6.0