public class AcceptHeaderLocaleContextResolver extends java.lang.Object implements LocaleContextResolver
LocaleContextResolver
implementation that simply uses the primary locale
specified in the "Accept-Language" header of the HTTP request (that is,
the locale sent by the client browser, normally that of the client's OS).
Note: Does not support setLocale
, since the accept header
can only be changed through changing the client's locale settings.
Constructor and Description |
---|
AcceptHeaderLocaleContextResolver() |
Modifier and Type | Method and Description |
---|---|
java.util.Locale |
getDefaultLocale()
The configured default locale, if any.
|
java.util.List<java.util.Locale> |
getSupportedLocales()
Return the configured list of supported locales.
|
LocaleContext |
resolveLocaleContext(ServerWebExchange exchange)
Resolve the current locale context via the given exchange.
|
void |
setDefaultLocale(java.util.Locale defaultLocale)
Configure a fixed default locale to fall back on if the request does not
have an "Accept-Language" header (not set by default).
|
void |
setLocaleContext(ServerWebExchange exchange,
LocaleContext locale)
Set the current locale context to the given one,
potentially including a locale with associated time zone information.
|
void |
setSupportedLocales(java.util.List<java.util.Locale> locales)
Configure supported locales to check against the requested locales
determined via
HttpHeaders.getAcceptLanguageAsLocales() . |
public void setSupportedLocales(@Nullable java.util.List<java.util.Locale> locales)
HttpHeaders.getAcceptLanguageAsLocales()
.locales
- the supported localespublic java.util.List<java.util.Locale> getSupportedLocales()
public void setDefaultLocale(@Nullable java.util.Locale defaultLocale)
defaultLocale
- the default locale to use@Nullable public java.util.Locale getDefaultLocale()
public LocaleContext resolveLocaleContext(ServerWebExchange exchange)
LocaleContextResolver
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.
resolveLocaleContext
in interface LocaleContextResolver
exchange
- current server exchangenull
)public void setLocaleContext(ServerWebExchange exchange, @Nullable LocaleContext locale)
LocaleContextResolver
setLocaleContext
in interface LocaleContextResolver
exchange
- current server exchangelocale
- the new locale context, or null
to clear the localeSimpleLocaleContext
,
SimpleTimeZoneAwareLocaleContext