有一些網站會有語言欄選項:
選擇英文,內容就顯示為英文;
選擇中文,內容就顯示文中文。
這里就用到了國際化資源。
先看效果圖:
步驟:
1.建立資源包:
mess_en_US.properties (英文)
mess_ko_KR.properties (韓文)
mess_zh_CN.properties (中文)
...以及其他國家語言的資源文件。
文件內容如下:
mess_en_US.properties:
mess_ko_KR.properties:
mess_zh_CN.properties:
這三個文件放置在src目錄下,頁面和后台可以直接引用使用:
2.頁面代碼:
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@page import="java.util.Locale"%> <%@page import="java.util.Date"%> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String ctxPath = request.getContextPath(); request.setAttribute("ctxpath", ctxPath);//項目根路徑 %> <!-- 動態獲取,顯示中英文切換 --> <% String code = request.getParameter("code"); //HttpSession session = request.getSession(); if(code!=null){ if("en".equals(code)){ //英文 session.setAttribute("locale", new Locale("en", "US")); } else if("zh".equals(code)){ //中文 session.setAttribute("locale", new Locale("zh", "CN")); } else if("ko".equals(code)){ //韓文 session.setAttribute("locale", new Locale("ko", "KR")); } }else{ //默認為中文 session.setAttribute("locale", new Locale("zh", "CN")); } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="${ctxpath}/js/jquery-1.11.3.min.js"></script> <title>國際化資源</title> <style type="text/css"> /* 調整語言欄選項位置 */ .language{ position: absolute; top: 4%; right:0; } /* 主頁 */ .main{ width: 50%; position: absolute; left: 25%; height: 100%; } /* 內容 */ .content{ margin-left: 25%; } /* 標題 */ .title{ display:block; width: 250px; margin: 0 auto; text-align: center; } </style> <script type="text/javascript"> $(function(){ $("#language").bind("change",function(){ window.location.href = "i18n_1.jsp?code="+this.value; }); set_select_checked(); }); function set_select_checked(){ var language = "<%=session.getAttribute("locale")%>"; language = language.substring(0, 2); var select = $("#language option"); for (var i = 0; i < select.length; i++){ if (select[i].value == language){ select[i].selected = true; } } } </script> </head> <body> <div class="main"> <div class="language"> <span>語言:</span> <select id="language"> <option value="zh">中文</option> <option value="en">英文</option> <option value="ko">韓文</option> </select> </div> <br> <!-- 在session范圍內設置locale,便於在jsp中獲取locale --> <c:if test="${sessionScope.locale!=null }"> <fmt:setLocale value="${sessionScope.locale }"/> </c:if> <fmt:setBundle basename="mess"/> <span class="title"><fmt:message key="hello"></fmt:message></span> <div class="content"> <%-- <fmt:message key="salaryUnit"></fmt:message> <fmt:formatNumber value="12"></fmt:formatNumber> --%> <br><br> <span><fmt:message key="menu"></fmt:message>:</span> <ul> <li><fmt:message key="profTeam"></fmt:message></li> <li><fmt:message key="responsibility"></fmt:message></li> <li><fmt:message key="contactUs"></fmt:message></li> </ul> </div> </div> </body> </html>
頁面使用JSTL標簽顯示國際化的內容。這個頁面就實現了動態圖所顯示的效果(資源國際化)。
========================================
后台測試效果圖:
后台測試類:
package test.locale; import java.util.Locale; import java.util.ResourceBundle; public class GuojihuaTest { /** * @param args */ public static void main(String[] args) { //取得系統默認的國家/語言環境 Locale myLocale = Locale.ENGLISH; //Locale localeUS = new Locale("en", "US"); //ko KR Locale localeUS = new Locale("zh", "CN"); //根據指定國家/語言環境加載資源文件 ResourceBundle bundle = ResourceBundle.getBundle("mess" , localeUS); //打印從資源文件中取得的消息 System.out.println(bundle.getString("hello")); } }