首页天道酬勤SpringBoot中使用MessageSource的getMessage获取i18n资源文件中的消息

SpringBoot中使用MessageSource的getMessage获取i18n资源文件中的消息

admin 03-18 14:34 32次浏览

场景

默认情况下,springboot支持国际化,不需要做太多配置,只需要在resources/下创建国际配置文件,注意名称必须从messages开始。messages.properties(默认语言配置文件,当找不到其他语言配置时,使用该文件进行显示)。

例如,在resoure下新建i18n目录,在此目录下新建message.properties

在这个资源文件中配置一些信息映射,以便在需要使用的代码中使用左边的code获取信息,并在显示时显示右边的信息。

所以如果要修改显示的信息,直接修改文件右侧显示的文本,则不需要在每个代码中进行修改。

注:

关注公众号

专横的程序猿

获取与编程相关的电子书、教程推送和免费下载。


实现

首先上面新建了资源文件message.properties


not.null=* 必须填写

user.jcaptcha.error=验证码错误

user.jcaptcha.expire=验证码已失效


添加一些自定义消息。


然后需要告诉spring国际化资源文件的位置。


打开application.yml


spring:

  # 资源信息

  messages:

    # 国际化资源文件路径

    basename: i18n/messages


配置完成后新建一个工具类MessageUtils


import org.springframework.context.MessageSource;

import org.springframework.context.i18n.LocaleContextHolder;

import com.common.utils.spring.SpringUtils;


/**

 * 获取i18n资源文件

 *

 */

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());

    }

}


因为这是在工具类中,属于不受Spring管理的环境,所以通过


SpringUtils.getBean(MessageSource.class);

1.

来获取MessageSource这个Bean


工具类代码


package com.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.stereotype.Component;


/**

 * spring工具类 方便在非spring管理环境中获取bean

 *

 */

@Component

public final class SpringUtils implements BeanFactoryPostProcessor

{

    /** Spring应用上下文环境 */

    private static ConfigurableListableBeanFactory beanFactory;


    @Override

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException

    {

        SpringUtils.beanFactory = beanFactory;

    }


    /**

     * 获取对象

     *

     * @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;

    }


    /**

     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true

     *

     * @param name

     * @return boolean

     */

    public static boolean containsBean(String name)

    {

        return beanFactory.containsBean(name);

    }


    /**

     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)

     *

     * @param name

     * @return boolean

     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException

     *

     */

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException

    {

        return beanFactory.isSingleton(name);

    }


    /**

     * @param name

     * @return Class 注册对象的类型

     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException

     *

     */

    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException

    {

        return beanFactory.getType(name);

    }


    /**

     * 如果给定的bean名字在bean定义中有别名,则返回这些别名

     *

     * @param name

     * @return

     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException

     *

     */

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException

    {

        return beanFactory.getAliases(name);

    }


    /**

     * 获取aop代理对象

     *

     * @param invoker

     * @return

     */

    @SuppressWarnings("unchecked")

    public static <T> T getAopProxy(T invoker)

    {

        return (T) AopContext.currentProxy();

    }

}


获取到MessageSource后,直接调用其getMessage方法,传递一个code参数和Local参数,code就是在


资源文件中配置的左边的 编码


Locale参数的获取是通过


LocaleContextHolder.getLocale()

1.

可以看MessageSource接口的源码


package org.springframework.context;


import java.util.Locale;

import org.springframework.lang.Nullable;


public interface MessageSource {

    @Nullable

    String getMessage(String var1, @Nullable Object[] var2, @Nullable String var3, Locale var4);


    String getMessage(String var1, @Nullable Object[] var2, Locale var3) throws NoSuchMessageException;


    String getMessage(MessageSourceResolvable var1, Locale var2) throws NoSuchMessageException;

}


来获取


工具类封装完成,怎样根据code获取消息


String message = MessageUtils.message("user.jcaptcha.expire")


安卓自定义相机拍照功能全解(不调用系统相机) JS操作XML中DTD介绍及使用方法分析