我目前尝试过的有三种实现方式:
每种方法适用场景:
三种方法的实现方式:
第一种
文件命名也类似这样xxxx.properties,xxxx_zh.properties,xxxx_en.properties这样的。
global_shop.properties里的字段设置
username=Personal
password=123456
submit=submit
username=\u4e2a\u4eba
password=123456
submit=\u63d0\u4ea4
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.ResourceBundle"%>
<%
//加载i18n资源文件,request.getLocale()获取访问用户所在的国家地区
ResourceBundle myResourcesBundle = ResourceBundle.getBundle("global/global_shop",request.getLocale());
%>
补充说明:”global/global_shop”中的global_shop是用默认语言文件的文件名,不要加文件后缀.properties,这方法会根据request.getLocale()来获得语言,然后拼接到默认的方法名后面,比如拿到的语言是en,拼接后就是global_shop_en,就能获得英文的语言文件。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="../comm/tag.jsp" %>
<html lang="zh-CN">
<head>
<base href="<%=basePath%>">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="1,2,3">
<meta name="description" content="">
<meta name="renderer" content="webkit">
<title><%=myResourcesBundle.getString("index.title")%>title>
<link href="<%=request.getContextPath() %>/resources/css/comm/base.css" rel="stylesheet">
<link href="<%=request.getContextPath() %>/resources/css/index/index.css" rel="stylesheet">
head>
<body>
<%@ include file="../comm/test/header.jsp" %>
<div class="clear pt-30 protipsplmedia pb-80-20-ipad cur-d">
<p class="line-h-30 h-30 f-16 f-w col-252525">
用户名: <%=myResourcesBundle.getString("username")%>
p>
<p class="line-h-20 h-40 f-14 col-8c8c8c pt-20">
密码: <%=myResourcesBundle.getString("password")%>
p>
div>
body>
html>
@RequestMapping(value="/test_global")
public String test_global() {
return "global/test_index";
}
第二种
举例页面实现:
<li class="pull-left pr-10"><a class="col-333 hovecol-black" href="${pageContext.request.contextPath}/changeLang?lang=en">Enga>li>
<li class="pull-left pr-5"><a class="col-333 hovecol-black" href="${pageContext.request.contextPath}/changeLang?lang=zh-HK">繁a>li>
<li class="pull-left pr-5"><a class="col-333 hovecol-black" href="${pageContext.request.contextPath}/changeLang?lang=zh-CN">简a>li>
@RequestMapping(value = "/changeLang", method = RequestMethod.GET)
public String changeLang(Model model,HttpSession session, HttpServletRequest request,String lang){
lang = LanguageUtils.getlang(lang);
session.setAttribute("lang", lang);
System.out.println("setAttribute_lang:"+lang);
return lang+"_index";
}
点击进入其他页面的时候,从session获得语言,再跳转到对应页面就可以了。
@RequestMapping(value = "/aboutBis", method = RequestMethod.GET)
public String index(HttpServletRequest request,HttpSession session){
String lang = (String) session.getAttribute("lang");
if (StringUtils.isEmpty(lang)) {
lang = "EN";
}
return "about/"+lang+"_About";
}
第三种:
@RequestHeader(“Accept-Language”)获取浏览器设置的优先语言
@RequestMapping(value="/displayHeaderInfo")
public String displayHeaderInfo(@RequestHeader("Accept-Language") String language) {
System.out.println("language:"+language);
String lang = getlang(language);
System.out.println("浏览器优先语言:"+getlang(language));
return "about/"+lang+"_About";
}
public static String getlang(String accept_language){
String[] lang_arr = accept_language.split(",");
String first_lang = lang_arr[0];
System.out.println("浏览器优先语言:"+first_lang);
if(first_lang.equals("zh")||first_lang.equals("zh-CN")){
return "ZH";
}if(first_lang.equals("zh-TW")||first_lang.equals("zh-HK")){
return "HK";
}else{
//默认英语
return "EN";
}
}