-
Notifications
You must be signed in to change notification settings - Fork 327
[fit] Enhance FIT's i18n capabilities by parsing locales from HTTP requests. #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CodeCasterX
merged 25 commits into
ModelEngine-Group:main
from
Yager-42:fit-feature-i18n
Sep 15, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
26a293e
[fit] 实现请求地区解析
Yager-42 21fe248
[fit] 实现国际化消息处理器,完善测试
Yager-42 c79a348
[fit] 完善注释
Yager-42 8a97b05
[fit] 完善注释
Yager-42 53f1c3e
[fit] 修改校验处理默认使用的插值器
Yager-42 bcb4f86
[fit] 修改校验处理插值器,提供默认地区设置功能
Yager-42 f1b5130
初步实现
Yager-42 dfec108
可用的多插件地区解析实现
Yager-42 c7afa65
完善注释
Yager-42 c82354c
恢复样例
Yager-42 956948e
修改实现
Yager-42 1ea4da2
完善代码
Yager-42 ffe6777
删除pom中的旧实现残留
Yager-42 88ce487
修改错误方法名,为工具类提供测试方法。
Yager-42 90ff96b
修改格式。
Yager-42 dac1a24
修改格式
Yager-42 0493ec8
删除localecontext
Yager-42 f849028
修复逻辑错误,完善格式
Yager-42 f530625
完善格式
Yager-42 ecb2134
修复错误
Yager-42 53a6a7a
修复错误
Yager-42 9b695ca
完善测试
Yager-42 c8a6d16
修复错误
Yager-42 57ac0b1
为插值方法提供null值保护
Yager-42 4976605
完善格式以及优化null检查
Yager-42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...a/src/main/java/modelengine/fitframework/validation/LocaleContextMessageInterpolator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fitframework.validation; | ||
|
|
||
| import jakarta.validation.MessageInterpolator; | ||
| import modelengine.fitframework.inspection.Validation; | ||
| import modelengine.fitframework.util.ObjectUtils; | ||
| import modelengine.fitframework.util.i18n.LocaleContextHolder; | ||
|
|
||
| import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * 检验消息处理的代理类。 | ||
| * <p> | ||
| * 从 {@link LocaleContextHolder} 中获取当前线程设置的 {@link Locale} 并委托 {@link MessageInterpolator} 去处理消息。 | ||
| * </p> | ||
| * | ||
| * @author 阮睿 | ||
| * @since 2025-07-31 | ||
| */ | ||
| public class LocaleContextMessageInterpolator implements MessageInterpolator { | ||
| private final MessageInterpolator targetInterpolator; | ||
| private Locale locale; | ||
|
|
||
| /** | ||
| * 构造函数。 | ||
| * | ||
| * @param targetInterpolator 表示目标检验消息处理对象的 {@link MessageInterpolator}。 | ||
| */ | ||
| public LocaleContextMessageInterpolator(MessageInterpolator targetInterpolator) { | ||
| this.targetInterpolator = targetInterpolator; | ||
| this.locale = Locale.getDefault(); | ||
| } | ||
|
|
||
| /** | ||
| * 构造函数,默认使用 {@link ParameterMessageInterpolator} 作为目标检验消息处理对象。 | ||
| */ | ||
| public LocaleContextMessageInterpolator() { | ||
| this.targetInterpolator = new ParameterMessageInterpolator(); | ||
| this.locale = Locale.getDefault(); | ||
| } | ||
|
|
||
| /** | ||
| * 构造函数。 | ||
| * | ||
| * @param locale 表示当前设置默认的 {@link Locale}。 | ||
| */ | ||
| public LocaleContextMessageInterpolator(Locale locale) { | ||
| this.targetInterpolator = new ParameterMessageInterpolator(); | ||
| this.locale = ObjectUtils.getIfNull(locale, Locale::getDefault); | ||
| } | ||
|
|
||
| /** | ||
| * 构造函数。 | ||
| * | ||
| * @param targetInterpolator 表示目标检验消息处理对象的 {@link MessageInterpolator}。 | ||
| * @param locale 表示当前设置默认的 {@link Locale}。 | ||
| */ | ||
| public LocaleContextMessageInterpolator(MessageInterpolator targetInterpolator, Locale locale) { | ||
| this.targetInterpolator = targetInterpolator; | ||
| this.locale = ObjectUtils.getIfNull(locale, Locale::getDefault); | ||
| } | ||
|
|
||
| /** | ||
| * 设置默认的 {@link Locale}。 | ||
| * | ||
| * @param locale 默认设置的 {@link Locale}。 | ||
| */ | ||
| public void setLocale(Locale locale) { | ||
| this.locale = ObjectUtils.getIfNull(locale, Locale::getDefault); | ||
| } | ||
|
|
||
| @Override | ||
| public String interpolate(String messageTemplate, Context context) { | ||
| if (LocaleContextHolder.getLocale() != null) { | ||
| return this.targetInterpolator.interpolate(messageTemplate, context, LocaleContextHolder.getLocale()); | ||
| } | ||
| return this.targetInterpolator.interpolate(messageTemplate, context, this.locale); | ||
| } | ||
|
|
||
| @Override | ||
| public String interpolate(String messageTemplate, Context context, Locale locale) { | ||
| Validation.notNull(locale, "Locale cannot be null."); | ||
| return this.targetInterpolator.interpolate(messageTemplate, context, locale); | ||
| } | ||
| } | ||
86 changes: 0 additions & 86 deletions
86
...-jakarta/src/main/java/modelengine/fitframework/validation/LocaleMessageInterpolator.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
...rta/src/test/java/modelengine/fitframework/validation/LocaleValidationControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fitframework.validation; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import modelengine.fit.http.client.HttpClassicClientResponse; | ||
| import modelengine.fit.http.entity.Entity; | ||
| import modelengine.fit.http.entity.ObjectEntity; | ||
| import modelengine.fitframework.annotation.Fit; | ||
| import modelengine.fitframework.test.annotation.MvcTest; | ||
| import modelengine.fitframework.test.domain.mvc.MockMvc; | ||
| import modelengine.fitframework.test.domain.mvc.request.MockMvcRequestBuilders; | ||
| import modelengine.fitframework.test.domain.mvc.request.MockRequestBuilder; | ||
| import modelengine.fitframework.validation.data.Company; | ||
| import modelengine.fitframework.validation.data.LocaleValidationController; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * 表示评估国际化校验的测试类。 | ||
| * | ||
| * @author 阮睿 | ||
| * @since 2025-08-01 | ||
| */ | ||
| @MvcTest(classes = {LocaleValidationController.class}) | ||
| @DisplayName("测试地区化验证消息功能") | ||
| public class LocaleValidationControllerTest { | ||
| @Fit | ||
| private MockMvc mockMvc; | ||
|
|
||
| private HttpClassicClientResponse<?> response; | ||
|
|
||
| @AfterEach | ||
| void teardown() throws IOException { | ||
| if (this.response != null) { | ||
| this.response.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("测试法文地区的验证消息") | ||
| void shouldReturnFrenchValidationMessage() { | ||
| Company invalidCompany = new Company(null); | ||
|
|
||
| MockRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/validation/locale/simple") | ||
| .header("Accept-Language", "fr") | ||
| .jsonEntity(invalidCompany) | ||
| .responseType(Map.class); | ||
|
|
||
| this.response = this.mockMvc.perform(requestBuilder); | ||
| // 获取JSON格式的错误信息 | ||
| String errorMessage = ""; | ||
| if (this.response.entity().isPresent()) { | ||
| Entity entity = this.response.entity().get(); | ||
| if (entity instanceof ObjectEntity) { | ||
| ObjectEntity<?> objectEntity = (ObjectEntity<?>) entity; | ||
| Object errorObj = objectEntity.object(); | ||
| if (errorObj instanceof Map) { | ||
| Map<String, Object> errorMap = (Map<String, Object>) errorObj; | ||
| errorMessage = | ||
| errorMap.get("error") != null ? errorMap.get("error").toString() : errorMap.toString(); | ||
| } else { | ||
| errorMessage = errorObj.toString(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assertThat(errorMessage).isEqualTo("validateSimpleParam.company.employees: ne doit pas être nul"); | ||
| assertThat(this.response.statusCode()).isEqualTo(500); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("测试英文地区的验证消息") | ||
| void shouldReturnEnglishValidationMessage() { | ||
| Company invalidCompany = new Company(null); | ||
|
|
||
| MockRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/validation/locale/simple") | ||
| .header("Accept-Language", "en-us") | ||
| .jsonEntity(invalidCompany) | ||
| .responseType(Map.class); | ||
|
|
||
| this.response = this.mockMvc.perform(requestBuilder); | ||
| // 获取JSON格式的错误信息 | ||
| String errorMessage = ""; | ||
| if (this.response.entity().isPresent()) { | ||
| Entity entity = this.response.entity().get(); | ||
| if (entity instanceof ObjectEntity) { | ||
| ObjectEntity<?> objectEntity = (ObjectEntity<?>) entity; | ||
| Object errorObj = objectEntity.object(); | ||
| if (errorObj instanceof Map) { | ||
| Map<String, Object> errorMap = (Map<String, Object>) errorObj; | ||
| errorMessage = | ||
| errorMap.get("error") != null ? errorMap.get("error").toString() : errorMap.toString(); | ||
| } else { | ||
| errorMessage = errorObj.toString(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assertThat(errorMessage).isEqualTo("validateSimpleParam.company.employees: must not be null"); | ||
| assertThat(this.response.statusCode()).isEqualTo(500); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("测试URL参数指定地区") | ||
| void shouldUseLocaleFromUrlParam() { | ||
| Company invalidCompany = new Company(null); | ||
|
|
||
| MockRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/validation/locale/simple") | ||
| .param("locale", "en-US") | ||
| .jsonEntity(invalidCompany) | ||
| .responseType(Map.class); | ||
|
|
||
| this.response = this.mockMvc.perform(requestBuilder); | ||
|
|
||
| // 获取JSON格式的错误信息 | ||
| String errorMessage = ""; | ||
| if (this.response.entity().isPresent()) { | ||
| Entity entity = this.response.entity().get(); | ||
| if (entity instanceof ObjectEntity) { | ||
| ObjectEntity<?> objectEntity = (ObjectEntity<?>) entity; | ||
| Object errorObj = objectEntity.object(); | ||
| if (errorObj instanceof Map) { | ||
| Map<String, Object> errorMap = (Map<String, Object>) errorObj; | ||
| errorMessage = | ||
| errorMap.get("error") != null ? errorMap.get("error").toString() : errorMap.toString(); | ||
| } else { | ||
| errorMessage = errorObj.toString(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assertThat(errorMessage).isEqualTo("validateSimpleParam.company.employees: must not be null"); | ||
| assertThat(this.response.cookies().get("locale").isPresent()); | ||
| assertThat(this.response.cookies().get("locale").get().value()).isEqualTo("en-US"); | ||
| assertThat(this.response.statusCode()).isEqualTo(500); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.