只是把自己做的DEMO放在这儿,没有涉及原理。
首先在Spring的XML中配置
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
其中Bean的ID和class是固定的。messages是spring会在classpath下寻找message*.*的文件。比如message文件为messages_zh_CN.properties。在文件中写了userinfo=当前登录用户: [{0}] 登录时间:[{1}]。在messages_en_US.properties中写了userinfo=Current Login user: [{0}] Login time:[{1}]。我们在测试代码中写
ApplicationContext ctx=new FileSystemXmlApplicationContext("bean.xml");
Object[] arg = new Object[]{"Erica",Calendar.getInstance().getTime()};
//以系统默认Locale加载信息(对于中文WinXP而言,默认为zh_CN)
String msg = ctx.getMessage("userinfo", arg,Locale.CHINA);
System.out.println("Message is ===> "+msg);
则会输出相应国家的文字。如果有乱码可以native2ascii messages_zh_CN.properties msg.txt将文件的内容重新编码。native2ascii messages_zh_CN.properties -reserve msg.txt为将文件内容本地化。
这样就达到了message国际化。
1459

被折叠的 条评论
为什么被折叠?



