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);
}
相关推荐
学长毕设5 小时前
基于SpringBoot的建筑材料管理系统-附源码
java·spring boot·后端
星光开发者6 小时前
基于springboot智慧草场牛羊管理系统-计算机毕设【课程设计】50360
vue.js·spring boot·后端·mysql·php·课程设计·express
weixin_BYSJ19876 小时前
电竞先锋网站---附源码02032
java·vue.js·spring boot·python·django·tomcat·php
她的男孩7 小时前
超时配置了自动驳回,任务状态却全变「已取消」:Forge Admin 流程超时终态与 Flowable ENTITY_DELETED
spring boot·低代码·工作流引擎
二宝哥8 小时前
02.springboot项目对接国内模型实现对话聊天
数据库·人工智能·spring boot
学长毕设9 小时前
基于springboot的农资管理系统-附源码
java·spring boot·后端
Magicdata大数据11 小时前
AI辅助教学:调用大模型从教材中生成习题,方便学员练习
java·spring boot·大模型·知识图谱·neo4j·ai辅助教学
不识君11 小时前
esl-client 2.0.0 发布:Java 项目接入 FreeSWITCH,从引入到使用
spring boot·后端·开源
二宝哥12 小时前
03.Spring AI Embedding 实战:从向量化原理到项目落地
人工智能·spring boot·向量·spring ai
captain37613 小时前
Spring Web MVC(2)
java·前端·spring boot·后端·spring·mvc