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
孤飞燕 阅读(2619)
评论(0) 编辑 收藏 所属分类:
Spring