public class AcceptHeaderLocaleResolver extends java.lang.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.
javax.servlet.http.HttpServletRequest#getLocale()
Modifier and Type | Field and Description |
---|---|
private java.util.Locale |
defaultLocale |
private java.util.List<java.util.Locale> |
supportedLocales |
Constructor and Description |
---|
AcceptHeaderLocaleResolver() |
Modifier and Type | Method and Description |
---|---|
private java.util.Locale |
findSupportedLocale(HttpServletRequest request,
java.util.Locale fallback) |
java.util.Locale |
getDefaultLocale()
The configured default locale, if any.
|
java.util.List<java.util.Locale> |
getSupportedLocales()
Return the configured list of supported locales.
|
private boolean |
isSupportedLocale(java.util.Locale locale) |
java.util.Locale |
resolveLocale(HttpServletRequest request)
Resolve the current locale via the given request.
|
void |
setDefaultLocale(java.util.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,
java.util.Locale locale)
Set the current locale to the given one.
|
void |
setSupportedLocales(java.util.List<java.util.Locale> locales)
Configure supported locales to check against the requested locales
determined via
HttpServletRequest#getLocales() . |
private final java.util.List<java.util.Locale> supportedLocales
private java.util.Locale defaultLocale
public void setSupportedLocales(java.util.List<java.util.Locale> locales)
HttpServletRequest#getLocales()
. If this is not
configured then HttpServletRequest#getLocale()
is used instead.locales
- the supported localespublic java.util.List<java.util.Locale> getSupportedLocales()
public void setDefaultLocale(java.util.Locale defaultLocale)
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
HttpServletRequest#getLocale()
.
defaultLocale
- the default locale to usepublic java.util.Locale getDefaultLocale()
public java.util.Locale resolveLocale(HttpServletRequest request)
LocaleResolver
resolveLocale
in interface LocaleResolver
request
- the request to resolve the locale fornull
)private boolean isSupportedLocale(java.util.Locale locale)
private java.util.Locale findSupportedLocale(HttpServletRequest request, java.util.Locale fallback)
public void setLocale(HttpServletRequest request, HttpServletResponse response, java.util.Locale locale)
LocaleResolver
setLocale
in interface LocaleResolver
request
- the request to be used for locale modificationresponse
- the response to be used for locale modificationlocale
- the new locale, or null
to clear the locale