✨ [2025-05-15] 添加 i18n 处理
All checks were successful
Publish to Confluence / confluence (push) Successful in 56s
All checks were successful
Publish to Confluence / confluence (push) Successful in 56s
This commit is contained in:
parent
c8ffa34498
commit
4ef3180d5a
@ -509,3 +509,6 @@ yuanmeng:
|
||||
----
|
||||
|
||||
## 开发说明
|
||||
### 功能列表
|
||||
- 添加电商对接
|
||||
- 添加企微通讯录回调
|
||||
|
@ -35,6 +35,7 @@
|
||||
- [X] `YmResponseAdvice`: 全局结果封装
|
||||
- [X] `YmGlobalExceptionHandlerAdvice`: 全局异常拦截处理
|
||||
- [X] `YmAutofillResponseAdvice`: 全局返回结果填充处理
|
||||
- [X] `YmI18nResponseAdvice`: I18n全局处理
|
||||
- [X] `YmHealthCheckFilter`: 全局健康检查接口
|
||||
- [X] `YmRequestBodyWrapperFilter`: request 和 response 可重复读封装
|
||||
- [X] `YmRequestPrintFilter`: 请求日志打印
|
||||
@ -77,9 +78,11 @@
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 使用工具
|
||||
---
|
||||
|
||||
#### 忽略结果封装
|
||||
## 使用工具
|
||||
|
||||
### 忽略结果封装
|
||||
|
||||
```java
|
||||
public class Usage {
|
||||
@ -98,7 +101,7 @@ public class Usage {
|
||||
}
|
||||
```
|
||||
|
||||
#### 全局异常拦截
|
||||
### 全局异常拦截
|
||||
|
||||
```java
|
||||
public class Usage {
|
||||
@ -134,7 +137,98 @@ public class Usage {
|
||||
}
|
||||
```
|
||||
|
||||
#### 结果自动填充
|
||||
### 国际化支持
|
||||
|
||||
- 开启国际化支撑
|
||||
|
||||
```yaml
|
||||
yuanmeng:
|
||||
web:
|
||||
i18n:
|
||||
enable: true
|
||||
```
|
||||
|
||||
- 添加国际化文件
|
||||
- 默认 `resources > i18n > messages_zh_CN.properties`
|
||||
|
||||
```properties
|
||||
Base\ Error=服务端错误
|
||||
```
|
||||
|
||||
- 编写代码
|
||||
|
||||
```java
|
||||
|
||||
@GetMapping("/i18n")
|
||||
public String i18n() {
|
||||
return "test success";
|
||||
}
|
||||
|
||||
@YmIgnoreI18n
|
||||
@GetMapping("/i18n-ignore")
|
||||
public String i18nIgnore() {
|
||||
return "i18n ignore";
|
||||
}
|
||||
|
||||
@GetMapping("/i18n-error")
|
||||
public String i18nError() {
|
||||
throw new YmBizException(YmResultCode.BASE_ERROR);
|
||||
}
|
||||
```
|
||||
|
||||
- **自定义国际化处理类(可选)**
|
||||
|
||||
```java
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CustomI18nMessageHandler implements YmI18nMessageHandler {
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
@Override
|
||||
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
|
||||
return messageSource.getMessage(code, args, defaultMessage, locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
|
||||
return messageSource.getMessage(code, args, locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
|
||||
return messageSource.getMessage(resolvable, locale);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public class YmI18nConfiguration {
|
||||
|
||||
public static final String I18N_MESSAGE_SOURCE = "ymI18nMessageSource";
|
||||
public static final String I18N_MESSAGE_HANDLER = "ymI18nMessageHandler";
|
||||
|
||||
@Bean(I18N_MESSAGE_SOURCE)
|
||||
@ConditionalOnMissingBean(name = I18N_MESSAGE_SOURCE)
|
||||
public MessageSource messageSource() {
|
||||
log.info("[WEB_STARTER_INIT]: CUSTOM I18N_MESSAGE_SOURCE init");
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("i18n/test");
|
||||
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean(I18N_MESSAGE_HANDLER)
|
||||
@ConditionalOnMissingBean(name = I18N_MESSAGE_HANDLER)
|
||||
public YmI18nMessageHandler ymI18nMessageHandler(@Qualifier(I18N_MESSAGE_SOURCE) MessageSource messageSource) {
|
||||
log.info("[WEB_STARTER_INIT]: CUSTOM YmI18nMessageHandler init");
|
||||
return new CustomI18nMessageHandler(messageSource);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### 结果自动填充
|
||||
|
||||
```java
|
||||
|
||||
@ -259,4 +353,6 @@ yuanmeng:
|
||||
print-type: 2
|
||||
external-url-list:
|
||||
- "/external/test"
|
||||
i18n:
|
||||
enable: true
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user