在Spring中处理I18N问题(ReloadableResourceBundleMessageSource)

 在Spring中处理I18N问题和使用Java里面的类基本上是一样的.使用org.springframework.context.support.ResourceBundleMessageSource

然后注入资源文件(一个名字为basename的属性),然后就可以在Context中使用资源文件了, 如下为一个配置示例: test.xml

<?

xml version = "1.0" encoding = "UTF-8" ?>

<!

DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

<

beans >

 

< bean id = "messageSource" class = "org.springframework.context.support.ResourceBundleMessageSource" >

 

< property name = "basename" >

 

<!-- 注意此处设置 资源 名字 和路径 -->

 

< value > test/i18n/test </ value >

 

</ property >

 

</ bean >

 

</

beans >

下面为资源文件 test.properties

name =

\u51B0\u96E8

 

sex =

\u5148\u751F

test_zh.properties

name =

\u51B0\u96E8

 

sex =

\u5148\u751F

test_en_US.properties

name =

ice rain

 

sex =

male

下面是一个简单的测试类:

package

test.i18n;

import

java.util.Locale;

import

org.springframework.context.ApplicationContext;

import

org.springframework.context.support.ClassPathXmlApplicationContext;

public

class TestI18n { /**

 

* @param args

 

*/

 

  public static void main(String[] args) {     // TODO Auto-generated method stub

    ApplicationContext context =

new ClassPathXmlApplicationContext( "test/i18n/test.xml" );

    String text = context.getMessage(

"sex" , new Object[0], Locale. US );

    String textZH = context.getMessage(

"sex" , new Object[0], Locale. CHINA );

    System.

out .println(text + " 中文:" +textZH);

  }

}

很简单,这样就可以了.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值