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 坑

  1. 配置了 xxx_en_US.properties,但是始终无法使用,调试了几个小时,系统就是顽固地使用中文提示消息。
    后来我灵机一动,是否是properties文件中有不规范的格式。于是我注释了所有的消息行,仅留下一行需要使用的。
    然后再次测试,好了!提示英文消息了!
    那么可能是哪个地方书写不规范,或者有不该出现的字符。

于是我放开了所有的注释,重新测试,还是正常的。那这就很奇怪了。

而且我的键值对的值,有些值是有空格的,有些值还带 * 号,有些键和值之间的等号两边还有空格,结果通通没问题,都可以。

小结:还是判断是“哪个地方书写不规范,或者有不该出现的字符”。
以后如果遇到这个问题,还是可以这么解决。

posted @ 2022-06-06 11:00  mediocrep  阅读(205)  评论(0编辑  收藏  举报
既然选择了远方,便只顾风雨兼程!