国际化的登录页面

1.通过选择来实现国际化的功能:

国际化的ResourceBundle使用介绍的文章的基础上,我们新建一个login.jsp的页面.


login.jsp的代码如下.

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    <script type="text/javascript">
    	function sendForm(){
    		document.getElementById("f").submit();
    	}
    </script>
  </head>
  
  <body>
  	<form id="f" action="${pageContext.request.contextPath}/login.jsp" method="post">
  		<select name="country" onchange="sendForm()">
  		<option>--选择国家--</option>
  		<option value="china">中国</option>
  		<option value="us">US</option>
  	</select>
  	</form>
  	
  	<%
  		String country = request.getParameter("country");
  		ResourceBundle bundle = null;
  		if("us".equals(country)){
  			bundle=ResourceBundle.getBundle("message",Locale.US);
  		}else{
  			bundle=ResourceBundle.getBundle("message",Locale.CHINA);
  		}
  	 %>
  
	<h1><%=bundle.getString("title") %></h1>
	<form>
		<%=bundle.getString("username") %>:<input type="text" name="username"><br>
		<%=bundle.getString("password") %>:<input type="password" name="password"><br>
		<input type="submit" value="<%=bundle.getString("submit") %>">
	</form>
  </body>
</html>

然后修改message_en_US.properties配置文件的内容.

title=LOGIN WINDOM
username=USERNAME
password=PASSWORD
submit=LOGIN

然后修改message_zh_CN.properties配置文件的内容.

title=\u767B\u5F55\u7A97\u53E3
username=\u7528\u6237\u540D
password=\u5BC6\u7801
submit=\u767B\u5F55

默认会显示中文页面.


当我选择US时,页面就会显示英文的.




2.通过获取浏览器的语言来自动实现国际化功能.

添加一个login1.jsp的页面.

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
  	
  	<%
  		String country = request.getHeader("accept-language");    //通过获取accept-language
  		ResourceBundle bundle = null;
  		if(country.startsWith("zh-CN")){  //这里我判断country是不是zh-CN 语言,若是中文语言让其显示英文网页.(正常是zh-CN就显示中文,en-US就显示英文,这里为了方便测试效果)
  			bundle=ResourceBundle.getBundle("message",Locale.US);
  		}else{
  			bundle=ResourceBundle.getBundle("message",Locale.CHINA);
  		}
  	 %>
  	
	<h1><%=bundle.getString("title") %></h1>
	<form>
		<%=bundle.getString("username") %>:<input type="text" name="username"><br>
		<%=bundle.getString("password") %>:<input type="password" name="password"><br>
		<input type="submit" value="<%=bundle.getString("submit") %>">
	</form>
  </body>
</html>

页面就自动显示为英文的了.





猜你喜欢

转载自blog.csdn.net/superman___007/article/details/80789456
今日推荐