ResourceBundleMessageSource配置
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basenames" value="messages"></property>
<property name="useCodeAsDefaultMessage" value="true"></property>
</bean>
message是你的properties文件的通用名,
例如中文messages_zh_CN.properties,英文messages_en_US.properties
如果想把properties文件放到某一个包下,如:com.diyjava.language下
那怎么配置呢
<property name="basenames" value="com.diyjava.language.messages"></property>
jsp文件
messages_zh_CN.properties内容如下
app.content= 哈哈,欢迎您!
那怎样国际化jsp呢
jsp引入spring标签
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
在需要显示地方输入如下标签
<spring:message code="app.content"/>
注:可以用jstl标签,如<fmt:message key="app.content" />
如果带有参数呢,那又怎么办呢?比如:哈哈,xxx,欢迎您!
messages_zh_CN.properties内容如下
app.content=哈哈, {0},欢迎您!
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code="app.content" arguments="${userName}"/>
userName是后台返回的参数
动态国际化
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
@Autowired
private SessionLocaleResolver localeResolver;
@RequestMapping("login.do")
public String login(String local,HttpServletRequest request,HttpServletResponse response){
if("zh".equals(local)){
localeResolver.setLocale(request, response, Locale.CHINA);
}else if("en".equals(local)){
localeResolver.setLocale(request, response, Locale.US);
}
return "login";
}
<div style="margin-left: 0px;">
<a href="?local=en">英文</a> <a href="?local=zh">中文</a>
</div>
http://localhost:8080/MyTest2/login.do?local=en