一:应用
多语言配置一般用在前端页面,后台返回的提示信息以及js中,在前后端交互过程中保证语言的一致性,一般采用cookie或者session来存取语言参数,以此保证同步。
二:springmvc配置
在springMVC中添加如下配置项
<!-- 存储区域设置信息
SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中。
-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- 国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!--
资源文件路径:名字应为message_en.properties和message_zh.properties
en zh 为语言参数
-->
<property name="basename" value="classpath:messages/message" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<mvc:interceptors>
<!-- 自定义拦截器拦截HTTP请求,使其重新设置页面的区域化信息 -->
<!-- <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> -->
<bean class="yuanlian.emailSubscription.Interceptor.LanguageInterceptor">
</bean>
</mvc:interceptors>
创建org.springframework.web.servlet.i18n.LocaleChangeInterceptor拦截器
package yuanlian.emailSubscription.Interceptor;
import java.util.Locale;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
* 语言拦截器
* @author CY
*
*/
public class LanguageInterceptor extends HandlerInterceptorAdapter{
/**
* Default name of the locale specification parameter: "locale".
*/
public static final String DEFAULT_PARAM_NAME = "locale";
private String paramName = DEFAULT_PARAM_NAME;
public void setParamName(String paramName) {
this.paramName = paramName;
}
public String getParamName() {
return this.paramName;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String locale = "zh";
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies){
if(cookie.getName().equals("locale")){
locale = cookie.getValue();
}
}
}
Locale newLocale = getLocale(locale);
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
localeResolver.setLocale(request, response, newLocale);
return true;
}
//根据language 获取Locale
public static Locale getLocale(String language){
Locale locale = new Locale("zh", "CN");
if(language!=null && language.equals("en")){
locale = new Locale("en", "US");
}
return locale;
}
}
创建资源文件:
message_en.properties
userName=username
age=age
message_zh.properties
userName=\u59D3\u540D
age=\u5E74\u9F84
三:前端配置
1:以session的形式存取,因为js无法操作session,所以一般同过ajax请求后台,将语言参数传至后台存到session中,在保存到隐藏表单域中供js取用即可。
2:以cookie的形式存取,通过js将语言参数存到cookie中,在js和后台代码中分别取值。(以下为代码实例)
将语言参数存至cookie中(js文件)
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); /*cookie过期时间*/
$(function(){
$("#chinese").click(function(){
document.cookie = "locale" + '=' + escape("zh") + ';expires=' + exp.toGMTString();
location.reload();
});
$("#english").click(function(){
document.cookie = "locale" + '=' + escape("en") + ';expires=' + exp.toGMTString();
location.reload();
});
})
建立js语言资源文件
var arr,reg=new RegExp("(^| )"+"locale"+"=([^;]*)(;|$)");
var locale;
if(arr=document.cookie.match(reg)){
locale = unescape(arr[2]);
}else{
locale = "zh";;
}
var edwin;
if(locale == "en"){
edwin = "asdadasdasdas"
}
if(locale == "zh" || locale == null){
edwin ="中文"
}
四:取值
1:java取值,在 message*.properties文件中取值,使用一个util类的静态方法传入key获取value
package yuanlian.emailSubscription.util;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;
public class LocaleMessageUtil {
/**
* 从国际化资源配置文件中根据key获取value 方法一
* @param request
* @param key
* @return
*/
public static String getMessage(String key){
ServletRequestAttributes RequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
Locale currentLocale = RequestContextUtils.getLocale(RequestAttributes.getRequest());
String lang = currentLocale.getLanguage();
ResourceBundle bundle = ResourceBundle.getBundle("messages_"+lang, currentLocale);
return bundle.getString(key);
}
/**
* 从国际化资源配置文件中根据key获取value 方法二
* @param request
* @param key
* @return
*/
public static String LocaleMessage(String key){
ServletRequestAttributes RequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContext requestContext = new RequestContext(RequestAttributes.getRequest());
String value = requestContext.getMessage(key);
return value;
}
}
2:jsp/ftl取值:在 message*.properties文件中取值
<!--userName为资源文件中的key-->
<spring:message code="userName" />
3:js取值:在js资源文件中取值,引入资源js,直接将key当成普通变量即可
$(function(){
$("#test").click(function(){
alert(edwin);
});
})