1. 根據瀏覽器語言配置國際化
1.1 例如國際化配置文件internationalization.xml,放置在spring/appServlet/文件夾下,可根據需要具體放置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!-- Internationalization start -->

<bean id="messageSource" class="com.util.ResourceBundleMessageSourceExtend">

<property name="basename">

<value>config/account/messages-account </value>

</property>

<property name="useCodeAsDefaultMessage" value="true" />




</beans>




<!-- Internationalization end -->


</bean>

注:此配置文件加載國家化語言配置文件,value中值表示在當前src下的config/core文件件下的語言配置文件,例如:
可根據模塊配置多個不同的配置文件以逗號放置在value中,例如:
1.2 加載internationalization.xml
在web,xml需要配置,加載國際化配置文件
例如:
<context-param>
<param-name>contextConfigLocation</param-name>

<param-value>

classpath:spring/appServlet/internationalization.xml

</param-value>

</context-param>

1.3 國際化頁面的展現
引入spring或者jstl的標簽庫,例如:
<%@ taglib uri="/spring"
prefix="spring"%>,也可手動引入spring.tld,那么在uri中寫入spring.tld所在位置,例如:/WEB-INF/tag/spring.tld
在需要國際化的jsp中引入標簽:
<spring:message code="CM01S01_Label_Password " />
code中可根據需要寫入國際化語言配置的key值
1.4 啟動加載國際化語言的類以及后臺國際化
1.4.1 ResourceBundleMessageSourceExtend.java類
package com.core.util;

import org.springframework.context.support.ResourceBundleMessageSource;


/** *//**
* ResourceBundleMessageSourceExtend
*
* @author liu_dawei
*
*/

public class ResourceBundleMessageSourceExtend extends ResourceBundleMessageSource
{

private String[] extendBaseNames;


/** *//**
*
* <p>
* setBasenames
* </p>
*
* @param basenames
* @see org.springframework.context.support.ResourceBundleMessageSource#setBasenames(java.lang.String[])
*/
@Override

public void setBasenames(String[] basenames)
{
// TODO Auto-generated method stub
String tempBaseName = basenames[0].toString();
int length = tempBaseName.split(",").length;
extendBaseNames = new String[length];


for (int i = 0; i < length; i++)
{
extendBaseNames[i] = tempBaseName.split(",")[i];
}

super.setBasenames(extendBaseNames);
}

}

1.4.2 MessageManager類

/** *//**
* MessageManager.java
*
* @screen core
* @author liu_dawei
*/
package com.core.util;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;


/** *//**
* The internationalization message manager class
*
* @author liu_dawei
*
*/

public class MessageManager
{


/** *//** the single instance */
private static MessageManager instance;


/** *//**
* <p>
* The Constructors Method.
* </p>
*
*/

private MessageManager()
{}


/** *//**
* <p>
* get the single instance of MessageManager.
* </p>
*
* @return the single instance
*/

public static MessageManager getInstance()
{


if (null == instance)
{
instance = new MessageManager();
}

return instance;
}


/** *//**
* get Message
*
* @param key properties key
* @return the message
*/

public String getMessage(String key)
{
return this.getMessage(key, null);
}


/** *//**
* get Message
*
* @param key properties key
* @param obj the parameters
* @return the message
*/

public String getMessage(String key, Object[] obj)
{
// TODO
ApplicationContext actx = null;
actx = WebApplicationContextUtils.getWebApplicationContext(getHttpServletRequest().getSession()
.getServletContext());

String value = "";
value = actx.getMessage(key, obj, getLocale());


if (StringUtil.isEmpty(value))
{
value = key;
}
return value;
}


/** *//**
* get Message
*
* @param key properties key
* @param obj the parameters
* @return the message
*/

public String getMessage(String key, Object[] obj, Locale locale)
{
// TODO
ApplicationContext actx = ApplicationContextManager.getInstance().getApplicationContext();

String value = actx.getMessage(key, obj, locale);


if (StringUtil.isEmpty(value))
{
value = key;
}
return value;
}


/** *//**
* get the Locale info
*
* @return the Locale info
*/

public Locale getLocale()
{

HttpServletRequest request = getHttpServletRequest();

LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

return localeResolver.resolveLocale(request);

}


/** *//**
*
* <p>
* getIELanguage
* </p>
*
* @return
*/

public String getIELanguage()
{

return getHttpServletRequest().getLocale().getLanguage();
}


/** *//**
* <p>
* get the request.
* </p>
*
* @return the request
*/

public HttpServletRequest getHttpServletRequest()
{
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();

if (servletRequestAttributes != null)
{
return servletRequestAttributes.getRequest();

} else
{
return null;
}
}

}

注:getMessage(String key, Object[] obj) 中obj的參數傳的國際化語言中附帶的參數,例如:
Common_E0014=Input value of {0} is too short. Minimum {1} character
Obj[0]=”a”,obj[1]=”b”,如果調用getMessage(“Common_E0014”,obj),那么形成的值為:Input value of a is too short. Minimum b character
getLocale()方法用于spring Session國際化的,只要session中有Locale,就會使用session中的locale,以下會詳解
2. Spring Session國際化
1.1 與瀏覽器國際化相比需要在internationalization.xml中加入以下配置
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"></property>
</bean>


<bean id="controllerClassNameHandler"
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
<list>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</list>

</property>
</bean>
1.2 調用jsp中改變語言的按鈕時,需要把變化的語言傳到后臺,并放置在session中,例如:
// language 頁面傳入的語言
String newLanguage = language;

if (!StringUtil.isEmpty(newLanguage))
{
// get the locale resolver
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

if ("zh".equals(newLanguage))
{
localeResolver.setLocale(request, response, Locale.CHINA);

} else if ("en".equals(newLanguage))
{
localeResolver.setLocale(request, response, Locale.ENGLISH);

} else if ("jp".equals(newLanguage))
{
localeResolver.setLocale(request, response, Locale.JAPAN);

} else
{
localeResolver.setLocale(request, response, Locale.ENGLISH);
}

} else
{
////此處使用的外界傳入locale,如果默認進入主頁面想使用瀏覽器的,可調用request.getLocale().getLanguage();
newLanguage = locale.getLanguage();
}
其他使用方法類似------------------------------end
posted on 2012-11-01 11:00
孤飛燕 閱讀(2621)
評論(0) 編輯 收藏 所屬分類:
Spring