Spring国际化

Spring国际化,意味着将程序中的字符串,统一写到不同语言的配置文件中,然后在程序运行期间,读取不同的配置语言文件,从而获取不同支持语言的字符用于程序中。具体需要如下设置:

1,配置applicationContext.xml文件。下面是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
>
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <!-- basenames确定资源文件名 -->
        <property name="basenames">
            <list>
                <value>lang</value>
            </list>

        </property>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
</beans>

2,创建词库配置文件。文件名称必须符合语言要求,比如lang_zh_CN.properties。其中lang就是上面代码中,标签basenames的值。需要几种语言支持,就需要几个文件,Idea会自动把语言配置文件归类到Resource Bundle包中,比如:

另外一点需要注意的是,每个properties文件的编码格式一定要是UTF-8,否则Spring无法正确读取到字符。

3,创建测试类,读取字符:

@Test
    public void fun(){
        ApplicationContext app= new ClassPathXmlApplicationContext("internationnalityContext.xml");
        //读取英文字符
        String mes=app.getMessage("message",null,"default",Locale.US);
        //读取中文字符
        String mess=app.getMessage("message",null,"default",Locale.SIMPLIFIED_CHINESE);
        //读取日文字符
        String messa=app.getMessage("message",null,"default",Locale.JAPAN);
        System.out.println(messa);
    }

输出结果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值