Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.springframework.web.servlet.FlashMapManager;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
Expand Down Expand Up @@ -335,9 +335,9 @@ ControllerSpec mappedInterceptors(
/**
* Provide the LocaleResolver to use.
* <p>This is delegated to
* {@link StandaloneMockMvcBuilder#setLocaleResolver(LocaleResolver)}.
* {@link StandaloneMockMvcBuilder#setLocaleResolver(LocaleContextResolver)}.
*/
ControllerSpec localeResolver(LocaleResolver localeResolver);
ControllerSpec localeResolver(LocaleContextResolver localeResolver);

/**
* Provide a custom FlashMapManager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.web.servlet.FlashMapManager;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
Expand Down Expand Up @@ -135,7 +135,7 @@ public StandaloneMockMvcSpec singleView(View view) {
}

@Override
public StandaloneMockMvcSpec localeResolver(LocaleResolver localeResolver) {
public StandaloneMockMvcSpec localeResolver(LocaleContextResolver localeResolver) {
this.mockMvcBuilder.setLocaleResolver(localeResolver);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.springframework.web.servlet.FlashMapManager;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
Expand Down Expand Up @@ -121,7 +121,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
@Nullable
private List<ViewResolver> viewResolvers;

private LocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
private LocaleContextResolver localeResolver = new AcceptHeaderLocaleResolver();

@Nullable
private FlashMapManager flashMapManager;
Expand Down Expand Up @@ -297,7 +297,7 @@ public StandaloneMockMvcBuilder setSingleView(View view) {
* Provide a LocaleResolver instance.
* If not provided, the default one used is {@link AcceptHeaderLocaleResolver}.
*/
public StandaloneMockMvcBuilder setLocaleResolver(LocaleResolver localeResolver) {
public StandaloneMockMvcBuilder setLocaleResolver(LocaleContextResolver localeResolver) {
this.localeResolver = localeResolver;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
* implementation for Servlet 3 is included. The MultipartResolver bean name is
* "multipartResolver"; default is none.
*
* <li>Its locale resolution strategy is determined by a {@link LocaleResolver}.
* <li>Its locale resolution strategy is determined by a {@link LocaleContextResolver}.
* Out-of-the-box implementations work via HTTP accept header, cookie, or session.
* The LocaleResolver bean name is "localeResolver"; default is
* {@link org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver}.
Expand Down Expand Up @@ -156,6 +156,7 @@
* @author Chris Beams
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @author Vedran Pavic
* @see org.springframework.web.HttpRequestHandler
* @see org.springframework.web.servlet.mvc.Controller
* @see org.springframework.web.context.ContextLoaderListener
Expand Down Expand Up @@ -313,9 +314,9 @@ public class DispatcherServlet extends FrameworkServlet {
@Nullable
private MultipartResolver multipartResolver;

/** LocaleResolver used by this servlet. */
/** LocaleContextResolver used by this servlet. */
@Nullable
private LocaleResolver localeResolver;
private LocaleContextResolver localeResolver;

/** ThemeResolver used by this servlet. */
@Nullable
Expand Down Expand Up @@ -543,7 +544,7 @@ else if (logger.isDebugEnabled()) {
*/
private void initLocaleResolver(ApplicationContext context) {
try {
this.localeResolver = context.getBean(LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class);
this.localeResolver = context.getBean(LOCALE_RESOLVER_BEAN_NAME, LocaleContextResolver.class);
if (logger.isTraceEnabled()) {
logger.trace("Detected " + this.localeResolver);
}
Expand All @@ -553,7 +554,7 @@ else if (logger.isDebugEnabled()) {
}
catch (NoSuchBeanDefinitionException ex) {
// We need to use the default.
this.localeResolver = getDefaultStrategy(context, LocaleResolver.class);
this.localeResolver = getDefaultStrategy(context, LocaleContextResolver.class);
if (logger.isTraceEnabled()) {
logger.trace("No LocaleResolver '" + LOCALE_RESOLVER_BEAN_NAME +
"': using default [" + this.localeResolver.getClass().getSimpleName() + "]");
Expand Down Expand Up @@ -1186,13 +1187,8 @@ private void processDispatchResult(HttpServletRequest request, HttpServletRespon
*/
@Override
protected LocaleContext buildLocaleContext(final HttpServletRequest request) {
LocaleResolver lr = this.localeResolver;
if (lr instanceof LocaleContextResolver) {
return ((LocaleContextResolver) lr).resolveLocaleContext(request);
}
else {
return () -> (lr != null ? lr.resolveLocale(request) : request.getLocale());
}
LocaleContextResolver lr = this.localeResolver;
return (lr != null ? lr.resolveLocaleContext(request) : request::getLocale);
}

/**
Expand Down Expand Up @@ -1379,8 +1375,13 @@ else if (logger.isDebugEnabled()) {
*/
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine locale for request and apply it to the response.
Locale locale =
(this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
Locale locale = null;
if (this.localeResolver != null) {
locale = this.localeResolver.resolveLocaleContext(request).getLocale();
}
if (locale == null) {
locale = request.getLocale();
}
response.setLocale(locale);

View view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @see org.springframework.web.servlet.support.RequestContext#getTimeZone
* @see org.springframework.web.servlet.support.RequestContextUtils#getTimeZone
*/
@SuppressWarnings("deprecation")
public interface LocaleContextResolver extends LocaleResolver {

/**
Expand All @@ -58,7 +59,6 @@ public interface LocaleContextResolver extends LocaleResolver {
* the returned context, which again can be accessed through downcasting.
* @param request the request to resolve the locale context for
* @return the current locale context (never {@code null}
* @see #resolveLocale(HttpServletRequest)
* @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
* @see org.springframework.web.servlet.support.RequestContextUtils#getTimeZone
*/
Expand All @@ -72,7 +72,6 @@ public interface LocaleContextResolver extends LocaleResolver {
* @param localeContext the new locale context, or {@code null} to clear the locale
* @throws UnsupportedOperationException if the LocaleResolver implementation
* does not support dynamic changing of the locale or time zone
* @see #setLocale(HttpServletRequest, HttpServletResponse, Locale)
* @see org.springframework.context.i18n.SimpleLocaleContext
* @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
* @see org.springframework.context.i18n.LocaleContextHolder
* @see org.springframework.web.servlet.support.RequestContext#getLocale
* @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
* @deprecated since 6.0 in favor of {@link LocaleContextResolver}
*/
@Deprecated(since = "6.0")
public interface LocaleResolver {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.servlet.RequestToViewNameTranslator;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.function.support.HandlerFunctionAdapter;
Expand Down Expand Up @@ -1158,7 +1158,7 @@ public HandlerMappingIntrospector mvcHandlerMappingIntrospector() {
}

@Bean
public LocaleResolver localeResolver() {
public LocaleContextResolver localeResolver() {
return new AcceptHeaderLocaleResolver();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,12 +28,12 @@
* {@link ServletWebRequest} subclass that is aware of
* {@link org.springframework.web.servlet.DispatcherServlet}'s
* request context, such as the Locale determined by the configured
* {@link org.springframework.web.servlet.LocaleResolver}.
* {@link org.springframework.web.servlet.LocaleContextResolver}.
*
* @author Juergen Hoeller
* @since 2.0
* @see #getLocale()
* @see org.springframework.web.servlet.LocaleResolver
* @see org.springframework.web.servlet.LocaleContextResolver
*/
public class DispatcherServletWebRequest extends ServletWebRequest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.servlet.i18n;

import java.util.Locale;
import java.util.TimeZone;

import org.springframework.lang.Nullable;
Expand All @@ -32,12 +33,32 @@
* @see #setDefaultLocale
* @see #setDefaultTimeZone
*/
public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolver implements LocaleContextResolver {
public abstract class AbstractLocaleContextResolver implements LocaleContextResolver {

@Nullable
private Locale defaultLocale;

@Nullable
private TimeZone defaultTimeZone;


/**
* Set a default {@link Locale} that this resolver will return if no other
* locale is found.
*/
public void setDefaultLocale(@Nullable Locale defaultLocale) {
this.defaultLocale = defaultLocale;
}

/**
* Get the default {@link Locale} that this resolver is supposed to fall back
* to, if any.
*/
@Nullable
protected Locale getDefaultLocale() {
return this.defaultLocale;
}

/**
* Set a default {@link TimeZone} that this resolver will return if no other
* time zone is found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
* @author Juergen Hoeller
* @since 1.2.9
* @see #setDefaultLocale
* @deprecated since 6.0 in favor of {@link AbstractLocaleContextResolver}
*/
@Deprecated(since = "6.0")
public abstract class AbstractLocaleResolver implements LocaleResolver {

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@
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;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.LocaleContextResolver;

/**
* {@link LocaleResolver} implementation that simply uses the primary locale
* {@link LocaleContextResolver} implementation that simply uses the primary locale
* 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).
*
* <p>Note: Does not support {@link #setLocale} since the {@code Accept-Language}
* <p>Note: Does not support {@link #setLocaleContext} since the {@code Accept-Language}
* header can only be changed by changing the client's locale settings.
*
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @since 27.02.2003
* @see jakarta.servlet.http.HttpServletRequest#getLocale()
*/
public class AcceptHeaderLocaleResolver extends AbstractLocaleResolver {
public class AcceptHeaderLocaleResolver extends AbstractLocaleContextResolver {

private final List<Locale> supportedLocales = new ArrayList<>(4);

Expand All @@ -68,21 +70,21 @@ public List<Locale> getSupportedLocales() {


@Override
public Locale resolveLocale(HttpServletRequest request) {
public LocaleContext resolveLocaleContext(HttpServletRequest request) {
Locale defaultLocale = getDefaultLocale();
if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
return defaultLocale;
return new SimpleLocaleContext(defaultLocale);
}
Locale requestLocale = request.getLocale();
List<Locale> supportedLocales = getSupportedLocales();
if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
return requestLocale;
return new SimpleLocaleContext(requestLocale);
}
Locale supportedLocale = findSupportedLocale(request, supportedLocales);
if (supportedLocale != null) {
return supportedLocale;
return new SimpleLocaleContext(supportedLocale);
}
return (defaultLocale != null ? defaultLocale : requestLocale);
return new SimpleLocaleContext(defaultLocale != null ? defaultLocale : requestLocale);
}

@Nullable
Expand Down Expand Up @@ -112,7 +114,8 @@ else if (languageMatch == null) {
}

@Override
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
@Nullable LocaleContext localeContext) {
throw new UnsupportedOperationException(
"Cannot change HTTP Accept-Language header - use a different locale resolution strategy");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.util.WebUtils;

/**
* {@link LocaleResolver} implementation that uses a cookie sent back to the user
* {@link LocaleContextResolver} implementation that uses a cookie sent back to the user
* in case of a custom setting, with a fallback to the configured default locale,
* the request's {@code Accept-Language} header, or the default locale for the server.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
import org.springframework.lang.Nullable;

/**
* {@link org.springframework.web.servlet.LocaleResolver} implementation
* {@link org.springframework.web.servlet.LocaleContextResolver} implementation
* that always returns a fixed default locale and optionally time zone.
* Default is the current JVM's default locale.
*
Expand Down
Loading