Class AcceptHeaderLocaleContextResolver
- java.lang.Object
- org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver
- All Implemented Interfaces:
LocaleContextResolver
public class AcceptHeaderLocaleContextResolver extends Object implements LocaleContextResolver
LocaleContextResolverimplementation 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
setLocaleContext(org.springframework.web.server.ServerWebExchange, org.springframework.context.i18n.LocaleContext), since the accept header can only be changed through changing the client's locale settings.- Since:
- 5.0
- Author:
- Sebastien Deleuze, Juergen Hoeller
- See Also:
HttpHeaders.getAcceptLanguageAsLocales()
Constructor Summary
Constructors Constructor Description AcceptHeaderLocaleContextResolver()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description LocalegetDefaultLocale()The configured default locale, if any.List<Locale>getSupportedLocales()Return the configured list of supported locales.LocaleContextresolveLocaleContext(ServerWebExchange exchange)Resolve the current locale context via the given exchange.voidsetDefaultLocale(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).voidsetLocaleContext(ServerWebExchange exchange, LocaleContext locale)Set the current locale context to the given one, potentially including a locale with associated time zone information.voidsetSupportedLocales(List<Locale> locales)Configure supported locales to check against the requested locales determined viaHttpHeaders.getAcceptLanguageAsLocales().
Constructor Detail
AcceptHeaderLocaleContextResolver
public AcceptHeaderLocaleContextResolver()
Method Detail
setSupportedLocales
public void setSupportedLocales(List<Locale> locales)
Configure supported locales to check against the requested locales determined viaHttpHeaders.getAcceptLanguageAsLocales().- Parameters:
locales- the supported locales
getSupportedLocales
public List<Locale> getSupportedLocales()
Return the configured list of supported locales.
setDefaultLocale
public void setDefaultLocale(@Nullable 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).- Parameters:
defaultLocale- the default locale to use
getDefaultLocale
@Nullable public Locale getDefaultLocale()
The configured default locale, if any.This method may be overridden in subclasses.
resolveLocaleContext
public LocaleContext resolveLocaleContext(ServerWebExchange exchange)
Description copied from interface:LocaleContextResolverResolve the current locale context via the given exchange.The returned context may be a
TimeZoneAwareLocaleContext, containing a locale with associated time zone information. Simply apply aninstanceofcheck and downcast accordingly.Custom resolver implementations may also return extra settings in the returned context, which again can be accessed through downcasting.
- Specified by:
resolveLocaleContextin interfaceLocaleContextResolver- Parameters:
exchange- current server exchange- Returns:
- the current locale context (never
null)
setLocaleContext
public void setLocaleContext(ServerWebExchange exchange, @Nullable LocaleContext locale)
Description copied from interface:LocaleContextResolverSet the current locale context to the given one, potentially including a locale with associated time zone information.- Specified by:
setLocaleContextin interfaceLocaleContextResolver- Parameters:
exchange- current server exchangelocale- the new locale context, ornullto clear the locale- See Also:
SimpleLocaleContext,SimpleTimeZoneAwareLocaleContext