目录
支持多种语言(简、繁、英)
-
springmvc.xml配置
<!-- 以下是国际化的配置 -->
<!-- 1.国际化 - 存储区域设置信息SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中。-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- 2.国际化 - 资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:properties/messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 3.国际化 -->
<mvc:interceptors>
<!-- 该拦截器通过名为”language”的参数来拦截HTTP请求,使其重新设置页面的区域化信息 -->
<bean class="com.hkow.application.interceptor.LanguageInterceptor">
<!-- 通过拦截language来区分-->
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>
"classpath:properties/messages"对应resource目录下的properties文件夹下messages_en_US.properties、messages_zh_CN.properties、messages_zh_TW.properties文件。
目录结构:
-
语言拦截LanguageInterceptor
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
* 多语言国际化
* @author ex-liuq2
*
*/
public class LanguageInterceptor extends HandlerInterceptorAdapter {
private static Log logger = LogFactory.getLog(LanguageInterceptor.class);
/**
*
* 设置默认的语言参数parameter: "language"
*/
public static final String DEFAULT_PARAM_NAME = "language";
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 {
Locale newLocale = getLocale(request.getParameter(getParamName()));
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){
// 1. 中文
//Locale locale = new Locale("zh-CN", "CN");//默认中文
Locale locale = new Locale("zh","CN");//中文
// 2. 英文
if(language!=null && language.equals("en")){
//locale = new Locale("en", "US");
locale = new Locale("en","US");//
}
// 3. 繁体
if(language!=null && language.equals("zh-TW")) {
//locale = new Locale("zh-TW", "TW");
locale = new Locale("zh","TW");//
}
return locale;
}
}
-
国际化的使用
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
* 国际化相关
* @author:liuq
* @date:2018年10月15日
*
*/
public class INTUtil {
private static Log logger = LogFactory.getLog(INTUtil.class);
/**
* 从国际化资源配置文件中根据key获取value 方法一
*/
public static String getMessage(HttpServletRequest request, String key) {
Locale currentLocale = RequestContextUtils.getLocale(request);
String lang = currentLocale.getLanguage();// zh
String country = currentLocale.getCountry();//
String fileName = "properties/messages_" + lang+"_"+country;
logger.info("INTUtil 的currentLocale = "+currentLocale);//zh_CN
logger.info("INTUtil 的fileName = "+fileName);//
ResourceBundle bundle = ResourceBundle.getBundle(fileName, currentLocale);
return bundle.getString(key);
}
/**
* 从国际化资源配置文件中根据key获取value 方法二
* @param request
* @param key
* @return
*/
public static String getMessageContent(HttpServletRequest request, String key){
RequestContext requestContext = new RequestContext(request);
String value = requestContext.getMessage(key);
return value;
}
}
-
使用
使用 String str = INTUtil.getMessageContent(request, "main_cust_vcode_a")