在制作网站时候,使用一般公司(特别是小公司)都希望有英文版的网站 所以就用到了国际化 废话不多说 我说的是下面的一种(还有使用cookie,url等方法)
首先要有自己的springmvc环境(不多说,需要的可以在最下面下载)
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!--这里是基于session的-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- 资源文件绑定器 (这里配置的是messages 根据配置文件绑定的名称查找 没找到就用相似的文件,最好还是准确的文件)-->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="messages" />
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
在spring配置文件中就需要这么多(需要其他的可以在下面项目中直接下载完整版项目)
下面是建立你需要用的要国际化文件,我这里是messages_en.properties,messages_zh_CN.properties
messages_en
money=hello
messages_zh_CN
money=\u4F60\u597D
下面是请求文件
引入标签
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/spring/global/test1.action?local=zh">中文</a>
<a href="/spring/global/test1.action?local=en">英文</a>
<spring:message code="money"></spring:message>
</body>
</html>
下面就是关键的切换系统语言的方法
@RequestMapping(value="/test1", method = {RequestMethod.GET})
public String test1(HttpServletRequest request,String local,HttpServletResponse response){
if("zh".equals(local))
resolver.setLocale(request, response, Locale.CHINA);
else if("en".equals(local))
resolver.setLocale(request, response, Locale.ENGLISH);
return "welcome1";
}