標籤:post end err extends 國際 getc lan 尾碼 pre
java提供了一個資源類java.util.ResourceBundle來試下多國語言版本。其實ResourceBundle只是一個抽象的類,她有兩個子類:ListResourceBundle,和,PropertyResourceBundle.前一個子類需要編寫不同的國家語言資訊放置在對象類中,下面將執行個體示範,後一個子類是需要編寫不同國家語言的檔案尾碼為.properties()來存放語言資訊,這部分內容讀者可以在國際化標籤以及Struts時瞭解到。例如,存放在RES_zh_EN.propertieh和存放英文的RES_en_US.properties資源檔.
1 package cn.com.resouce; 2 import java.util.ListResourceBundle; 3 4 public class RES_zh_CN extends ListResourceBundle { 5 6 static final Object[][] contents = new String[][]{ 7 {"version","這是中文版"}, 8 {"title","中文版本"}, 9 {"index","首頁"},10 {"news","新聞"},11 {"life","生活"},12 {"sports","體育"},13 {"entertainment","娛樂"}14 };15 16 17 @Override18 protected Object[][] getContents() {19 // TODO Auto-generated method stub20 return contents;21 }22 23 }
1 package cn.com.resouce; 2 import java.util.ListResourceBundle; 3 4 public class RES_en_US extends ListResourceBundle { 5 6 //建立對應的中文文本資訊 7 8 static final Object[][] contents = new String[][]{ 9 10 {"version","this is english"},11 {"title","english"},12 {"index","index"},13 {"news","news"},14 {"life","life"},15 {"sports","sports"},16 {"entertainment","entertainment"}17 };18 19 20 @Override21 protected Object[][] getContents() {22 // TODO Auto-generated method stub23 return contents;24 }25 26 }
select.jsp
1 <%@ page language="java" contentType="text/html; charset=utf8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 <title>語言選擇</title> 7 </head> 8 <body> 9 10 11 <form action="multiLanguage.jsp" method="get">12 <select name="language">13 <option name="default">預設</option>14 <option value="chinese">中文</option>15 <option value="english">English</option>16 </select>17 <input type="submit" value="提交">18 </form>19 20 </body>21 </html>
language.jsp
<%@ page language="java" contentType="text/html; charset=utf8"%><%@ page import="java.util.ResourceBundle,java.util.Locale" %><%String language = request.getParameter("language");//預設語言,瀏覽器會根據請求中所包含的Accept language來辨別預設的何種語言ResourceBundle res = ResourceBundle.getBundle("cn.com.resouce.RES");//中文版本的顯示if(language.equals("chinese")){ res = ResourceBundle.getBundle("cn.com.resouce.RES", new Locale("zh","CN")); }if(language.equals("english")){ res = ResourceBundle.getBundle("cn.com.resouce.RES", new Locale("en", "US")); }%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title><%=res.getString("title") %></title></head><body><%=res.getString("version") %><hr><%=res.getString("index") %> | <%=res.getString("news") %> |<%=res.getString("life") %>| <%=res.getString("sports") %> | <%=res.getString("entertainment") %></body></html>
java:jsp: ResourceBundle國際化多語言