Package org.springframework.web.server.i18n

# Class AcceptHeaderLocaleContextResolver

java.lang.Object
org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver

All Implemented Interfaces:
  `LocaleContextResolver`

public class AcceptHeaderLocaleContextResolver extends Object implements LocaleContextResolver
`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(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

Modifier and Type
Method
Description
`Locale`
`getDefaultLocale()`
The configured default locale, if any.
`List<Locale>`
`getSupportedLocales()`
Return the configured list of supported locales.
`org.springframework.context.i18n.LocaleContext`
`resolveLocaleContext(ServerWebExchange exchange)`
Resolve the current locale context via the given exchange.
`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).
`void`
`setLocaleContext(ServerWebExchange exchange, org.springframework.context.i18n.LocaleContext locale)`
Set the current locale context to the given one, potentially including a locale with associated time zone information.
`void`
`setSupportedLocales(List<Locale> locales)`
Configure the list of supported locales to compare and match against `requested locales`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### AcceptHeaderLocaleContextResolver

public AcceptHeaderLocaleContextResolver()

## Method Details

### setSupportedLocales

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` - 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 org.springframework.context.i18n.LocaleContext resolveLocaleContext(ServerWebExchange exchange)
Description copied from interface: `LocaleContextResolver`
Resolve 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 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.

Specified by:
  `resolveLocaleContext` in interface `LocaleContextResolver`
Parameters:
  `exchange` - current server exchange
Returns:
  the current locale context (never `null`)

### setLocaleContext

public void setLocaleContext(ServerWebExchange exchange, @Nullable org.springframework.context.i18n.LocaleContext locale)
Description copied from interface: `LocaleContextResolver`
Set the current locale context to the given one, potentially including a locale with associated time zone information.

Specified by:
  `setLocaleContext` in interface `LocaleContextResolver`
Parameters:
  `exchange` - current server exchange
  `locale` - the new locale context, or `null` to clear the locale
See Also:
  - `SimpleLocaleContext`
  - `SimpleTimeZoneAwareLocaleContext`