Springboot组件:国际化
Springboot组件:国际化
1 使用说明
引用一篇别人的文章,人家已经说的很详细了。。。
参考:https://blog.csdn.net/weixin_42298068/article/details/113318925
另外,要获取国际化的消息,除了文章中的注入MessageSource实例的方法之外,还可以参考若依的方法,建立一个MessageUtils.java 工具类,如下:
package com.ruoyi.common.utils; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import com.ruoyi.common.utils.spring.SpringUtils; /** * 获取i18n资源文件 * * @author ruoyi */ public class MessageUtils { /** * 根据消息键和参数 获取消息 委托给spring messageSource * * @param code 消息键 * @param args 参数 * @return 获取国际化翻译值 */ public static String message(String code, Object... args) { MessageSource messageSource = SpringUtils.getBean(MessageSource.class); return messageSource.getMessage(code, args, LocaleContextHolder.getLocale()); } }
附带其中用到的 SpringUtils.java 工具类(部分必需的代码):
package com.ruoyi.common.utils.spring; import org.springframework.aop.framework.AopContext; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import com.ruoyi.common.utils.StringUtils; /** * spring工具类 方便在非spring管理环境中获取bean * * @author ruoyi */ @Component public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware { /** Spring应用上下文环境 */ private static ConfigurableListableBeanFactory beanFactory; private static ApplicationContext applicationContext; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtils.beanFactory = beanFactory; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtils.applicationContext = applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws org.springframework.beans.BeansException * */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } /** * 获取类型为requiredType的对象 * * @param clz * @return * @throws org.springframework.beans.BeansException * */ public static <T> T getBean(Class<T> clz) throws BeansException { T result = (T) beanFactory.getBean(clz); return result; } }
然后,如果要获取国际化的消息,可以这么获取:
MessageUtils.message("user.password.not.match")
2 坑
- 配置了 xxx_en_US.properties,但是始终无法使用,调试了几个小时,系统就是顽固地使用中文提示消息。
后来我灵机一动,是否是properties文件中有不规范的格式。于是我注释了所有的消息行,仅留下一行需要使用的。
然后再次测试,好了!提示英文消息了!
那么可能是哪个地方书写不规范,或者有不该出现的字符。
于是我放开了所有的注释,重新测试,还是正常的。那这就很奇怪了。
而且我的键值对的值,有些值是有空格的,有些值还带 * 号,有些键和值之间的等号两边还有空格,结果通通没问题,都可以。
小结:还是判断是“哪个地方书写不规范,或者有不该出现的字符”。
以后如果遇到这个问题,还是可以这么解决。
分类:
springBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理