springBoot3 国际化项目实战

1 配置文件:

yml 复制代码
# Spring配置
spring:
  # 资源信息
  messages:
    # 国际化资源文件路径,如果有多个通过逗号隔开,这里的messages不是路径,而是文件名前缀
    basename: i18n/messages,i18n/modules/product/product,i18n/modules/salesOrder/salesOrder,i18n/modules/system/system
    encoding: UTF-8
    # 默认语言回滚控制
    fallback-to-system-locale: false

2 i18nConfig 配置

java 复制代码
package com.okyun.framework.config;

@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 添加自定义拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        // 添加拦截器
        registry.addInterceptor(localeChangeInterceptor());
    }
}

3 封装工具类

typescript 复制代码
package com.okyun.common.utils;


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(语言环境)
        return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
    }
}

4 配置国际化文件

matlab 复制代码
/src/main/resources/
├── i18n/
│   ├── messages.properties          # 默认文件
│   ├── messages_zh_CN.properties    # 中文文件
│   ├── messages_en_US.properties    # 英文文件
│   ├── messages_es_ES.properties    # 西语文件
│   └── modules/                     # 模块目录(可选)
│       ├── user.properties
│       ├── user_zh_CN.properties
│       ├── user_en_US.properties
│       ├── user_es_ES.properties
│       ├── system.properties
│       ├── system_zh_CN.properties
│       └── system_en_US.properties
│       └── system_es_ES.properties

5 项目使用

java 复制代码
// 1 响应消息
#配置语言 -zh_CN
user.token.remoteLogin=该账号异地登录,异地登陆时间:{0}!
// 使用配置
MessageUtils.message("user.token.remoteLogin", RemoteLoginTime)


// 2 导入导出
# 配置语言 -zh_CN
product.product.initStock=期初库存
# 使用配置
@Excel(name = "期初库存", i18nKey = "product.product.initStock", sort = 22
@Min(value = 0, message = "期初库存不能小于0")
private BigDecimal initStock;
相关推荐
泡海椒17 分钟前
Java 后端最优 PDF 导出方案:jquick-pdf 项目引入与快速测试
java·开发语言·pdf
阿飞76623 分钟前
订单超时自动关单:RabbitMQ 延迟队列 + 定时任务"双保险"实践
java
二炮手亮子28 分钟前
使用云服务+hermes+mino模型 我用微信操控服务器自己编程并部署实操
java·语言模型·ai编程
她的男孩32 分钟前
安全同事找我要"上周所有登录失败记录",我查完数据库:一条都没有
java·后端·架构
m0_5873830037 分钟前
本地同城无人机作业接单系统源码:架构拆解与抢单调度实现
java·小程序·架构·需求分析
FantanLee41 分钟前
面试级「分布式 ID 设计方案」
java
青春易逝丶1 小时前
Maven
java·maven
吠品1 小时前
纯HTML+ECharts构建交互式数据看板:实现思路与踩坑记录
java·服务器·数据库
盖伦发发1 小时前
Redis 核心教学: 数据结构, 缓存设计, 分布式锁
java·redis·后端·软件工程
卷毛的技术笔记1 小时前
RocketMQ事务消息:我把分布式事务这层窗户纸捅破了
java·分布式·后端·java-rocketmq