使用JSP的fmt标签实现国际化支持。
使用JSP的fmt标签配置i18n国际化资源文件可以实现根据不同的地区和语言切换不同的显示.
JSP 实例
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<c:out value="${system_language}" escapeXml="false" default="defaultValue" />
<fmt:setBundle basename="i18n/i18n_${system_language}"/>
<div id="footer">
<div id="copyright">
<span><fmt:message key="common.copyright"/></span>
</div>
<div id="language">
<span><fmt:message key="common.language"/>:</span>
<a href="#" data-value="zh_CN">中文</a>
<span>|</span>
<a href="#" data-value="en_US">English</a>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#language').find('a').click(function () {
var language = $(this).data('value');
document.cookie = "cookie_language=" + language +
";expires=365";
location.reload();
});
});
</script>
</body>
</html>
2. 在src/main/resources 路径下新建i18n文件夹(或者在其他的路径下也可以,编译之后,文件夹要在classes目录下),新建资源文件 i18n_en_US.properties 和 i18n_zh_CN.properties,内容如下:
i18n_en_US.properties:
common.copyright=\u7248\u6743\u6240\u6709 \u00A9 2013
common.language=\u7cfb\u7edf\u8bed\u8a00
common.bookname=\u56fe\u89e3HTTP
i18n_zn_CN.properties:
common.copyright=Copyright \u00A9 2013
common.language=language
common.bookname=HTTP by picture
在服务器端添加一个filter,在请求中判断需要显示哪种语言,通过设置system_language的值,使页面的显示发生变化:
实例
package com.runoops.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
* Servlet Filter implementation class FirstFilter
*/
public class FirstFilter implements Filter {
private FilterConfig filterConfig = null;
String paramValue = null;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
paramValue = filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("begin headers-------------------");
Enumeration<?> headerNames = ((HttpServletRequest)request).getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
System.out.println(headerName + ": " + ((HttpServletRequest)request).getHeader(headerName));
}
System.out.println("end headers-------------------");
//在调用目标前写入响应内容
response.setContentType("text/html; charset=UTF8");
PrintWriter out = response.getWriter();
out.println("IP地址为:" + request.getRemoteHost() + "<br>");
chain.doFilter(request, response);
//在目标返回后写入响应内容
out.println("<br>名称为encoding的初始化参数的值为:" + paramValue);
out.println("<br>当前Web程序的真实路径为:" + filterConfig.getServletContext().getRealPath("/"));
//out.println("<br>修改了test.html文件!");
}
@Override
public void destroy() {
this.filterConfig = null;
}
}
我们通过request.setAttribute("system_language",${system_language}); 来改变JSP中的bundle的名字,实现切换显示语言的功能,getSystemLanguage()方法,先是从cookie中找显示语言,若没有找到,则使用浏览器的语言,如果是空,则取操作系统的默认语言。
为了能够动态显示出来效果,我们提供两个按钮,点击之后可以切换语言: