spring国际化

<beans
		xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:p="http://www.springframework.org/schema/p"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

     
     <!-- 该bean就会负责去加载国际化资源文件 -->
     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
     	<property name="basenames">
     		<list>
     			<!-- 方法里面传的是basename,mess就是basename,en是语言,US是国家 -->
             <!-- 本项目的两个资源文件分别为:中文mess_zh_CN.properties  英文mess_en_US.properties -->
                    <value>mess</value>
             </list>
        </property>
     </bean>
     
</beans>



mess_zh_CN.properties文件

hello={0},您好,欢迎您!
loginTitle={0}恭喜你啊!{1}

mess_en_US_properties文件

hello={0},Hello,You Are Welcome!
loginTitle={0}gong xi{1}




package org.spring.test;

import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//ApplicationContext 会预初始话容器中的singleton Bean.即:创建Spring容器时,会立即创建容器中的Bean
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
		
		System.out.println(applicationContext.getMessage("hello",new String[]{"老孙"},Locale.CHINA));
		System.out.println(applicationContext.getMessage("hello",new String[]{"老猪"},Locale.US));
		System.out.println(applicationContext.getMessage("loginTitle",new String[]{"haha","lala"},Locale.US));
		
		
	}

}






Note:
用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"

这样的错误。
有可能由以下原因造成:
1.如果你使用eclipse创建的工程是class和src分开的,那么资源属性文件一定要放在src目录以内。
2.属性文件名的写法:
mess_zh_CN.properties  (中文)
mess_en_US.properties  (英文)
3.配置messageSource这个bean(注意:一定是messageSource不是messageResource ,这是Spring规定的)
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mess</value>
</list>
</property>
</bean>
4.如果你的资源文件不是在根目录下而在org.spring.messages目录下的话,你就应该这样配置了:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
   <list>
    <value>org.spring.messages.mess</value>
   </list>
  </property>
 </bean>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值