当前位置: 移动技术网 > IT编程>开发语言>Java > 基于Session的国际化实现方法

基于Session的国际化实现方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

如何将我们网站的其它内容(如菜单、标题等)做国际化处理呢?这就是本篇要将的内容—>国际化。

在项目的spring.xml文件添加的内容如下

<mvc:interceptors>  
<span style="white-space:pre">  </span><!-- 国际化操作拦截器 如果采用基于(请求/session/cookie)则必需配置 -->  
  <bean class="org.springframework.web.servlet.i18n.localechangeinterceptor" />  
</mvc:interceptors>

在项目中的源文件夹resources中添加myproperties.properties、myproperties_zh_.properties、myproperties_en_.properties三个文件

下面是jsp页面的一些简单信息如下,仅仅是演示没考虑其他的:

<%@ page language="java" contenttype="text/html; charset=utf-8" 
  pageencoding="utf-8"%> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> 
 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>insert title here</title> 
</head> 
<% 
  locale name = (locale) session.getattribute("i18nlanguage"); 
  resourcebundle myresourcesbundle = resourcebundle.getbundle("myproperties",name); 
%> 
<body> 
  <a href="${pagecontext.request.contextpath}/index/findex.do?langtype=en&page=home">eng</a> | 
 <a href="${pagecontext.request.contextpath}/index/findex.do?langtype=zh&page=home"><%=myresourcesbundle.getstring("simplified")%></a> 
</body> 
</html>

后台action层代码如下:

package com.zhidao.oms.index; 
 
import java.util.locale; 
 
import javax.servlet.http.httpservletrequest; 
 
import org.springframework.stereotype.controller; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.requestparam; 
 
@controller 
@requestmapping("/index") 
public class indexaction { 
   
   
  @requestmapping("/findex") 
  public string findex(httpservletrequest request,@requestparam string langtype,string page){ 
 
  if(langtype.equals("zh")){ 
      locale locale = new locale("zh", "cn");  
      request.getsession().setattribute("i18nlanguage",locale);  
    } 
    else if(langtype.equals("en")){ 
      locale locale = new locale("en", "us");  
      request.getsession().setattribute("i18nlanguage",locale); 
    }else{ 
      request.getsession().setattribute("i18nlanguage",locale.getdefault()); 
    } 
    return "/front/"+page+".jsp"; 
  } 
   
}

有关的效果图展示大家测试一下就好了!写的不好的地方希望大家批评指正。

以上这篇基于session的国际化实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网