spring 国际化遇到的问题总结
用Spring做国际化时经常会报:
org.springframework.context.NoSuchMessageException: No message found under code 'userlogin' for locale 'zh_CN'.
at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65)
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:646)
at com.neusoft.Test.Test.main(Test.java:43)
Exception in thread "main"
这样的错误。
at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65)
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:646)
at com.neusoft.Test.Test.main(Test.java:43)
Exception in thread "main"
这样的错误。
总结一下,有可能由以下原因造成:
1.如果你使用eclipse创建的工程是class和src分开的,那么资源属性文件一定要放在src目录以内。
2.属性文件名的写法:
messages_zh_CN.properties (中文)
messages_en_US.properties (英文)
3.配置messageSource这个bean(注意:一定是messageSource不是messageResource ,这是Spring规定的)
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
4,测试程序要这样写:
ApplicationContext context = new FileSystemXmlApplicationContext(
"bean.xml");
Object[] arguments = new Object[] { "gao kan",
Calendar.getInstance().getTime() };
System.out.println(context
.getMessage("userlogin", arguments, Locale.US));
System.out.println(context.getMessage("userlogin", arguments,
Locale.CHINA));
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
4,测试程序要这样写:
ApplicationContext context = new FileSystemXmlApplicationContext(
"bean.xml");
Object[] arguments = new Object[] { "gao kan",
Calendar.getInstance().getTime() };
System.out.println(context
.getMessage("userlogin", arguments, Locale.US));
System.out.println(context.getMessage("userlogin", arguments,
Locale.CHINA));
注意以上问题,就OK了。