springMVC基于session的国际化实现

       springMVC的国际化功能主要是为了解决软件的多语言实现问题。

        以下是主要的配置信息:(spring+springMVC+myBatis框架,这里持久层框架不限)

第一:配置资源文件。

     所谓的资源文件,即是不同语言的对应信息,需要注意两点:其一是资源文件的命名规则,basename_language_country_varient.properties,language、country和variant的值需要是Locale类中的已有值。如下配置了两个资源文件,其中的信息都是以键值对的形式书写,而且两个文件中的key必须一致,这也是需要注意的第二点。如文件命名message_en_US.properties、message_zh_CN.properties,示例内容user.name=name。

      在xml文件中配置资源文件的信息,如编码、文件名前缀等。

<bean id="messageSource"
	class="org.springframework.context.support.ResourceBundleMessageSource">
	<!-- 默认的编码设置 -->
	<property name="defaultEncoding" value="UTF-8"></property>
	<!-- 默认的国际化资源文件名的前缀,可以有多个 -->
	<property name="basenames" value="message" />
</bean>

第二:拦截器配置。

       拦截器可以使用默认配置即可,当然也可以自定义,需要实现HandlerInterceptor。 

<!-- 
   配置 LocaleChanceInterceptor 拦截器 
   作用: 获取name=locale的请求参数 
-->
<mvc:interceptors>
	<!-- 自定义拦截器:用于处理国际化请求 -->
	<bean class="com.giser.controller.LanguageInterceptor"></bean>

	<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>

第三:配置localeResolver

<!-- 
    配置 SessionLocalResolver
    作用:将Locale对象设置为session的属性;从session中获取Locale对象 
-->
<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

第四:页面请求及解析方式

    页面请求:

<a href="i18n/lang?locale=en_US">英文</a><br/>  
<a href="i18n/lang?locale=zh_CN">中文</a><br/>

  页面解析:

            解析方式一:c标签

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:message key="user.name"></fmt:message>

            解析方式二:spring标签

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<spring:message  code="user.name" />

补充:自定义拦截器时的拦截器实现

/**
 * 基于session的springMVC国际化
 * 拦截器方法:用于设定请求的国际化语言
 * @author giserDev
 *
 */
public class LanguageInterceptor implements HandlerInterceptor{


    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
            throws Exception {  
        String language = request.getParameter("locale");  
        if (language != null&&language.equals("zh_CN")) {  
            Locale locale = new Locale("zh", "CN");  
            request.getSession().setAttribute(  
                            SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,  
                            locale);  
            request.setAttribute("language", language);  
        } else if (language != null&&language.equals("en_US")) {  
            Locale locale = new Locale("en", "US");  
            request.getSession().setAttribute(  
                            SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,  
                            locale);  
            request.setAttribute("language", language);  
        } else {  
            request.getSession().setAttribute(  
                    SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,  
                    LocaleContextHolder.getLocale());  
            language = LocaleContextHolder.getLocale().getLanguage();  
            request.setAttribute("language", language);  
        }  
        
        return true;  
    }  
    
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,  
            ModelAndView modelAndView) throws Exception {  
    }  
    
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  
            throws Exception {  
    }  

}

    其中en_US和zh_CN是国际化标准所定义的,更多信息如下:

国家/地区语言缩写代码国家/地区语言缩写代码
简体中文(中国)zh_CN繁体中文(中国台湾地区)zh_TW
英语(英国)en_GB英语(美国)en_US

如何查看国际化方面的信息?

   进入标准化国际组织主页,网址为:International Organization for Standardization。在Standards下找到Popular standards,即流行的国际标准,点击进入后可以看到有很多标准,如ISO 13485 Medical devices医疗设备、ISO 22000 Food safety management食品安全等,找到ISO 639 Language codes即语言编码即是我们所需要的信息,它的表述为Describe languages in an internationally accepted way with this standard。在点击打开的新页面中,点击Codes list,即可查看到关于语言的国际化标准信息。这里我直接粘贴过来了2017年更新的语言编码信息说明。

Codes for the Representation of Names of Languages

Codes arranged alphabetically by alpha-3/ISO 639-2 Code

Note: ISO 639-2 is the alpha-3 code in Codes for the representation of names of languages-- Part 2. There are 21 languages that have alternative codes for bibliographic or terminology purposes. In those cases, each is listed separately and they are designated as "B" (bibliographic) or "T" (terminology). In all other cases there is only one ISO 639-2 code. Multiple codes assigned to the same language are to be considered synonyms. ISO 639-1 is the alpha-2 code.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值