jsf學習二(國際化)
國際化(I18N)就是設計軟件應用,在不改變它們程序邏輯的前提下支持各種語言和區域
本地化(L10N)就是設計軟件應用支持特定地區
由于數據經過本地化,因此同一應用程序能在全世界使用
當增加一種新的語言時,應用不需要重新編譯
在顯示和文化相關的數據(例如日期或貨幣)時,格式應用遵循用戶的語言和區域
文本內容(如狀態消息和組件標簽)不是硬編碼到應用程序中,而通常是從文本文件中動態提取的
國際化的步驟
創建一個包含應用程序默認語言的鍵/值對的文件
?文件命名格式:
Filename_languagecode_CountryCode.properties
例如:
ApplicationResources_zh_CN.properties
JSF 提供 <f:loadBundle> 標簽以加載資源包
此標簽具有兩個屬性:basename 和 var
basename 指定要加載的 properties 文件的名稱
var屬性用來為該properties文件起一個別名
如
<
f:view
>
????
<
f:loadBundle?
basename
="message"
?vars
="msg"
?
/>
….
????
<
h:outputLabel?
value
="#{msg.first}"
?
/>
</
f:view
>
資源文件編譯
native2ascii –encoding gb2312 messagesCN.properties? messages_zh_CN.properties
例
一個登陸實現國際化
后臺檢查登陸是否正確
??public?String?Login(string?userName,string?passWord)?{
??????if?(?UserName=="blog")?{
??????????return?"success";
??????}?else?{
?????FacesContext?context?=?FacesContext.getCurrentInstance();
?????ResourceBundle?bundle?=?ResourceBundle.getBundle("messages",?context.getViewRoot().getLocale());
??????String?msg?=?"";
?????if?(?username==""?||?passWord=="")
?????????msg?=?bundle.getString("username_isnull");
??
????context.addMessage?(null,?new?FacesMessage(msg));
????return?"shibai";?

?}
??}
前臺
<f:loadBundle?basename="message"?var="msg"/>
<html>
?<f:view>
?????<h:form>
??????<h:outputText?value="#{msg.login}"/>?<br/>
??????<h:messages?style="color:?blue"/><br/>
??????<h:inputText?id="username"?value="#{UserBean.userName}"
???????????????????????????required="true">
???????????<f:validateLongRange?minimum="0"?maximum="20"/>
??????</h:inputText><br/><br/>


????<h:outputText?value="#{msg.password}"/>?<br/>
??????<h:messages?style="color:?blue"/><br/>
??????<h:inputText?id="pssword"?value="#{UserBean.passWord}"
???????????????????????????required="true">
???????????<f:validateLongRange?minimum="0"?maximum="16"/>
??????</h:inputText><br/><br/>


?????<h:commandButton?value="#{msg.button_title}"?
??????????????????????????????????????action="#{userBean.Login}"/>
???</h:form>
?</f:view>
</html>
本地化(L10N)就是設計軟件應用支持特定地區
由于數據經過本地化,因此同一應用程序能在全世界使用
當增加一種新的語言時,應用不需要重新編譯
在顯示和文化相關的數據(例如日期或貨幣)時,格式應用遵循用戶的語言和區域
文本內容(如狀態消息和組件標簽)不是硬編碼到應用程序中,而通常是從文本文件中動態提取的
國際化的步驟
創建一個包含應用程序默認語言的鍵/值對的文件
?文件命名格式:
Filename_languagecode_CountryCode.properties
例如:
ApplicationResources_zh_CN.properties
JSF 提供 <f:loadBundle> 標簽以加載資源包
此標簽具有兩個屬性:basename 和 var
basename 指定要加載的 properties 文件的名稱
var屬性用來為該properties文件起一個別名
如





資源文件編譯
native2ascii –encoding gb2312 messagesCN.properties? messages_zh_CN.properties
例
一個登陸實現國際化
后臺檢查登陸是否正確















前臺

























posted on 2006-08-20 10:43 record java and net 閱讀(2363) 評論(1) 編輯 收藏 所屬分類: jsf學習