Skip to content

Commit 8a97b05

Browse files
committed
[fit] 完善注释
1 parent c79a348 commit 8a97b05

File tree

13 files changed

+119
-69
lines changed

13 files changed

+119
-69
lines changed

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-jakarta/src/main/java/modelengine/fitframework/validation/LocaleContextMessageInterpolator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.Locale;
1616

1717
/**
18-
* 检验消息处理包装类
18+
* 检验消息处理的代理类
1919
* <p>
2020
* 从 {@link LocaleContextHolder} 中获取当前线程设置的 {@link Locale} 并委托 {@link MessageInterpolator} 去处理消息。
2121
* </p>
@@ -24,13 +24,20 @@
2424
* @since 2025-07-31
2525
*/
2626
public class LocaleContextMessageInterpolator implements MessageInterpolator {
27-
2827
private final MessageInterpolator targetInterpolator;
2928

29+
/**
30+
* 构造函数。
31+
*
32+
* @param targetInterpolator 表示目标检验消息处理对象的 {@link MessageInterpolator}。
33+
*/
3034
public LocaleContextMessageInterpolator(MessageInterpolator targetInterpolator) {
3135
this.targetInterpolator = targetInterpolator;
3236
}
3337

38+
/**
39+
* 构造函数,默认使用 {@link ParameterMessageInterpolator} 作为目标检验消息处理对象。
40+
*/
3441
public LocaleContextMessageInterpolator() {
3542
this.targetInterpolator = new ParameterMessageInterpolator();
3643
}

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-jakarta/src/test/java/modelengine/fitframework/validation/LocaleValidationController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import jakarta.validation.Valid;
1616

1717
/**
18-
* 地区验证控制器 - 测试ValidationHandler与LocaleContextMessageInterpolator的集成
18+
* 用于测试 {@link ValidationHandler} 与 {@link LocaleContextMessageInterpolator} 的集成地区验证控制器。
1919
*
2020
* @author 阮睿
2121
* @since 2025-08-01
@@ -24,13 +24,11 @@
2424
@RequestMapping(path = "/validation/locale", group = "地区验证测试接口")
2525
@Validated
2626
public class LocaleValidationController {
27-
2827
/**
29-
* 测试验证消息的地区化 - 使用简单参数
28+
* 使用简单参数测试验证消息的地区化。
3029
*
31-
* @param company 表示注解验证类 {@link Company}。
30+
* @param company 表示注解验证的测试实体类 {@link Company}。
3231
*/
3332
@PostMapping(path = "/simple", description = "测试简单参数的地区化验证消息")
3433
public void validateSimpleParam(@RequestBody @Valid Company company) {}
35-
3634
}

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-jakarta/src/test/java/modelengine/fitframework/validation/LocaleValidationControllerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import static org.assertj.core.api.Assertions.assertThat;
2727

2828
/**
29-
* {@link LocaleValidationController} 的测试集
29+
* 表示评估国际化校验的测试类
3030
*
3131
* @author 阮睿
3232
* @since 2025-08-01
3333
*/
3434
@MvcTest(classes = {LocaleValidationController.class})
3535
@DisplayName("测试地区化验证消息功能")
3636
public class LocaleValidationControllerTest {
37-
3837
@Fit
3938
private MockMvc mockMvc;
4039

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-jakarta/src/test/java/modelengine/fitframework/validation/data/localeResolveConfig.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
import modelengine.fitframework.annotation.Component;
1212

1313
/**
14-
* {@link LocaleResolveFilter} 配置类
14+
* 表示 {@link LocaleResolveFilter} 的配置类。
1515
*
1616
* @author 阮睿
1717
* @since 2025-08-01
1818
*/
1919
@Component
2020
public class localeResolveConfig {
21+
/**
22+
* 注册一个 {@link LocaleResolveFilter} 的 bean。
23+
*
24+
* @return 返回一个 {@link LocaleResolveFilter} 的实例。
25+
*/
2126
@Bean
2227
public LocaleResolveFilter localeResolveFilter() {
2328
return new LocaleResolveFilter();

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-javax/src/main/java/modelengine/fitframework/validation/LocaleContextMessageInterpolator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.Locale;
1616

1717
/**
18-
* 检验消息处理包装类
18+
* 检验消息处理的代理类
1919
* <p>
2020
* 从 {@link LocaleContextHolder} 中获取当前线程设置的 {@link Locale} 并委托 {@link MessageInterpolator} 去处理消息。
2121
* </p>
@@ -24,13 +24,20 @@
2424
* @since 2025-07-31
2525
*/
2626
public class LocaleContextMessageInterpolator implements MessageInterpolator {
27-
2827
private final MessageInterpolator targetInterpolator;
2928

29+
/**
30+
* 构造函数。
31+
*
32+
* @param targetInterpolator 表示目标检验消息处理对象的 {@link MessageInterpolator}。
33+
*/
3034
public LocaleContextMessageInterpolator(MessageInterpolator targetInterpolator) {
3135
this.targetInterpolator = targetInterpolator;
3236
}
3337

38+
/**
39+
* 构造函数,默认使用 {@link ParameterMessageInterpolator} 作为目标检验消息处理对象。
40+
*/
3441
public LocaleContextMessageInterpolator() {
3542
this.targetInterpolator = new ParameterMessageInterpolator();
3643
}

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-javax/src/test/java/modelengine/fitframework/validation/LocaleValidationController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import modelengine.fit.http.annotation.RequestMapping;
1616

1717
/**
18-
* 地区验证控制器 - 测试ValidationHandler与LocaleContextMessageInterpolator的集成
18+
* 用于测试 {@link ValidationHandler} 与 {@link LocaleContextMessageInterpolator} 的集成地区验证控制器。
1919
*
2020
* @author 阮睿
2121
* @since 2025-08-01
@@ -24,13 +24,11 @@
2424
@RequestMapping(path = "/validation/locale", group = "地区验证测试接口")
2525
@Validated
2626
public class LocaleValidationController {
27-
2827
/**
29-
* 测试验证消息的地区化 - 使用简单参数
28+
* 使用简单参数测试验证消息的地区化。
3029
*
31-
* @param company 表示注解验证类 {@link Company}。
30+
* @param company 表示注解验证的测试实体类 {@link Company}。
3231
*/
3332
@PostMapping(path = "/simple", description = "测试简单参数的地区化验证消息")
3433
public void validateSimpleParam(@RequestBody @Valid Company company) {}
35-
3634
}

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-javax/src/test/java/modelengine/fitframework/validation/LocaleValidationControllerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import static org.assertj.core.api.Assertions.assertThat;
2727

2828
/**
29-
* {@link LocaleValidationController} 的测试集
29+
* 表示评估国际化校验的测试类
3030
*
3131
* @author 阮睿
3232
* @since 2025-08-01
3333
*/
3434
@MvcTest(classes = {LocaleValidationController.class})
3535
@DisplayName("测试地区化验证消息功能")
3636
public class LocaleValidationControllerTest {
37-
3837
@Fit
3938
private MockMvc mockMvc;
4039

framework/fit/java/fit-builtin/plugins/fit-validation-hibernate-javax/src/test/java/modelengine/fitframework/validation/data/localeResolveConfig.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
import modelengine.fitframework.annotation.Component;
1212

1313
/**
14-
* {@link LocaleResolveFilter} 配置类
14+
* 表示 {@link LocaleResolveFilter} 的配置类。
1515
*
1616
* @author 阮睿
1717
* @since 2025-08-01
1818
*/
1919
@Component
2020
public class localeResolveConfig {
21+
/**
22+
* 注册一个 {@link LocaleResolveFilter} 的 bean。
23+
*
24+
* @return 返回一个 {@link LocaleResolveFilter} 的实例。
25+
*/
2126
@Bean
2227
public LocaleResolveFilter localeResolveFilter() {
2328
return new LocaleResolveFilter();

framework/fit/java/fit-builtin/services/fit-http-classic/definition/src/main/java/modelengine/fit/http/util/i18n/DefualtLocaleResolver.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
* @author 阮睿
1919
* @since 2025-08-01
2020
*/
21-
public class DefualtLocaleResolver implements LocaleResolver{
22-
21+
public class DefualtLocaleResolver implements LocaleResolver {
2322
public static final String DEFAULT_COOKIE_NAME = "locale";
2423
public static final int DEFAULT_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
2524
public static final String DEFAULT_COOKIE_DOMAIN = "/";
@@ -32,7 +31,7 @@ public class DefualtLocaleResolver implements LocaleResolver{
3231

3332
@Override
3433
public Locale resolveLocale(HttpClassicServerRequest request) {
35-
// 先解析 Cookie,如果没有则使用 Accept-Language 头
34+
// 先解析 Cookie,如果没有则解析 Accept-Language 头
3635
String newLocale = request.cookies().get(this.cookieName).map(Cookie::value).orElse(null);
3736
if (newLocale != null) {
3837
return Locale.forLanguageTag(newLocale);
@@ -52,39 +51,61 @@ public Locale resolveLocale(HttpClassicServerRequest request) {
5251

5352
@Override
5453
public void setLocale(HttpClassicServerResponse response, Locale locale) {
55-
56-
if(locale != null){
57-
response.cookies().add(Cookie.builder()
58-
.name(cookieName)
59-
.value(locale.toLanguageTag())
60-
.maxAge(cookieMaxAge)
61-
.domain(cookieDomain)
62-
.path(cookiePath)
63-
.build());
64-
}else{
65-
response.cookies().add(Cookie.builder()
66-
.name(cookieName)
67-
.maxAge(0)
68-
.build());
54+
if (locale != null) {
55+
response.cookies()
56+
.add(Cookie.builder()
57+
.name(cookieName)
58+
.value(locale.toLanguageTag())
59+
.maxAge(cookieMaxAge)
60+
.domain(cookieDomain)
61+
.path(cookiePath)
62+
.build());
63+
} else {
64+
response.cookies().add(Cookie.builder().name(cookieName).maxAge(0).build());
6965
}
7066
}
7167

68+
/**
69+
* 设置存储地区信息的 Cookie 名称。
70+
*
71+
* @param cookieName 表示将要设置的 Cookie 名称 {@link String}。
72+
*/
7273
public void setCookieName(String cookieName) {
7374
this.cookieName = cookieName;
7475
}
7576

77+
/**
78+
* 设置存储地区信息的 Cookie 的最大有效期。
79+
*
80+
* @param cookieMaxAge 表示将要设置的 Cookie 最大有效期的 {@code int}。
81+
*/
7682
public void setCookieMaxAge(int cookieMaxAge) {
7783
this.cookieMaxAge = cookieMaxAge;
7884
}
7985

86+
/**
87+
* 设置存储地区信息的 Cookie 的作用域。
88+
*
89+
* @param cookieDomain 存储地区信息的 Cookie 作用域的 {@link String}。
90+
*/
8091
public void setCookieDomain(String cookieDomain) {
8192
this.cookieDomain = cookieDomain;
8293
}
8394

95+
/**
96+
* 设置存储地区信息的 Cookie 的可见 URL 路径。
97+
*
98+
* @param cookiePath 存储地区信息的 Cookie 作用域的 {@link String}。
99+
*/
84100
public void setCookiePath(String cookiePath) {
85101
this.cookiePath = cookiePath;
86102
}
87103

104+
/**
105+
* 设置默认地区。
106+
*
107+
* @param defaultLocale 表示默认地区的 {@link Locale}。
108+
*/
88109
public void setDefaultLocale(Locale defaultLocale) {
89110
this.defaultLocale = defaultLocale;
90111
}

framework/fit/java/fit-builtin/services/fit-http-classic/definition/src/main/java/modelengine/fit/http/util/i18n/LocaleResolveFilter.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ public class LocaleResolveFilter implements HttpServerFilter {
3030

3131
private Scope scope = Scope.GLOBAL;
3232

33+
/**
34+
* 构造函数。
35+
*
36+
* @param localeResolver 表示地区解析器的 {@link LocaleResolver}。
37+
*/
3338
public LocaleResolveFilter(LocaleResolver localeResolver) {
3439
localeResolver = localeResolver;
3540
}
3641

37-
public LocaleResolveFilter(){
42+
/**
43+
* 默认构造函数。
44+
*/
45+
public LocaleResolveFilter() {
3846
this.localeResolver = new DefualtLocaleResolver();
3947
}
4048

@@ -58,35 +66,29 @@ public List<String> mismatchPatterns() {
5866
return mismatchPatterns;
5967
}
6068

61-
/**
62-
* 过滤请求,从请求中解析地区值并放入线程上下文和cookie。
63-
*
64-
* @param request 表示服务器请求 {@link HttpClassicServerRequest}。
65-
* @param response 表示服务器响应 {@link HttpClassicServerResponse}。
66-
* @param chain 过滤链 {@link HttpServerFilterChain}。
67-
*/
6869
@Override
6970
public void doFilter(HttpClassicServerRequest request, HttpClassicServerResponse response,
7071
HttpServerFilterChain chain) throws DoHttpServerFilterException {
7172
try {
72-
// 如果参数中带有地区,则直接设置地区
73+
// 如果参数中带有地区,说明用户想使用新地区执行后续的操作,直接设置地区。
7374
String paramLocale = request.queries().first("locale").orElse(null);
7475
Locale responseLocale = null;
7576
if (paramLocale != null && !paramLocale.trim().isEmpty()) {
7677
responseLocale = Locale.forLanguageTag(paramLocale);
7778
LocaleContextHolder.setLocaleContext(new LocaleContext(responseLocale));
7879
}
79-
// 如果参数中不包含地区,则解析
80-
else{
81-
Locale locale =localeResolver.resolveLocale(request);
80+
// 如果参数中不包含地区,则解析请求所带的地区参数。
81+
else {
82+
Locale locale = localeResolver.resolveLocale(request);
8283
LocaleContextHolder.setLocaleContext(new LocaleContext(locale));
8384
}
8485

86+
// 继续执行后续过滤器。
8587
chain.doFilter(request, response);
8688

87-
// responseLocale是用户期望设置的地区,不受 server 端处理的影响
89+
// responseLocale 是用户期望设置的地区,不受 server 端处理的影响
8890
localeResolver.setLocale(response, responseLocale);
89-
}finally {
91+
} finally {
9092
LocaleContextHolder.clear();
9193
}
9294

0 commit comments

Comments
 (0)