Update LocaleContextResolver to implement LocaleResolver

This commit updates LocaleContextResolver to implement LocaleResolver
using default methods, which simplifies AbstractLocaleContextResolver
and aligns it more closely with AbstractLocaleResolver.

See gh-27609
This commit is contained in:
Vedran Pavic 2021-10-25 23:06:50 +02:00 committed by Sam Brannen
parent 9a0eb8db28
commit 94cc2da33b
2 changed files with 15 additions and 20 deletions

View File

@ -22,12 +22,16 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.lang.Nullable;
/**
* Extension of {@link LocaleResolver} that adds support for a rich locale context
* (potentially including locale and time zone information).
*
* <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale},
* delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
*
* @author Juergen Hoeller
* @since 4.0
* @see org.springframework.context.i18n.LocaleContext
@ -73,4 +77,15 @@ public interface LocaleContextResolver extends LocaleResolver {
void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
@Nullable LocaleContext localeContext);
@Override
default Locale resolveLocale(HttpServletRequest request) {
Locale locale = resolveLocaleContext(request).getLocale();
return (locale != null ? locale : request.getLocale());
}
@Override
default void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));
}
}

View File

@ -16,13 +16,8 @@
package org.springframework.web.servlet.i18n;
import java.util.Locale;
import java.util.TimeZone;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleContextResolver;
@ -32,9 +27,6 @@ import org.springframework.web.servlet.LocaleContextResolver;
* <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},
* delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
*
* @author Juergen Hoeller
* @since 4.0
* @see #setDefaultLocale
@ -63,16 +55,4 @@ public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolv
return this.defaultTimeZone;
}
@Override
public Locale resolveLocale(HttpServletRequest request) {
Locale locale = resolveLocaleContext(request).getLocale();
return (locale != null ? locale : request.getLocale());
}
@Override
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));
}
}