Polish LocaleResolver support

This commit is contained in:
Sam Brannen 2022-06-03 13:10:55 +02:00
parent cda1e5507c
commit aab9da0366
9 changed files with 77 additions and 65 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,9 +37,9 @@ public class SimpleLocaleContext implements LocaleContext {
/** /**
* Create a new SimpleLocaleContext that exposes the specified Locale. * Create a new {@code SimpleLocaleContext} that exposes the specified {@link Locale}.
* Every {@link #getLocale()} call will return this Locale. * <p>Every {@link #getLocale()} call will return this locale.
* @param locale the Locale to expose, or {@code null} for no specific one * @param locale the {@code Locale} to expose, or {@code null} for no specific one
*/ */
public SimpleLocaleContext(@Nullable Locale locale) { public SimpleLocaleContext(@Nullable Locale locale) {
this.locale = locale; this.locale = locale;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ import org.springframework.context.i18n.LocaleContext;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
* Extension of {@link LocaleResolver}, adding support for a rich locale context * Extension of {@link LocaleResolver} that adds support for a rich locale context
* (potentially including locale and time zone information). * (potentially including locale and time zone information).
* *
* @author Juergen Hoeller * @author Juergen Hoeller

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -55,7 +55,7 @@ public interface LocaleResolver {
/** /**
* Resolve the current locale via the given request. * Resolve the current locale via the given request.
* Can return a default locale as fallback in any case. * <p>Can return a default locale as fallback in any case.
* @param request the request to resolve the locale for * @param request the request to resolve the locale for
* @return the current locale (never {@code null}) * @return the current locale (never {@code null})
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,9 @@ import org.springframework.web.servlet.LocaleContextResolver;
/** /**
* Abstract base class for {@link LocaleContextResolver} implementations. * Abstract base class for {@link LocaleContextResolver} implementations.
* Provides support for a default locale and a default time zone. *
* <p>Provides support for a {@linkplain #setDefaultLocale(Locale) default locale}
* and a {@linkplain #setDefaultTimeZone(TimeZone) default time zone}.
* *
* <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale}, * <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale},
* delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}. * delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
@ -45,14 +47,16 @@ public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolv
/** /**
* Set a default TimeZone that this resolver will return if no other time zone found. * Set a default {@link TimeZone} that this resolver will return if no other
* time zone is found.
*/ */
public void setDefaultTimeZone(@Nullable TimeZone defaultTimeZone) { public void setDefaultTimeZone(@Nullable TimeZone defaultTimeZone) {
this.defaultTimeZone = defaultTimeZone; this.defaultTimeZone = defaultTimeZone;
} }
/** /**
* Return the default TimeZone that this resolver is supposed to fall back to, if any. * Get the default {@link TimeZone} that this resolver is supposed to fall
* back to, if any.
*/ */
@Nullable @Nullable
public TimeZone getDefaultTimeZone() { public TimeZone getDefaultTimeZone() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,7 +23,8 @@ import org.springframework.web.servlet.LocaleResolver;
/** /**
* Abstract base class for {@link LocaleResolver} implementations. * Abstract base class for {@link LocaleResolver} implementations.
* Provides support for a default locale. *
* <p>Provides support for a {@linkplain #setDefaultLocale(Locale) default locale}.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 1.2.9 * @since 1.2.9
@ -36,14 +37,16 @@ public abstract class AbstractLocaleResolver implements LocaleResolver {
/** /**
* Set a default Locale that this resolver will return if no other locale found. * Set a default {@link Locale} that this resolver will return if no other
* locale is found.
*/ */
public void setDefaultLocale(@Nullable Locale defaultLocale) { public void setDefaultLocale(@Nullable Locale defaultLocale) {
this.defaultLocale = defaultLocale; this.defaultLocale = defaultLocale;
} }
/** /**
* Return the default Locale that this resolver is supposed to fall back to, if any. * Get the default {@link Locale} that this resolver is supposed to fall back
* to, if any.
*/ */
@Nullable @Nullable
protected Locale getDefaultLocale() { protected Locale getDefaultLocale() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,11 +30,11 @@ import org.springframework.web.servlet.LocaleResolver;
/** /**
* {@link LocaleResolver} implementation that simply uses the primary locale * {@link LocaleResolver} implementation that simply uses the primary locale
* specified in the "accept-language" header of the HTTP request (that is, * specified in the {@code Accept-Language} header of the HTTP request (that is,
* the locale sent by the client browser, normally that of the client's OS). * the locale sent by the client browser, normally that of the client's OS).
* *
* <p>Note: Does not support {@code setLocale}, since the accept header * <p>Note: Does not support {@link #setLocale} since the {@code Accept-Language}
* can only be changed through changing the client's locale settings. * header can only be changed by changing the client's locale settings.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -62,7 +62,7 @@ public class AcceptHeaderLocaleResolver implements LocaleResolver {
} }
/** /**
* Return the configured list of supported locales. * Get the configured list of supported locales.
* @since 4.3 * @since 4.3
*/ */
public List<Locale> getSupportedLocales() { public List<Locale> getSupportedLocales() {
@ -140,7 +140,7 @@ public class AcceptHeaderLocaleResolver implements LocaleResolver {
@Override @Override
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) { public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Cannot change HTTP accept header - use a different locale resolution strategy"); "Cannot change HTTP Accept-Language header - use a different locale resolution strategy");
} }
} }

View File

@ -36,8 +36,8 @@ import org.springframework.web.util.WebUtils;
/** /**
* {@link LocaleResolver} implementation that uses a cookie sent back to the user * {@link LocaleResolver} implementation that uses a cookie sent back to the user
* in case of a custom setting, with a fallback to the specified default locale * in case of a custom setting, with a fallback to the configured default locale,
* or the request's accept-header locale. * the request's {@code Accept-Language} header, or the default locale for the server.
* *
* <p>This is particularly useful for stateless applications without user sessions. * <p>This is particularly useful for stateless applications without user sessions.
* The cookie may optionally contain an associated time zone value as well; * The cookie may optionally contain an associated time zone value as well;
@ -96,8 +96,8 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
/** /**
* Create a new instance of the {@link CookieLocaleResolver} class * Create a new instance of {@link CookieLocaleResolver} using the
* using the {@link #DEFAULT_COOKIE_NAME default cookie name}. * {@linkplain #DEFAULT_COOKIE_NAME default cookie name}.
*/ */
public CookieLocaleResolver() { public CookieLocaleResolver() {
setCookieName(DEFAULT_COOKIE_NAME); setCookieName(DEFAULT_COOKIE_NAME);
@ -153,14 +153,14 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
} }
/** /**
* Set a fixed locale that this resolver will return if no cookie found. * Set a fixed locale that this resolver will return if no cookie is found.
*/ */
public void setDefaultLocale(@Nullable Locale defaultLocale) { public void setDefaultLocale(@Nullable Locale defaultLocale) {
this.defaultLocale = defaultLocale; this.defaultLocale = defaultLocale;
} }
/** /**
* Return the fixed locale that this resolver will return if no cookie found, * Return the fixed locale that this resolver will return if no cookie is found,
* if any. * if any.
*/ */
@Nullable @Nullable
@ -169,7 +169,7 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
} }
/** /**
* Set a fixed time zone that this resolver will return if no cookie found. * Set a fixed time zone that this resolver will return if no cookie is found.
* @since 4.0 * @since 4.0
*/ */
public void setDefaultTimeZone(@Nullable TimeZone defaultTimeZone) { public void setDefaultTimeZone(@Nullable TimeZone defaultTimeZone) {
@ -177,7 +177,7 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
} }
/** /**
* Return the fixed time zone that this resolver will return if no cookie found, * Return the fixed time zone that this resolver will return if no cookie is found,
* if any. * if any.
* @since 4.0 * @since 4.0
*/ */
@ -326,10 +326,11 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
} }
/** /**
* Determine the default locale for the given request, * Determine the default locale for the given request, called if no locale
* Called if no locale cookie has been found. * cookie has been found.
* <p>The default implementation returns the specified default locale, * <p>The default implementation returns the configured default locale, if any,
* if any, else falls back to the request's accept-header locale. * and otherwise falls back to the request's {@code Accept-Language} header
* locale or the default locale for the server.
* @param request the request to resolve the locale for * @param request the request to resolve the locale for
* @return the default locale (never {@code null}) * @return the default locale (never {@code null})
* @see #setDefaultLocale * @see #setDefaultLocale
@ -344,9 +345,9 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
} }
/** /**
* Determine the default time zone for the given request, * Determine the default time zone for the given request, called if no locale
* Called if no time zone cookie has been found. * cookie has been found.
* <p>The default implementation returns the specified default time zone, * <p>The default implementation returns the configured default time zone,
* if any, or {@code null} otherwise. * if any, or {@code null} otherwise.
* @param request the request to resolve the time zone for * @param request the request to resolve the time zone for
* @return the default time zone (or {@code null} if none defined) * @return the default time zone (or {@code null} if none defined)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,8 +30,8 @@ import org.springframework.web.util.WebUtils;
/** /**
* {@link org.springframework.web.servlet.LocaleResolver} implementation that * {@link org.springframework.web.servlet.LocaleResolver} implementation that
* uses a locale attribute in the user's session in case of a custom setting, * uses a locale attribute in the user's session in case of a custom setting,
* with a fallback to the specified default locale or the request's * with a fallback to the configured default locale, the request's
* accept-header locale. * {@code Accept-Language} header, or the default locale for the server.
* *
* <p>This is most appropriate if the application needs user sessions anyway, * <p>This is most appropriate if the application needs user sessions anyway,
* i.e. when the {@code HttpSession} does not have to be created just for storing * i.e. when the {@code HttpSession} does not have to be created just for storing
@ -61,8 +61,8 @@ import org.springframework.web.util.WebUtils;
public class SessionLocaleResolver extends AbstractLocaleContextResolver { public class SessionLocaleResolver extends AbstractLocaleContextResolver {
/** /**
* Name of the session attribute that holds the Locale. * Default name of the session attribute that holds the Locale.
* Only used internally by this implementation. * <p>Only used internally by this implementation.
* <p>Use {@code RequestContext(Utils).getLocale()} * <p>Use {@code RequestContext(Utils).getLocale()}
* to retrieve the current locale in controllers or views. * to retrieve the current locale in controllers or views.
* @see org.springframework.web.servlet.support.RequestContext#getLocale * @see org.springframework.web.servlet.support.RequestContext#getLocale
@ -71,8 +71,8 @@ public class SessionLocaleResolver extends AbstractLocaleContextResolver {
public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE"; public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";
/** /**
* Name of the session attribute that holds the TimeZone. * Default name of the session attribute that holds the TimeZone.
* Only used internally by this implementation. * <p>Only used internally by this implementation.
* <p>Use {@code RequestContext(Utils).getTimeZone()} * <p>Use {@code RequestContext(Utils).getTimeZone()}
* to retrieve the current time zone in controllers or views. * to retrieve the current time zone in controllers or views.
* @see org.springframework.web.servlet.support.RequestContext#getTimeZone * @see org.springframework.web.servlet.support.RequestContext#getTimeZone
@ -157,10 +157,12 @@ public class SessionLocaleResolver extends AbstractLocaleContextResolver {
/** /**
* Determine the default locale for the given request, * Determine the default locale for the given request, called if no
* Called if no Locale session attribute has been found. * {@link Locale} session attribute has been found.
* <p>The default implementation returns the specified default locale, * <p>The default implementation returns the configured
* if any, else falls back to the request's accept-header locale. * {@linkplain #setDefaultLocale(Locale) default locale}, if any, and otherwise
* falls back to the request's {@code Accept-Language} header locale or the
* default locale for the server.
* @param request the request to resolve the locale for * @param request the request to resolve the locale for
* @return the default locale (never {@code null}) * @return the default locale (never {@code null})
* @see #setDefaultLocale * @see #setDefaultLocale
@ -175,9 +177,9 @@ public class SessionLocaleResolver extends AbstractLocaleContextResolver {
} }
/** /**
* Determine the default time zone for the given request, * Determine the default time zone for the given request, called if no
* Called if no TimeZone session attribute has been found. * {@link TimeZone} session attribute has been found.
* <p>The default implementation returns the specified default time zone, * <p>The default implementation returns the configured default time zone,
* if any, or {@code null} otherwise. * if any, or {@code null} otherwise.
* @param request the request to resolve the time zone for * @param request the request to resolve the time zone for
* @return the default time zone (or {@code null} if none defined) * @return the default time zone (or {@code null} if none defined)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,7 +60,7 @@ import org.springframework.web.util.UriComponentsBuilder;
public abstract class RequestContextUtils { public abstract class RequestContextUtils {
/** /**
* The name of the bean to use to look up in an implementation of * The name of the bean to use to determine if an implementation of
* {@link RequestDataValueProcessor} has been configured. * {@link RequestDataValueProcessor} has been configured.
* @since 4.2.1 * @since 4.2.1
*/ */
@ -121,10 +121,10 @@ public abstract class RequestContextUtils {
} }
/** /**
* Return the LocaleResolver that has been bound to the request by the * Return the {@link LocaleResolver} that has been bound to the request by the
* DispatcherServlet. * {@link DispatcherServlet}.
* @param request current HTTP request * @param request current HTTP request
* @return the current LocaleResolver, or {@code null} if not found * @return the current {@code LocaleResolver}, or {@code null} if not found
*/ */
@Nullable @Nullable
public static LocaleResolver getLocaleResolver(HttpServletRequest request) { public static LocaleResolver getLocaleResolver(HttpServletRequest request) {
@ -133,8 +133,9 @@ public abstract class RequestContextUtils {
/** /**
* Retrieve the current locale from the given request, using the * Retrieve the current locale from the given request, using the
* LocaleResolver bound to the request by the DispatcherServlet * {@link LocaleResolver} bound to the request by the {@link DispatcherServlet}
* (if available), falling back to the request's accept-header Locale. * (if available), falling back to the request's locale based on the
* {@code Accept-Language} header or the default locale for the server.
* <p>This method serves as a straightforward alternative to the standard * <p>This method serves as a straightforward alternative to the standard
* Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method, * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method,
* falling back to the latter if no more specific locale has been found. * falling back to the latter if no more specific locale has been found.
@ -153,18 +154,19 @@ public abstract class RequestContextUtils {
/** /**
* Retrieve the current time zone from the given request, using the * Retrieve the current time zone from the given request, using the
* TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet * {@link TimeZoneAwareLocaleContext} in the {@link LocaleResolver} bound to
* (if available), falling back to the system's default time zone. * the request by the {@link DispatcherServlet} (if available).
* <p>Note: This method returns {@code null} if no specific time zone can be * <p>Note: This method returns {@code null} if no specific time zone can be
* resolved for the given request. This is in contrast to {@link #getLocale} * resolved for the given request. This is in contrast to {@link #getLocale}
* where there is always the request's accept-header locale to fall back to. * where it is always possible to fall back to the request's locale based on the
* {@code Accept-Language} header or the default locale for the server.
* <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()} * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()}
* which will normally be populated with the same TimeZone: That method only * which will normally be populated with the same {@code TimeZone}: that method only
* differs in terms of its fallback to the system time zone if the LocaleResolver * differs in terms of its fallback to the system time zone if the {@code LocaleResolver}
* hasn't provided a specific time zone (instead of this method's {@code null}). * hasn't provided a specific time zone (instead of this method's {@code null}).
* @param request current HTTP request * @param request current HTTP request
* @return the current time zone for the given request, either from the * @return the current time zone for the given request, either from the
* TimeZoneAwareLocaleResolver or {@code null} if none associated * {@code TimeZoneAwareLocaleContext} or {@code null} if none associated
* @see #getLocaleResolver * @see #getLocaleResolver
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone() * @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
*/ */
@ -203,7 +205,7 @@ public abstract class RequestContextUtils {
} }
/** /**
* Retrieves the current theme from the given request, using the ThemeResolver * Retrieve the current theme from the given request, using the ThemeResolver
* and ThemeSource bound to the request by the DispatcherServlet. * and ThemeSource bound to the request by the DispatcherServlet.
* @param request current HTTP request * @param request current HTTP request
* @return the current theme, or {@code null} if not found * @return the current theme, or {@code null} if not found