SpringBoot3项目实现国际化的示例详解
目录
- 1 创建国际化资源文件
- 2 配置 application.yml
- 3 维护 i8n 配置类
- 4 封装消息工具
- 5 后台业务中使用
- 6 前端切换语言
- 7 特殊国际化处理 - 参数校验注解
- 8 特殊国际化处理 - Excel列名
- 8.1 Excel注解添加i18n字段
- 8.2 ExcelUtil 方法添加获取国际化逻辑
- 8.3 字典数据国际化
- 8.4 项目实战中使用
下面小编就来和大家简单介绍一下springBoot3 项目实现国际化的完整代码吧
1 创建国际化资源文件
//src/main/resources/i18n/Resource Bundle 'messages'
messages.properties (默认)
messages_en_US.properties (英文)
messages_zh_CN.properties (中文)
// 翻译文件内容:
# {}是参数引用,用于拼接传参
user.password.retry.limit.exceed=密码输入错误{0}次,帐户锁定{1}分钟
2 配置 application.yml
# Spring配置
spring:
# 资源信息
messages:
# 国际化资源文件路径,如果有多个通过逗号隔开,这里的messages不是路径,而是文件名前缀
basenajavascriptme: i18n/messages
encoding: UTF-8
3 维护 i8n 配置类
package com.okyun.framework.config;
/**
* 资源文件配置加载
* 语言切换优先级 -> 1 请求lang参数语言 -> 2 浏览器设置的默认语言 -> 3 该系统默认语言(简体中文)
*
* @author hubiao
*/
@Configuration
public class I18nConfig implements WebMvcConfigurer
{
/**
* 语言切换
* @return
*/
@Bean
public LocaleResolver localeResolver()
{
// SessionLocaleResolver 用来解析和存储用户的 Locale 信息在会话 (session) 中
SessionLocaleResolver slr = new SessionLocaleResolver();
// 设置默认语言 = 简体中文,可以用 Locale.CHINA
slr.setDefaultLocale(Constants.DEFAULT_LOCALE);
return slr;
}
/**
* LocaleChangeInterceptor: Spring 提供的用于切换 Locale 的拦截器,拦截 HTTP 请求中的参数来确定用户想要切换到的语言
* lci.setParamName("lang"): 设置 URL 请求中的参数名为 lang
* 即用户可以通过传递 ?lang=xx 来切换语言(如 ?lang=zh_CN 切换到简体中文,?lang=en 切换到英文)
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor()
{
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
}
/**
* WebMvcConfigurer 添加自定义拦截器
* @pawww.devze.comram registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry)
{
// 添加拦截器
registry.addInterceptor(localeChangeInterceptor());
}
}
4 封装消息工具
package com.okyun.common.utils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import com.okyun.common.utils.spring.SpringUtils;
/**
* 获取i18n资源文件
*
* @author hubiao
*/
public class MessageUtils
{
/**
* 根据消息键和参数 获取消息 委托给spring messageSource
*
* @param code 消息键
* @param args 参数
* @return 获取国际化翻译值
*/
public static String message(String code, Object... args)
{
// MessageSource:Spring 的国际化资源接口,主要用于获取不同语言环境下的消息内容
MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
// LocaleContextHolder.getLocale():从 LocaleContextHolder 中获取当前线程的 Locale(语言环境)python
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
}
}
5 后台业务中使用
AJAXResult.error(MessageUtils.message("user.password.retry.limit.exceed",new Object[] { retryLimitCount, lockTime }))
6 前端切换语言
/**
* 切换语言
* @param lang
* @return
*/
@GetMapping("/changeLanguage")
public AjaxResult changeLanguage(String lang)
{
return AjaxResult.success();
}
7 特殊国际化处理 - 参数校验注解
/**
* 参数验证国际化
*/
@Size(max = 30, message = "编码最大长度30个字符")
@Pattern(regexp = "^[^+-].*", message = "编码不能以 + 或 - 开头")
private String productNo;
// 举例 @Size(max = 30, message = "size.max.valid")
# Spring 会自动从 `messages.properties` 加载对应的消息
size.between.valid=长度必须在{min}到{max}个字符之间
size.max.valid=长度不能超过{max}个字符
size.min.valid=长度不能少于{min}个字符
8 特殊国际化处理 - Excel列名
8.1 Excel注解添加i18n字段
package com.okyun.common.annotation;
/**
* 自定义导出Excel数据注解
*
* @pythonauthor hubiao
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel{
...
/**
* 国际化Key(优先级高于name).
* 示例: excel.user.name
*/
public String i18nKey() default "";
...
}
8.2 ExcelUtil 方法添加获取国际化逻辑
package com.okyun.common.utils.poi;
/**
* Excel相关处理
* @author hubiao
*/
public class ExcelUtil<T>{
...
// 国际化 - 注入MessageSource(若依中可通过SpringUtils获取)
private static final MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
/**
* 获取国际化表头名称
*/
private String getI18nHeader(Excel excelAttr) {
if (StringUtils.isNotEmpty(excelAttr.i18nKey())) {
return messageSource.getMessage(
excelAttr.i18nKey(),
null,
LocaleContextHolder.getLocale()
);
}
return excelAttr.name(); // 默认返回name
}
/**
* 创建单元格
*/
public Cell createHeadCell(Excel attr, Row row, int column){
...
// 写入列信息 - 国际化
cell.setCellValue(getI18nHeader(attr));
...
}
/**
* 添加单元格
*/
public Cell addCell(Excel attr, Row row, T vo, Field field, int column){
......
else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value)) {
String label = DictUtils.getDictLabel(
dictType,
Convert.toStr(value),
separator,
LocaleContextHolder.getLocale() // 传递当前语言环境
);
cell.setCellValue(label);
}
......
}
...
}
8.3 字典数据国际化
package com.okyun.common.utils;
/**
* 字典工具类
*
* @author hubiao
*/
public class DictUtils{
......
/**
* 获取国际化字典标签(完整实现)
* @param dictType 字典类型
* @param dictValue 字典值
* @param separator 分隔符
* @param locale 语言环境
* @return 字典标签
*/
public static String getDictLabel(String dictType, String dictValue, String separator, Locale locale) {
// 1. 尝试获取国际化标签
String i18nKey = "dict." + dictType + "." + dictValue;
MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
String i18nLabel = messageSource.getMessage(i18编程客栈nKey, null, null, locale);
// 2. 如果不存在国际化配置,回退到系统字典
if (i18nLabel == null || i18nKey.equals(i18nLabel)) {
return getDictLabel(dictType, dictValue, separator); // 调用原有方法
}
return i18nLabel;
}
......
}
8.4 项目实战中使用
8.4.1 @Excel注解
/** * 商品状态 * dictType 字典类型 * i18nKey 映射的字段名 */ @Excel(name = "商品状态", dictType = "product_status", i18nKey = "product.product.status") private Integer productStatus; /** * 编码 * i18nKey 映射的字段名 */ @Excel(name = "编码", i18nKey = "product.product.code") private String productNo;
8.4.2 维护映射文件
# excel导出/导入 - 字段名映射 product.product.code=商品编码 product.product.status=商品状态 # dictData 的 value 对应 label # dict 固定前缀 # dictType 字典类型 # 0 字典值 # 启售 字典label dict.product_status.0=启售 dict.product_status.1=停售
到此这篇关于SpringBoot3项目实现国际化的示例详解的文章就介绍到这了,更多相关SpringBoot3国际化内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论