news 2026/4/18 12:08:39

SpringBoot国际化实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot国际化实现

Spring国际化实现

本次我们要实现的是统一错误信息,进行国际化。

1. 首先我们要添加SpringBoot配置,有两种方式能开启国际化功能:

1.1 yaml配置:

spring:messages:basename:i18n/messagesencoding:UTF-8
  • 与之对应的文件地址和名称要匹配
  • 具体如下:

    i18n:包名
    messages:文件前缀。
    en_US / zh_CN:对应的国际化语言,可以通过LocaleContextHolder.getLocale()这个方法来获取当前请求的Locale。如果要自定义的话:
    • en_US:Locale.US
    • zh_CN:Locale.SIMPLIFIED_CHINESE

你可以修改你的文件前缀名,但是对应文件的名称也需要改。

❓如果配置yaml不好使,或者输出的内容都是????????这种,我们要看一下对应的*.properties的文件编码是否是UTF-8

如果不是UTF-8,在IDEA中的 settings -> editor -> file ecoding ->Default encoding for properties files 这个配置改成UTF-8。 如果失败删除原文件,重新创建一个properties文件

1.2. 通过 Java 实现。

@ConfigurationpublicclassI18nConfig{/** * 创建并返回 MessageSource 实例,用于解析国际化消息。 * * @return 已配置的 {@link MessageSource},支持可重加载、指定编码与缓存时间。 */@BeanpublicMessageSourcemessageSource(){ReloadableResourceBundleMessageSourcems=newReloadableResourceBundleMessageSource();// 指定消息文件的基础路径(不带语言后缀与扩展名)ms.setBasename("classpath:i18n/messages");// 使用 UTF-8 编码,避免中文乱码ms.setDefaultEncoding("UTF-8");// 缓存时间(秒),为 3600s 表示每小时刷新一次;开发时可设置为 0ms.setCacheSeconds(3600);returnms;}}

1.创建国际化文件

上述我的配置i18n/messages,那么我就需要在项目中resource文件夹中创建一个名为i18n的包,在这个包下创建前缀为messages的国际化文件。

我创建了两个国际化文件,一个中文,一个英文:

  • i18n/messages_zh_CN.properties
  • i18n/messages_en_US.properties

2.国际化文件内容

国际化文件的内容非常简单,格式为key = value, 我们先配置好两个国际化文件。

  • key: 可以理解为Map中的key 用来定位具体内容。
  • value:具体内容。

messages_en_US.properties:

user.not.found=User not found user.disabled=User is disabled

messages_zh_CN.properties:

user.not.found=用户不存在 user.disabled=用户已被禁用

3.获取国际化内容工具类

我们可以直接注入这个工具类调用I18nUtil.get("user.not.found"),来获取国际化内容。
也可以主动设置Locale,来自定义获取国际化内容:

  • I18nUtil.get("user.not.found",Locale.SIMPLIFIED_CHINESE)调用这个方法,就在messages_zh_CN.properties配置中寻找user.not.found这个Key的Value值。返回结果为:用户不存在

  • I18nUtil.get("user.not.found",Locale.US)调用这个方法,就在messages_en_US.properties配置中寻找user.not.found这个Key的Value值。返回结果为:User not found

@ComponentpublicclassI18nUtil{@ResourceprivateMessageSourcemessageSource;/** * Resolve a message by its code using the current locale. * * @param code the message code (key) in the properties files * @param args optional arguments for parameterized messages * @return the localized message for the current locale */publicStringget(Stringcode,Object...args){returnmessageSource.getMessage(code,args,LocaleContextHolder.getLocale());}/** * Resolve a message by its code for a specific locale. * * @param code the message code (key) * @param locale the locale to use for lookup * @param args optional message parameters * @return the localized message for the given locale */publicStringget(Stringcode,Localelocale,Object...args){returnmessageSource.getMessage(code,args,locale);}/** * Resolve a message using an {@link ErrorCode} and the current locale. * * @param errorCode the error code which provides the message key * @param args optional message parameters * @return the localized message corresponding to the error code */publicStringget(ErrorCodeerrorCode,Object...args){returnmessageSource.getMessage(errorCode.getMsgKey(),args,LocaleContextHolder.getLocale());}}

如果你想要使用I18nUtil工具类,对应的类不能注入依赖,怎么办?

  • 通过静态的方式设置I18nUtil,让普通的类也能够调用I18nUtil内部的方法。
    • 可以直接在普通类中调用I18nHolder.getMessage("user.not.found")
    • 💡可以使用如下类:
importcom.entity.taient.exception.ErrorCode;importorg.springframework.stereotype.Component;importjavax.annotation.Resource;/** * I18nHolder 是一个静态访问点,用于在无法注入 Bean(例如静态方法或工具类中)时获取国际化消息。 * <p> * Spring 在启动时会通过 {@link #setI18nUtil(I18nUtil)} 注入实际的 {@code I18nUtil} 实例。 */@ComponentpublicclassI18nHolder{/** * 持有被注入的 I18nUtil 实例(用于检索国际化消息)。 */privatestaticI18nUtili18nUtil;/** * 由 Spring 注入 I18nUtil 实例。该方法会在容器初始化时被调用。 * * @param util 注入的 I18nUtil */@ResourcepublicvoidsetI18nUtil(I18nUtilutil){I18nHolder.i18nUtil=util;}/** * 根据错误码获取对应的国际化消息。 * 该方法为静态方法,方便在非 Spring 管理的静态上下文中直接调用。 * * @param errorCode 国际化 key * @param args 可选的参数,用于消息格式化 * @return 本地化后的消息字符串 * @throws NullPointerException 当 I18nUtil 未被注入时会抛出(通常表示 Spring 未正确初始化) */publicstaticStringgetMessage(StringerrorCode,Object...args){returni18nUtil.get(errorCode,args);}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 9:44:05

Puerts性能优化终极指南:如何让JavaScript在游戏中飞起来

你是否曾为游戏中的JavaScript性能瓶颈而苦恼&#xff1f;Puerts作为连接TypeScript与游戏引擎的桥梁&#xff0c;提供了完整的性能优化方案。本文将带你从CPU优化到内存管理&#xff0c;全方位提升游戏运行效率。&#x1f680; 【免费下载链接】puerts PUER(普洱) Typescript.…

作者头像 李华
网站建设 2026/4/18 6:28:01

为什么99%的团队都做不好MCP网关扩展?,一线专家的血泪经验总结

第一章&#xff1a;Docker MCP 网关扩展开发概述在现代云原生架构中&#xff0c;Docker MCP&#xff08;Microservice Control Plane&#xff09;网关作为微服务通信的核心枢纽&#xff0c;承担着请求路由、负载均衡、安全认证与流量控制等关键职责。随着业务场景的不断演进&am…

作者头像 李华
网站建设 2026/4/18 4:01:06

3步解锁wvp-GB28181-pro:零基础搭建专业级视频监控平台

3步解锁wvp-GB28181-pro&#xff1a;零基础搭建专业级视频监控平台 【免费下载链接】wvp-GB28181-pro 项目地址: https://gitcode.com/GitHub_Trending/wv/wvp-GB28181-pro 你是否曾为视频监控系统的高昂成本和复杂部署而头疼&#xff1f;wvp-GB28181-pro作为一款完全开…

作者头像 李华
网站建设 2026/4/17 9:10:21

VSCode连接量子处理器总是超时?你必须掌握的实时检测黑科技

第一章&#xff1a;VSCode 量子硬件的连接检测在开发量子计算应用时&#xff0c;确保本地开发环境与量子硬件之间的稳定连接至关重要。VSCode 作为主流的集成开发环境&#xff0c;通过扩展插件支持对量子设备的连接状态进行实时检测与调试。配置 Quantum Development Kit 扩展 …

作者头像 李华