1. resource_en_US.properties
username=xiongfeiEnglish
username=\u718A\u975E\u4E2D\u6587 (熊非中文)
JDK中提供了一个native2ascii工具程序,可以将某种本地字符集编码的字符转换成Unicode转义序列的形式
Dos下进入a.properties文件所在目录,运行下面命令将在当前目录下生成一个名为b.properties的文件
native2ascii -encoding gb2312 源文件 目标文件
native2ascii -encoding gb2312 a.properties b.properties
3. resource.properties
username=\u96C4\u98DE\u9ED8\u8BA4 (雄飞默认)
import java.util.Locale; import java.util.ResourceBundle; public class TestResourceBundle { public static void main(String[] args) { Locale locale=Locale.US; String baseName="resource"; readResource(baseName,locale); } public static void readResource(String baseName,Locale locale){ /* * param1:资源文件的基名 * param2:区 域 */ ResourceBundle rb=ResourceBundle.getBundle(baseName, locale); String username=rb.getString("username"); System.out.println("username= "+username); } }***********************************************************************************************************************
1. login.jsp
<%@ page language="java" import="java.util.*,java.text.DateFormat" pageEncoding="UTF-8"%> <% //获取服务器首选语言 Locale locale=request.getLocale(); System.out.println("locale= "+locale); //获取浏览器中选择的所有语言 Enumeration em=request.getLocales(); while(em.hasMoreElements()){ System.out.println("~~~= "+em.nextElement()); } //处理系统时间 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale); String dateStr=df.format(new Date()); //国际化 ResourceBundle rb=ResourceBundle.getBundle("resource", locale); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> 时间:<%=dateStr %> <form action="./LoginServlet" method="post"> 员工信息录入<br/> <font color="red"><%=rb.getString("username") %></font><input type="text" value="" name="username" /> <br/> 密码:<input type="password" name="psw"/> <br/> 验证码:<input name="checkcode" type="text" class="big-input" maxlength="20" value="" /><br/> <img src="${pageContext.request.contextPath }/ImageServlet" onclick="nextPic();" id="pic" title="看不清,再来一张" /><br/> 保存用户名和密码<input type="checkbox" name="save" value="yes" /><br/> <input type="submit" value="保存" /> <script type="text/javascript"> function nextPic(){ var picStr=document.getElementById("pic"); //使用随机数 // picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Math.random(); //使用时间戳 picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Date.parse(new Date()); } </script> </form> </body> </html>
<%@ page language="java" import="java.util.*,java.text.DateFormat" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% Date date=new Date(); pageContext.setAttribute("date",date); %> 时间:<fmt:formatDate value="${date}" dateStyle="MEDIUM" timeStyle="MEDIUM" type="both"/> <fmt:bundle basename="resource"> <form action="./LoginServlet" method="post"> 员工信息录入<br/> <fmt:message key="username"></fmt:message><input type="text" value="" name="username" /> <br/> 密码:<input type="password" name="psw"/> <br/> 验证码:<input name="checkcode" type="text" class="big-input" maxlength="20" value="" /><br/> <img src="${pageContext.request.contextPath }/ImageServlet" onclick="nextPic();" id="pic" title="看不清,再来一张" /><br/> 保存用户名和密码<input type="checkbox" name="save" value="yes" /><br/> <input type="submit" value="保存" /> <script type="text/javascript"> function nextPic(){ var picStr=document.getElementById("pic"); //使用随机数 // picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Math.random(); //使用时间戳 picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Date.parse(new Date()); } </script> </form> </fmt:bundle> </body> </html>
<%@ page language="java" import="java.util.*,java.text.DateFormat" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% Locale l=Locale.CHINA; String slocale=request.getParameter("locale"); if(slocale!=null){ if("zh".equals(slocale.trim())){ l=Locale.CHINA; } if("en".equals(slocale.trim())){ l=Locale.US; } } pageContext.setAttribute("l",l); Date date=new Date(); pageContext.setAttribute("date",date); %> <!--改变语言--> <fmt:setLocale value="${l}"/> 时间:<fmt:formatDate value="${date}" dateStyle="MEDIUM" timeStyle="MEDIUM" type="both"/> <fmt:setBundle basename="resource"/> <form action="./LoginServlet" method="post"> 员工信息录入<br/> <fmt:message key="username"></fmt:message><input type="text" value="" name="username" /> <br/> 密码:<input type="password" name="psw"/> <br/> 验证码:<input name="checkcode" type="text" class="big-input" maxlength="20" value="" /><br/> <img src="${pageContext.request.contextPath }/ImageServlet" onclick="nextPic();" id="pic" title="看不清,再来一张" /><br/> 保存用户名和密码<input type="checkbox" name="save" value="yes" /><br/> <input type="submit" value="保存" /> <script type="text/javascript"> function nextPic(){ var picStr=document.getElementById("pic"); //使用随机数 // picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Math.random(); //使用时间戳 picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Date.parse(new Date()); } </script> </form> <a href="${pageContext.request.contextPath}/loginen_zh.jsp?locale=zh">中文</a> <a href="${pageContext.request.contextPath}/loginen_zh.jsp?locale=en">English</a> </body> </html>