Class AcceptHeaderLocaleResolver
- java.lang.Object
- org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
- All Implemented Interfaces:
LocaleResolver
public class AcceptHeaderLocaleResolver extends Object implements LocaleResolver
LocaleResolver
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.- Since:
- 27.02.2003
- Author:
- Juergen Hoeller, Rossen Stoyanchev
- See Also:
ServletRequest.getLocale()
Constructor Summary
Constructors Constructor Description AcceptHeaderLocaleResolver()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Locale
getDefaultLocale()
The configured default locale, if any.List<Locale>
getSupportedLocales()
Return the configured list of supported locales.Locale
resolveLocale(HttpServletRequest request)
Resolve the current locale via the given request.void
setDefaultLocale(Locale defaultLocale)
Configure a fixed default locale to fall back on if the request does not have an "Accept-Language" header.void
setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)
Set the current locale to the given one.void
setSupportedLocales(List<Locale> locales)
Configure supported locales to check against the requested locales determined viaServletRequest.getLocales()
.
Constructor Detail
AcceptHeaderLocaleResolver
public AcceptHeaderLocaleResolver()
Method Detail
setSupportedLocales
public void setSupportedLocales(List<Locale> locales)
Configure supported locales to check against the requested locales determined viaServletRequest.getLocales()
. If this is not configured thenServletRequest.getLocale()
is used instead.- Parameters:
locales
- the supported locales- Since:
- 4.3
getSupportedLocales
public List<Locale> getSupportedLocales()
Return the configured list of supported locales.- Since:
- 4.3
setDefaultLocale
public void setDefaultLocale(Locale defaultLocale)
Configure a fixed default locale to fall back on if the request does not have an "Accept-Language" header.By default this is not set in which case when there is "Accept-Language" header, the default locale for the server is used as defined in
ServletRequest.getLocale()
.- Parameters:
defaultLocale
- the default locale to use- Since:
- 4.3
getDefaultLocale
public Locale getDefaultLocale()
The configured default locale, if any.- Since:
- 4.3
resolveLocale
public Locale resolveLocale(HttpServletRequest request)
Description copied from interface:LocaleResolver
Resolve the current locale via the given request. Can return a default locale as fallback in any case.- Specified by:
resolveLocale
in interfaceLocaleResolver
- Parameters:
request
- the request to resolve the locale for- Returns:
- the current locale (never
null
)
setLocale
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)
Description copied from interface:LocaleResolver
Set the current locale to the given one.- Specified by:
setLocale
in interfaceLocaleResolver
- Parameters:
request
- the request to be used for locale modificationresponse
- the response to be used for locale modificationlocale
- the new locale, ornull
to clear the locale