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);
}
相关推荐
Albert Edison4 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
六毛的毛7 小时前
Springboot开发常见注解一览
java·spring boot·后端
开开心心就好9 小时前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
猴哥源码9 小时前
基于Java+SpringBoot的农事管理系统
java·spring boot
光军oi10 小时前
java微服务(Springboot篇)——————IDEA搭建第一个Springboot入门项目
java·spring boot·微服务
猴哥源码12 小时前
基于Java+SpringBoot的健身房管理系统
java·spring boot
猴哥源码12 小时前
基于Java+SpringBoot的三国之家网站
java·spring boot
Spirit_NKlaus12 小时前
解决HttpServletRequest无法获取@RequestBody修饰的参数
java·spring boot·spring
不死的精灵12 小时前
【Java21】在spring boot中使用ScopedValue
java·spring boot·后端
仰望星空@脚踏实地15 小时前
Spring Boot Web 服务单元测试设计指南
spring boot·后端·单元测试