要求:创建一个首页,默然显示英文信息,但可以让用户选择使用英文,繁体中文或简体中文.
1.编写hello_en_US.txt,内容如下:
cc.openhome.welcome=welcome
cc.openhome.font=English
cc.openhome.name=This is your front page
2.编写hello_zh_CN.txt,内容如下:
cc.openhome.welcome=欢迎
cc.openhome.font=简中
cc.openhome.name=这是你的预设首页
3.编写hello_zh_TW.txt,内容如下:
cc.openhome.welcome=歡迎
cc.openhome.font=繁中
cc.openhome.name=這是你的預設首頁
4.文件编码需要注意:
在idea18 中 英文和简体中文 .txt文本编辑采用的UTF-8格式,编码格式使用的是:jdk自带的native2ascii 生成 .propertities
$ native2ascii -encoding utf-8 hello_en_US.txt hello_en_US.properties
中文繁体编码采用的big5 编码方式,需要把编辑器设置为Big5编码格式,类似:编码为Big5
$ native2ascii -encoding Big5 hello_zh_TW.txt hello_zh_TW.properties
5.处理请求jsp
1 <%-- 2 * created by on 12/4/18 3 homework p282 7-1 4 --%> 5 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 7 <%@ page import="java.util.ResourceBundle" %> 8 <%@ page import="java.util.Locale" %> 9 <%@ page import="javax.servlet.jsp.jstl.fmt.LocalizationContext" %> 10 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 11 <% 12 ResourceBundle zh_TW = ResourceBundle.getBundle("cc.openhome.hello", new Locale("zh","TW")); 13 ResourceBundle zh_CN = ResourceBundle.getBundle("cc.openhome.hello",new Locale("zh","CN")); 14 ResourceBundle ja_JP = ResourceBundle.getBundle("cc.openhome.hello",new Locale("ja","JP")); 15 ResourceBundle en_US = ResourceBundle.getBundle("cc.openhome.hello",new Locale("en","US")); 16 17 pageContext.setAttribute("zh_TW",new LocalizationContext(zh_TW)); 18 pageContext.setAttribute("zh_CN",new LocalizationContext(zh_CN)); 19 pageContext.setAttribute("ja_JP",new LocalizationContext(ja_JP)); 20 pageContext.setAttribute("en_US",new LocalizationContext(en_US)); 21 22 %> 23 <html> 24 <head> 25 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 26 <c:choose> 27 <c:when test="${param.local =='en_US'}"> 28 <title><fmt:message bundle="${en_US}" key="cc.openhome.welcome"/></title> 29 </c:when> 30 <c:when test="${param.local =='zh_CN'}"> 31 <title><fmt:message bundle="${zh_CN}" key="cc.openhome.welcome"/></title> 32 </c:when> 33 <c:when test="${param.local =='zh_TW'}"> 34 <title><fmt:message bundle="${zh_TW}" key="cc.openhome.welcome"/></title> 35 </c:when> 36 <c:otherwise> 37 <title><fmt:message bundle="${en_US}" key="cc.openhome.welcome"/></title> 38 </c:otherwise> 39 </c:choose> 40 <style type="text/css"> 41 #menu li { 42 float: left; 43 border: 27px solid #fff; 44 line-height : 33px; 45 height: 10px; 46 list-style-type: none; 47 } 48 </style> 49 </head> 50 <body> 51 52 <ul id="menu"> 53 <li> 54 <a href='<c:url value="index.jsp"> 55 <c:param name="local" value="en_US"/> 56 </c:url> '> 57 <fmt:message bundle="${en_US}" key="cc.openhome.font"/> 58 </a> 59 </li> 60 <li> 61 <a href='<c:url value="index.jsp"> 62 <c:param name="local" value="zh_CN"/> 63 </c:url> '> 64 <fmt:message bundle="${zh_CN}" key="cc.openhome.font"/> 65 </a> 66 </li> 67 <li> 68 <a href='<c:url value="index.jsp"> 69 <c:param name="local" value="zh_TW"/> 70 </c:url> '> 71 <fmt:message bundle="${zh_TW}" key="cc.openhome.font"/> 72 </a> 73 </li> 74 </ul> 75 <br><br><br><br> 76 <hr> 77 <c:choose> 78 <c:when test="${param.local =='en_US'}"> 79 <fmt:message bundle="${en_US}" key="cc.openhome.name"/> 80 </c:when> 81 <c:when test="${param.local =='zh_CN'}"> 82 <fmt:message bundle="${zh_CN}" key="cc.openhome.name"/> 83 </c:when> 84 <c:when test="${param.local =='zh_TW'}"> 85 <fmt:message bundle="${zh_TW}" key="cc.openhome.name"/> 86 </c:when> 87 <c:otherwise> 88 <fmt:message bundle="${en_US}" key="cc.openhome.name"/> 89 </c:otherwise> 90 </c:choose> 91 </body> 92 </html>
6.运行效果
7.总结:
(1).设计 jstl核心标签库的使用.<c choose>,<c when>,<c otherwise>,<c url> <fmt bundle>,<fmt message>.
(2).国际化处理流程的简单理解.
(3).前端页面的简单调试.
(4).有待优化,重复代码写的太多.