Spring 国际化资源

Spring 国际化资源
    以前,在学习Struts2的时候经常听到关于国际化的消息。记得Struts2国际化的时候我们需要在cmd下面编译资源文件,但是Spring在资源国际化的时候则不需要这么麻烦。而且如果你是使用Eclipse或者MyEclipse的话,当你新建好message_en_US.properties、message_zh_CN.properties资源文件之后。如果是在 message_zh_CN.properties文件中,当你输入中文的时候其会自动将其转换为unicode编码。这样就省去了cmd下的编译国际化。相对而言还是挺方便的。 使用Eclipse或者MyEclipse的时候, message_en_US.properties、message_zh_CN.properties资源文件必须放在src的目录下,否则编译的时候找不到资源文件下面给一个具体例子吧:
     package com.clark;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class UserBean implements InitializingBean, DisposableBean {
    private String username;
    private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void init() {
        this.username = "zhangsan";
        this.age = 20;
    }

    public void clear() {
        this.username = "";
        this.age = 0;
    }
    //销毁
    public void destroy() throws Exception {
        this.username = "";
        this.age = 0;
    }
    //在调用properties文件后初始化设值
    public void afterPropertiesSet() throws Exception {
        this.username = "wangwu";
        this.age = 20;
    }

}
package com.clark;

import java.util.Date;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
//测试类
public class Test {
    public static void main(String[] args) {
        ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
        UserBean userBean=(UserBean)context.getBean("user");
        System.out.println("username="+userBean.getUsername());
        System.out.println("age="+userBean.getAge());
        //之所以使用Object[]数组,是因为其资源配置文件.properties中对应的参数有两个
        Object[] objs=new Object[]{userBean.getUsername(),new Date().toString()};
        //获取资源文件,Locale.CHINA指定采用何种语言
        String str=context.getMessage("www.clark.com", objs, Locale.CHINA);
        System.out.println(str);
    }
}
资源文件是:message_en_US.properties下为:
www.clark.com=welcome {0},now time is\:{1}
message_zh_CN.properties中为:(最原始为www.clark.com=欢迎 {0},现在时间是:{1})------这就是编译器的强大所在
www.clark.com=\u6B22\u8FCE {0},\u73B0\u5728\u65F6\u95F4\u662F\:{1}

applicationContext.xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="user" class="com.clark.UserBean"
        abstract="false" singleton="true"
        lazy-init="default" autowire="default"
        dependency-check="default" destroy-method="clear">
    </bean>
    <!-- 配置国际化 配置messageSource这个bean(注意:一定是messageSource
        而不是messageResource ,这是Spring规定的),而且资源配置*_en_US.properties等文件必须位于src目录下
        否则,加载的时候找不到,会报错
    -->

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
        abstract="false" singleton="true" lazy-init="default"
        autowire="default" dependency-check="default">
        <!-- 国际化资源文件.properties中对应的key名称 -->
        <property name="basenames">
            <list>
                <!-- 此值对应资源文件的名称message_en_US中的message必须与其对应 -->
                <value>message</value>
            </list>
        </property>
    </bean>
</beans>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值