java:jsp: ResourceBundle國際化多語言

來源:互聯網
上載者:User

標籤: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國際化多語言

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.