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

public Class AcceptHeaderLocaleContextResolver

extends Object
implements LocaleContextResolver
Class Inheritance
All Implemented Interfaces
org.springframework.web.server.i18n.LocaleContextResolver
Imports
java.util.ArrayList, .List, .Locale, org.springframework.context.i18n.LocaleContext, .SimpleLocaleContext, org.springframework.http.HttpHeaders, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils, .StringUtils, org.springframework.web.server.ServerWebExchange

LocaleContextResolver implementation that looks for a match between locales in the Accept-Language header and a list of configured supported locales.

See setSupportedLocales(List) for further details on how supported and requested locales are matched.

Note

Does not support setLocaleContext, since the accept header can only be changed through changing the client's locale settings.

Authors
Sebastien Deleuze, Juergen Hoeller
Since
5.0
See Also
HttpHeaders#getAcceptLanguageAsLocales()

Field Summary

Modifier and TypeField and Description
private Locale
private final List<Locale>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Locale
getDefaultLocale()

The configured default locale, if any.

public List<Locale>
getSupportedLocales()

Return the configured list of supported locales.

public LocaleContext
resolveLocaleContext(ServerWebExchange
current server exchange
exchange
)

Implements org.springframework.web.server.i18n.LocaleContextResolver.resolveLocaleContext.

Resolve the current locale context via the given exchange.
private Locale
public void
setDefaultLocale(Locale
the default locale to use
defaultLocale
)

Configure a fixed default locale to fall back on if the request does not have an "Accept-Language" header (not set by default).

public void
setLocaleContext(ServerWebExchange
current server exchange
exchange
,
LocaleContext
the new locale context, or null to clear the locale
locale
)

Implements org.springframework.web.server.i18n.LocaleContextResolver.setLocaleContext.

Set the current locale context to the given one, potentially including a locale with associated time zone information.
public void
setSupportedLocales(List<Locale>
the supported locales
locales
)

Configure the list of supported locales to compare and match against requested locales.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultLocaleback to summary
private Locale defaultLocale
Annotations
@Nullable
supportedLocalesback to summary
private final List<Locale> supportedLocales

Constructor Detail

AcceptHeaderLocaleContextResolverback to summary
public AcceptHeaderLocaleContextResolver()

Method Detail

getDefaultLocaleback to summary
public Locale getDefaultLocale()

The configured default locale, if any.

This method may be overridden in subclasses.

Annotations
@Nullable
getSupportedLocalesback to summary
public List<Locale> getSupportedLocales()

Return the configured list of supported locales.

resolveLocaleContextback to summary
public LocaleContext resolveLocaleContext(ServerWebExchange exchange)

Implements org.springframework.web.server.i18n.LocaleContextResolver.resolveLocaleContext.

Doc from org.springframework.web.server.i18n.LocaleContextResolver.resolveLocaleContext.

Resolve the current locale context via the given exchange.

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
exchange:ServerWebExchange

current server exchange

Returns:LocaleContext

the current locale context (never null)

Annotations
@Override
resolveSupportedLocaleback to summary
private Locale resolveSupportedLocale(List<Locale> requestLocales)
Annotations
@Nullable
setDefaultLocaleback to summary
public void setDefaultLocale(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:Locale

the default locale to use

setLocaleContextback to summary
public void setLocaleContext(ServerWebExchange exchange, LocaleContext locale)

Implements org.springframework.web.server.i18n.LocaleContextResolver.setLocaleContext.

Doc from org.springframework.web.server.i18n.LocaleContextResolver.setLocaleContext.

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

Parameters
exchange:ServerWebExchange

current server exchange

locale:LocaleContext

the new locale context, or null to clear the locale

Annotations
@Override
setSupportedLocalesback to summary
public void setSupportedLocales(List<Locale> locales)

Configure the list of supported locales to compare and match against requested locales.

In order for a supported locale to be considered a match, it must match on both country and language. If you want to support a language-only match as a fallback, you must configure the language explicitly as a supported locale.

For example, if the supported locales are ["de-DE","en-US"], then a request for "en-GB" will not match, and neither will a request for "en". If you want to support additional locales for a given language such as "en", then you must add it to the list of supported locales.

If there is no match, then the defaultLocale is used, if configured, or otherwise falling back on the first requested locale.

Parameters
locales:List<Locale>

the supported locales