springboot国际化

springboot国际化

不需要引入额外的jar包

参考:https://zhuanlan.zhihu.com/p/551605839

1.rources要创建Resource Bundle

2.yml配置中引入Resource Bundle

引入Resource Bundle

yml 复制代码
spring:
  messages:
    encoding: UTF-8
    basename: i18n/messages_common

3.创建国际化工具

java 复制代码
/**
 * 国际化
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class MessageUtils {
    private static MessageSource messageSource;
    static {
        messageSource = (MessageSource)SpringContextUtils.getBean("messageSource");
    }

    public static String getMessage(int code){
        return getMessage(code, new String[0]);
    }

    public static String getMessage(int code, String... params){
        return messageSource.getMessage(code+"", params, LocaleContextHolder.getLocale());
    }
}

4.在校验中使用国际化提示信息

java 复制代码
public ZenException(int code, String... params) {
	this.code = code;
	this.msg = MessageUtils.getMessage(code, params);
}
相关推荐
ZhengEnCi8 小时前
S02-SpringBoot实体类新增字段对已有数据的影响及自动DDL同步机制详解
数据库·spring boot
程序员张38 小时前
SpringBoot集成BCrypt密码加密库
java·spring boot·后端
952369 小时前
RabbitMQ-基础操作
java·spring boot·分布式·后端·spring·rabbitmq
卓怡学长10 小时前
w266基于spring boot + vue 圣地延安美食乐享系统
java·数据库·vue.js·spring boot·spring·intellij-idea
Listen·Rain12 小时前
Springboot整合Ollama实现调用本地多模型
java·spring boot·后端
宠友信息12 小时前
多端即时通讯源码技术实践,Redis路由、MySQL持久化与Socket接入
spring boot·websocket·mysql·uni-app
Tom·Ge13 小时前
【Spring Boot 4】Spring Boot 4.0 AI Agent实战:Skills Agent + MCP协议集成
人工智能·spring boot·microsoft
Devin~Y13 小时前
电商场景下的Java面试实战:从Spring Boot微服务到Kafka、Redis与AI RAG
java·spring boot·redis·elasticsearch·spring cloud·微服务·kafka
白露与泡影14 小时前
用 Spring Boot + Vue + Fuzio 构建现代 Java 桌面应用
java·vue.js·spring boot
宠友信息15 小时前
Spring Boot与异步审核构建仿小红书源码内容发布全流程
java·数据库·spring boot·redis·mysql·oracle·uni-app