美文网首页
spring国际化消息配置

spring国际化消息配置

作者: 小狼在IT | 来源:发表于2018-09-06 14:24 被阅读0次

    1.配置一个ReloadableResourceBundleMessageSource的bean:

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找  -->
                <value>classpath:i18n/messages/messages</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="false"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
    </bean>
    

    2.国家化消息文件:


    image.png image.png

    3.使用:

    messageSource.getMessage(String.valueOf(result.getCode()), new Object[]{},Locale.SIMPLIFIED_CHINESE);
    

    bean的设置只是定位到文件的前缀 messages
    而后面的 zh_CN 由 Locale.SIMPLIFIED_CHINESE 获得。

    相关文章

      网友评论

          本文标题:spring国际化消息配置

          本文链接:https://www.haomeiwen.com/subject/fwlxgftx.html