在Spring中处理I18N有关问题(ReloadableResourceBundleMessageSource)
在Spring中处理I18N问题和使用Java里面的类基本上是一样的.使用org.springframework.context.support.ResourceBundleMessageSource
然后注入资源文件(一个名字为basename的属性),然后就可以在Context中使用资源文件了, 如下为一个配置示例: test.xml
<? <! <
< bean id = "messageSource" class = "org.springframework.context.support.ResourceBundleMessageSource" >
< property name = "basename" >
<!-- 注意此处设置 资源 名字 和路径 -->
< value > test/i18n/test </ value >
</ property >
</ bean >
</
下面为资源文件 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 import import import public
* @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);
}
}
很简单,这样就可以了.