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

public Class AcceptHeaderLocaleResolver

extends AbstractLocaleResolver
Class Inheritance
Imports
java.util.ArrayList, .Enumeration, .List, .Locale, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.lang.Nullable, org.springframework.util.StringUtils, org.springframework.web.servlet.LocaleResolver

LocaleResolver 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

This implementation does not support setLocale since the Accept-Language header can only be changed by changing the client's locale settings.

Authors
Juergen Hoeller, Rossen Stoyanchev
Since
27.02.2003
See Also
jakarta.servlet.http.HttpServletRequest#getLocale()

Field Summary

Modifier and TypeField and Description
private final List<Locale>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private Locale
findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales)

public List<Locale>
getSupportedLocales()

Get the configured list of supported locales.

public Locale
resolveLocale(HttpServletRequest
the request to resolve the locale for
request
)

Implements org.springframework.web.servlet.LocaleResolver.resolveLocale.

Resolve the current locale via the given request.

public void
setLocale(HttpServletRequest
the request to be used for locale modification
request
,
HttpServletResponse
the response to be used for locale modification
response
,
Locale
the new locale, or null to clear the locale
locale
)

Implements org.springframework.web.servlet.LocaleResolver.setLocale.

Set the current locale to the given one.

public void
setSupportedLocales(List<Locale>
the supported locales
locales
)

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

Inherited from org.springframework.web.servlet.i18n.AbstractLocaleResolver:
getDefaultLocalesetDefaultLocale

Field Detail

supportedLocalesback to summary
private final List<Locale> supportedLocales

Constructor Detail

AcceptHeaderLocaleResolverback to summary
public AcceptHeaderLocaleResolver()

Method Detail

findSupportedLocaleback to summary
private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales)
Annotations
@Nullable
getSupportedLocalesback to summary
public List<Locale> getSupportedLocales()

Get the configured list of supported locales.

Since
4.3
resolveLocaleback to summary
public Locale resolveLocale(HttpServletRequest request)

Implements org.springframework.web.servlet.LocaleResolver.resolveLocale.

Doc from org.springframework.web.servlet.LocaleResolver.resolveLocale.

Resolve the current locale via the given request.

Can return a default locale as fallback in any case.

Parameters
request:HttpServletRequest

the request to resolve the locale for

Returns:Locale

the current locale (never null)

Annotations
@Override
setLocaleback to summary
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)

Implements org.springframework.web.servlet.LocaleResolver.setLocale.

Doc from org.springframework.web.servlet.LocaleResolver.setLocale.

Set the current locale to the given one.

Parameters
request:HttpServletRequest

the request to be used for locale modification

response:HttpServletResponse

the response to be used for locale modification

locale:Locale

the new locale, 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 HttpServletRequest#getLocale().

Parameters
locales:List<Locale>

the supported locales

Since
4.3