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);
}
相关推荐
EatFan7 小时前
Java接入支付宝 JSAPI 支付保姆教程(二):流程讲解与前后端代码讲解
前端·spring boot·后端·微信小程序·小程序·uni-app
凤山老林7 小时前
Spring Boot 整合 Flowable 的企业级落地指南
数据库·spring boot·后端·flowable·工作流
代码调试师10 小时前
【毕设分享】基于SpringBoot的旅游向导分配管理系统57145
spring boot·毕业设计·源码·课程设计·毕设·大作业·程序定制
2301_322414280410 小时前
活力孕康复APP 47737- 原创(免费领源码+部署教程+开发环境)
java·vue.js·spring boot·mysql·微信小程序·idea·微信开发者工具
vx_Biye_Design11 小时前
springboot游泳馆系统93765-计算机课程设计、毕业设计
java·javascript·spring boot·后端·python·spring·课程设计
专业程序开发源12 小时前
springbootLivehouse票务系统-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·课程设计·pygame
卓怡学长12 小时前
w233基于vue和springboot的宠物管理系统的设计与实现
java·vue.js·spring boot·spring·intellij-idea
梨涡泥窝14 小时前
JavaWeb——基于 Spring Boot + Vue 的图书管理系统的设计与实现
vue.js·spring boot·后端
隐退山林17 小时前
JavaEE进阶:SpringBoot统一功能处理
java·spring boot·后端
loser.with.m17 小时前
【AgentScope 2.0】8-MCP 集成:Agent 的「USB-C 接口」是怎么接的
人工智能·spring boot·agentscope