基于Spring 国际化

 
<!-- Spring 国际化 begin -->
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="defaultEncoding" value="UTF-8" />
		<property name="basenames">
			<list>
				<value>classpath:valid/validation</value>
				<value>classpath:local/message</value>
			</list>
		</property>
	</bean>

	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
		<property name="defaultLocale" value="zh" /> 
	</bean>
	
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
	</mvc:interceptors>

 以上是Spring 国际化 所需要配置的,

有了interceptors,controler 就不用设置了,类似于下面

Locale locale = null;
    locale = new Locale("zh", "CN");  //中文
    //locale = new Locale("en", "US");   //英文
    request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale); 

页面设置如下:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

当前引用的是Spring 标签, 也可以用fmt

<span>
  <a href="?locale=zh_CN">中文</a> | <a href="?locale=en_US">English</a>
 </span>
 <spring:message code="hello" /> 

以上就实现了国际化

猜你喜欢

转载自ailongni.iteye.com/blog/2071784