posts - 1, comments - 1, trackbacks - 0, articles - 13
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          jsf國際化

          Posted on 2007-06-06 17:32 Linden.zhang 閱讀(540) 評論(1)  編輯  收藏 所屬分類: Jsf
          JSF的國際化(Internnationalization)信息處理是基于Java對國際化的支援,您可以在一個信息資源檔中統一管理信息資源,資源文件的名稱是.properties,而內容是名稱與值的配對,例如:  

          messages.properties  
             
           titleText=JSF  Demo  
           hintText=Please  input  your  name  and  password  
           nameText=name  
           passText=password  
           commandText=Submit  

          資源檔名稱由basename加上語言與地區來組成,例如:  

          basename.properties    
          basename_en.properties    
          basename_zh_TW.properties    
             

                 沒有指定語言與地區的basename是預設的資源檔名稱,JSF會根據瀏覽器送來的Accept-Language  header中的內容來決定該使用哪一個資源檔名稱,例如:  


          Accept-Language:  zh_TW,  en-US,  en  


                 如果瀏覽器送來這些header,則預設會使用繁體中文,接著是美式英文,再來是英文語系,如果找不到對應的信息資源檔,則會使用預設的信息資源檔。  


                 由于信息資源檔必須是ISO-8859-1編碼,所以對于非西方語系的處理,必須先將之轉換為Java  Unicode  Escape格式,例如您可以先在信息資源檔中寫下以下的內容:  

          messages_zh_TW.txt    
             
           titleText=JSF示範  
           hintText=請輸入名稱與密碼  
           nameText=名稱  
           passText=密碼  
           commandText=送出  
             


          然后使用JDK的工具程式native2ascii來轉換,例如:  


          native2ascii  -encoding  Big5  messages_zh_TW.txt  messages_zh_TW.properties  


          轉換后的內容會如下:  

          messages_zh_TW.properties    
             
           titleText=JSF\u793a\u7bc4  
           hintText=\u8acb\u8f38\u5165\u540d\u7a31\u8207\u5bc6\u78bc  
           nameText=\u540d\u7a31  
           passText=\u5bc6\u78bc  
           commandText=\u9001\u51fa  
             
          (所有資源文件放入你的JFS應用的WEB-INF/classes目錄下)  

          接下來您可以使用<f:loadBundle>標記來指定載入信息資源,一個例子如下:  

          index.jsp    
             
           <%@  taglib  uri="http://java.sun.com/jsf/core"  prefix="f"  %>  
           <%@  taglib  uri="http://java.sun.com/jsf/html"  prefix="h"  %>  
           <%@page  contentType="text/html;charset=UTF8"%>  

           <f:view>  
           <f:loadBundle  basename="messages"  var="msgs"/>  

           <html>  
           <head>  
           <title><h:outputText  value="#{msgs.titleText}"/></title>  
           </head>  
           <body>  

                 <h:form>  
                         <h3><h:outputText  value="#{msgs.hintText}"/></h3>  
                         <h:outputText  value="#{msgs.nameText}"/>:  
                                         <h:inputText  value="#{user.name}"/><p>  
                         <h:outputText  value="#{msgs.passText}"/>:    
                                         <h:inputSecret  value="#{user.password}"/><p>    
                         <h:commandButton  value="#{msgs.commandText}"    
                                                         actionListener="#{user.verify}"  
                                                         action="#{user.outcome}"/>  
               </h:form>  

           </body>  
           </html>  

           </f:view>  

               如此一來,如果您的瀏覽器預設接受zh_TW語系的話,則頁面上就可以顯示中文,否則預設將以英文顯示,也就是messages.properties的內容,為了能顯示多國語系,我們設定網頁編碼為UTF8。  


          <f:view>可以設定locale屬性,直接指定所要使用的語系,例如:  

             
           <f:view  locale="zh_TW">  
           <f:loadBundle  basename="messages"  var="msgs"/>  
             
               直接指定以上的話,則會使用繁體中文來顯示,JSF會根據<f:loadBundle>的basename屬性加上<f:view>的locale屬性來決定要使用哪一個信息資源檔,就上例而言,就是使用messages_zh_TW.properties,如果設定為以下的話,就會使用messages_en.properties:  

             
           <f:view  locale="en">  
           <f:loadBundle  basename="messages"  var="msgs"/>  
             


          您也可以在faces-config.xml中設定語系,例如:  

             
           <faces-config>  
                 <application>  
                         <local-config>  
                                 <default-locale>en</default-locale>  
                                 <supported-locale>zh_TW</supported-locale>  
                         </local-config>  
                 </application>  
             
           .....  
           </faces-config>  
             

                 在<local-config>一定有一個<default-locale>,而<supported-locale>可以有好幾個,這告訴JSF您的應用程序支援哪些語系。  


                 當然,如果您可以提供一個選項讓使用者選擇自己的語系會是更好的方式,例如根據user這個Bean的locale屬性來決定頁面語系:  

             
           <f:view  locale="#{user.locale}">  
           <f:loadBundle  basename="messages"  var="msgs"/>    


          在頁面中設定一個表單,可以讓使用者選擇語系,例如設定單選鈕:  

             
           <h:selectOneRadio  value="#{user.locale}">  
                   <f:selectItem  itemValue="zh_TW"    
                                               itemLabel="#{msgs.zh_TWText}"/>  
                   <f:selectItem  itemValue="en"    
                                               itemLabel="#{msgs.enText}"/>  
           </h:selectOneRadio>  

          評論

          # re: jsf  回復  更多評論   

          2008-01-13 19:28 by re: jsfre: jsfre: jsfre: jsfre: jsf
          內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容) 內容(請不要發表任何與政治相關的內容)
          主站蜘蛛池模板: 衡山县| 东港市| 武邑县| 永新县| 道真| 巩义市| 香港 | 许昌市| 张家界市| 肃南| 内江市| 深水埗区| 休宁县| 虹口区| 原平市| 山东省| 泰顺县| 潍坊市| 龙江县| 新干县| 延庆县| 南郑县| 子洲县| 泽库县| 炉霍县| 临沂市| 保康县| 东乌珠穆沁旗| 衡阳县| 库车县| 绥江县| 南京市| 双牌县| 饶河县| 博兴县| 和田市| 陵川县| 调兵山市| 沂源县| 大竹县| 鹤庆县|