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);
}
相关推荐
Java内核笔记6 小时前
方法级 trace 看不出第几轮:给 Agent 补一层轮次维度
人工智能·spring boot·后端
她的男孩6 小时前
转办给同事后对方一点同意就报幂等冲突:Forge Admin 不把转办键当成终态
spring boot·低代码·工作流引擎
北风toto10 小时前
Spring Boot 常用注解 · 概要笔记
spring boot·笔记·后端
宋冠巡10 小时前
Spring Boot 4 + OpenTelemetry + Spring Cloud Gateway:全链路 TraceId 传播验证
spring boot·可观测性·traceid
下一站春风里10 小时前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
小码哥06811 小时前
Spring Boot在线租房系统源码:UniApp找房、VR展示与预约管理落地解析
spring boot·租房小程序·租房系统·在线租房
芜名氏11 小时前
API 文档生成工具——SpringDoc
java·spring boot·ci/cd·设计规范
乌暮1 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
小荷才露尖尖角,早有蜻蜓立上头1 天前
SpringBoot+QQ邮箱发送邮件
spring boot
头发还在的女程序员2 天前
Java在线租房系统源码解析:Spring Boot+Vue 3+UniApp房源管理与预约看房方案
java·vue.js·spring boot·租房小程序·租房系统·租房软件