Skip to content

Commit 60f7560

Browse files
committed
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.
1 parent b6c32d1 commit 60f7560

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/LocaleContextResolver.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,12 +22,16 @@
2222
import jakarta.servlet.http.HttpServletResponse;
2323

2424
import org.springframework.context.i18n.LocaleContext;
25+
import org.springframework.context.i18n.SimpleLocaleContext;
2526
import org.springframework.lang.Nullable;
2627

2728
/**
2829
* Extension of {@link LocaleResolver}, adding support for a rich locale context
2930
* (potentially including locale and time zone information).
3031
*
32+
* <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale},
33+
* delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
34+
*
3135
* @author Juergen Hoeller
3236
* @since 4.0
3337
* @see org.springframework.context.i18n.LocaleContext
@@ -73,4 +77,15 @@ public interface LocaleContextResolver extends LocaleResolver {
7377
void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
7478
@Nullable LocaleContext localeContext);
7579

80+
@Override
81+
default Locale resolveLocale(HttpServletRequest request) {
82+
Locale locale = resolveLocaleContext(request).getLocale();
83+
return (locale != null ? locale : request.getLocale());
84+
}
85+
86+
@Override
87+
default void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
88+
setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));
89+
}
90+
7691
}

spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/AbstractLocaleContextResolver.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,23 +16,15 @@
1616

1717
package org.springframework.web.servlet.i18n;
1818

19-
import java.util.Locale;
2019
import java.util.TimeZone;
2120

22-
import jakarta.servlet.http.HttpServletRequest;
23-
import jakarta.servlet.http.HttpServletResponse;
24-
25-
import org.springframework.context.i18n.SimpleLocaleContext;
2621
import org.springframework.lang.Nullable;
2722
import org.springframework.web.servlet.LocaleContextResolver;
2823

2924
/**
3025
* Abstract base class for {@link LocaleContextResolver} implementations.
3126
* Provides support for a default locale and a default time zone.
3227
*
33-
* <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale},
34-
* delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
35-
*
3628
* @author Juergen Hoeller
3729
* @since 4.0
3830
* @see #setDefaultLocale
@@ -59,16 +51,4 @@ public TimeZone getDefaultTimeZone() {
5951
return this.defaultTimeZone;
6052
}
6153

62-
63-
@Override
64-
public Locale resolveLocale(HttpServletRequest request) {
65-
Locale locale = resolveLocaleContext(request).getLocale();
66-
return (locale != null ? locale : request.getLocale());
67-
}
68-
69-
@Override
70-
public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
71-
setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));
72-
}
73-
7454
}

0 commit comments

Comments
 (0)