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 disabledmessages_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);}}