文本和日期的国际化
程序员文章站
2022-05-16 10:30:14
...
软件的意义在于更好的服务生活,那么一款好的软件不止美国人能用,中国人能用,还应该 所有区域人都能用。
那么有一个问题,不同区域的人语言不通,软件设计者必须在软件当中放入多个语言支持包。以满足不同人的需要。
那么笔者就用一个简单的例子,来实现文本和日期的国际化。
首先新建一个jsp,用于显示日期和文本,切记新建一个目录放入jsp,这样方便过滤器的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri ="http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>文本和日期国际化</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%--绑定资源包 message为资源包前缀--%>
<fmt:setBundle basename="message"/>
<%--设置为中文资源包 作用域为整个应用--%>
<fmt:setLocale value="zh_CN" scope="application" />
<body>
<%--生产一个当前日期对象 --%>
<jsp:useBean id="dateId" class = "java.util.Date" scope ="page"></jsp:useBean>
<%--
value: 需要转换的日期时间值
type:选择日期或者时间或者日期时间生效.
--%>
<fmt:formatDate value="${ dateId }" dateStyle = "full" timeStyle = "long" type = "both"/>
<table align = "center" border = "1px" >
<tr>
<th><fmt:message key = "username"></fmt:message></th>
<td><input type = "text" name ="userName"></td>
</tr>
<tr>
<th><fmt:message key = "password"></fmt:message></th>
<td><input type = "password" name = "pwd"></td>
</tr>
<tr>
<th><input type = "submit" value = "<fmt:message key = "login"></fmt:message>"></th>
</tr>
</table>
</body>
</html>
然后新建一个class作为监听器,避免在中文状态下页面乱码
package com.chauncy.i18n;
import java.io.IOException;
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;
import javax.servlet.http.HttpServletResponse;
//防止中文乱码
public class ChineseFilter implements Filter {
@Override
public void destroy() {
System.out.println("过滤器销毁了");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
chain.doFilter(req, resp);
System.out.println("过滤成功");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化了");
}
}
接着配置xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>authlogin</filter-name>
<filter-class>com.chauncy.i18n.ChineseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>authlogin</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
</web-app>
运行效果: