SpringMVC国际化

一、java后台国际化
1、app-resources.xml配置国际化信息

<!-- add by fengzf 2016-10-25 -->
  <!-- 定义国际化消息 -->  
   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  
     <property name="basename" value="messages.Messages"/> 
      <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->              
    <property name="useCodeAsDefaultMessage" value="true" />   
   </bean>  
 
      <mvc:interceptors> 
    <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
           <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 
      </mvc:interceptors> 
     
      <!-- 获取本地 --> 
   <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
            <property name="defaultLocale" value="zh_CN"></property>
   </bean>

在这里插入图片描述
basename文件目录结构
在这里插入图片描述
以中文文件为例
在这里插入图片描述
其他语言代码一致,内容不同,代码不要重复定义

2、配置国际化监听(web.xml)

	<!-- 国际化监听 -->
	<listener>
		<listener-class>com.gstarcad.listener.ApplicationContextLoaderListener</listener-class>
	</listener>
	<!-- add by fengzf 2016.10.27 -->
	<listener>
   		 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>

监听内容

package com.gstarcad.listener;

import javax.servlet.ServletContextEvent;

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.gstarcad.util.MessageUtil;

public class ApplicationContextLoaderListener extends ContextLoaderListener{
	@Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		MessageUtil.setApplicationContext(WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()));
	}
}

3、国际化工具类MessageUtil.java

package com.gstarcad.util;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

public class MessageUtil  {
	private static WebApplicationContext applicationContext;  

	
	public static void setApplicationContext(WebApplicationContext context){
		applicationContext=context;
	}
	
	public static String getMessage(String code,HttpServletRequest request){
		Locale  locale=null;
		Object obj=request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
		if(obj!=null){
			locale = (Locale)obj;
		}else{
			locale =  request.getLocale();
			request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
		}
		 
		return applicationContext.getMessage(code,null, locale);
	}
	

}

4、调用:1、直接在BaseAction中定义,以后在Action中调用时getMessage(code)

	/**
	 * 获取国际化信息
	 * @param code
	 * @return
	 */
	public String getMessage(String code){
		return MessageUtil.getMessage(code, request);
	}
	
gsonEntity.setMsg(getMessage("post_error_message"));

code为资源包里的编码,会根据不同的语言环境选择不同资源下的内容
2、不在BaseAction定义,可直接使用MessageUtil.getMessage(code,request)

二、jsp国际化(利用spring标签)
1、要有上面第一步的配置(同上)
2、需要spring标签工具文件
目录结构
在这里插入图片描述
3、调用:
1)
引入标签

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
 <%@ taglib prefix="spring" uri="/WEB-INF/spring.tld" %>

调用

<!-- 热门帖子 -->
<spring:message code="hotcms"/>

根绝code码hotcms去查找资源,找不到直接返回hotcms
2)

${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'].language}

返回不同的语言编码,例如中文zh

三、纯js国际化

第一步:创建properties资源文件。
properties资源文件命名规则为:string_浏览器语言简码.properties,例如简体中文:string_zh-CN.properties这里需要注意是中划线而不是下划线,如图我创建了三个资源文件
js_en-US.properties(美国英语),js_ja.properties(日语),js_zh-CN.properties(简体中文)。
在这里插入图片描述
第二步:在js文件中引入jQuery.i18n.properties所需js文件。
因为jQuery.i18n.properties是依赖于Jquery框架的,所以需要在你的js文件中引入jQuery.i18n.properties所需js文件。
在这里插入图片描述
导入红色部分的js即可使用jQuery.i18n.properties了。
第三步:使用jQuery.i18n.properties API

<script type="text/javascript">
               $(document).ready(function(){
               //国际化加载属性文件
                       jQuery.i18n.properties({
                           name:'js',
                           path:'<%=path%>/js/i18n/',
                           mode:'map',
                           callback: function() {// 加载成功后设置显示内容
                               //alert(jQuery.i18n.prop("theme_manage.js_activity"));
                           }
                       });
        });
</script>

其中:name后面的值为你定义的资源文件中语言简码前面的字符串,因为我的资源文件为js_xxx.properties,所以这个值就为js
path后面的值为你资源文件的相对路径。即相对于工程结构WebContent下的路径所在
在这里插入图片描述

mode后面的值为加载模式;"vars"表示以JavaScript变量或函数的形式加载资源文件中的key值(默认为这种),“map”表示以map的方式加载资源文件中的key值。“both表示可以同时使用这两种方式”。我这里使用的是map。
callback为回调函数。
是怎样根据不同的语言环境加载不同的资源文件的呢?其实,jQuery.i18n.properties实现的原理就是,根据name后面的值,加上浏览器的语言简码,再加上.properties找到对应的资源文件。这个过程是自动的,只需要进行上面的配置即可。
propertites中键值对如下:(在properties文件中中文会自动转换成相对应的ASCII值,当然这里是可以设置的,也可以通过插件进行更改的,我这就没做了反正这些中文是从页面上复制出来的,不管了),等号前的为key,等号后的为值(注意一点的是,不同的资源文件中key必须保持一致,是自定义的)。
在这里插入图片描述
这样资源文件中的内容已经加载完成了。
第四步:js文件中根据key找对应的值。
在这里插入图片描述
红色部分就是取值的方式,引号中的字符串对应着上面资源文件中的key值。
需要注意的地方:
此上方法在谷歌和火狐的浏览器中实现完全没有问题。但在IE浏览器中会出现问题,问题在使用IE浏览器每次获取到的语言环境为系统的语言,而不是浏览器的语言。
这个问题我也纠结了很久,网上提供了一些方法来获得浏览器的语言,但是在IE中却不起作用,最后我的解决方法是:在使用jQuery.i18n.properties加载资源文件之前,在request头信息中先获取浏览器的语言,然后设置。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snowflakefengzf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值