读书人

在Spring中处理I18N有关问题(Reloada

发布时间: 2012-11-18 10:51:21 作者: rapoo

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

? }

}

很简单,这样就可以了.

读书人网 >软件架构设计

热点推荐