一:簡單理解
國際化簡稱i18n,其來源是英文單詞 internationalization的首末字符i 和n。18為中間的字符數。
隨着全球經濟的一體化,軟件開發者應該開發出支持多國語言、國際化的Web應用。對於Web應用來說,同樣的頁面在不同的語言環境下需要顯示不同的效果。
國際化文件的命名規則:
1、基本名.properties 如:message.properties
2、基本名_語言編碼_國家編碼.properties 如:message_zh_CN.properties, message_en_US.properties 其中語言編碼和國家編碼是固定的,可以在JDK中Locale類的常量中找到。
Java中已經實現了國際化功能,struts2中只是對該功能進行了整合,以方便我們的使用。
Struts2中使用到國際化的地方有: 1、jsp頁面的國際化; 2、Action信息國際化; 3、轉換錯誤信息的國際化; 4、校驗錯誤信息的國際化;
Struts2國際化文件分類: 1、全局范圍國際化文件 2、包范圍國際化文件 3、Action類范圍國際化文件。
全局范圍國際化文件:
編寫一個messages_zh_CN.properties和messages_en_US.properties放在src下。
我在這里配置了兩個屬性:
messages_en_US:
login.username=username
login.password=password
messages_zh_CN:
login.username=\u767B\u5F55\u7528\u6237\u540D
login.password=\u767B\u5F55\u5BC6\u7801
在struts.xml中通過struts.custom.i18n.resources常量把資源文件定義為全局資源文件:
eg:
<!-- 配置struts2國際化 value是國際化資源基本名message-->
<constant name="struts.custom.i18n.resources" value="messages_en_US"/>
或者
<constant name="struts.custom.i18n.resources" value="messages_zh_CN"/>
(java代碼中)國際化獲取配置文件值使用:getText("鍵")
eg:getText("login.username") ---------》username。
或者
getText("login.username") ------------>登錄用戶名。
在jsp頁面中使用國際化。這里需要使用標簽:<s:i18n>標簽
eg:
1 <!-- 局部定義使用哪一種國際化語音 --> 2 <s:i18n name="messages_zh_CN"> 3 <form action="<%=basePath%>login.action" method="post"> 4 <table> 5 <tr> 6 <td><s:text name="login.username"/></td> 7 <td><input type="text" name="user.userName"/></td> 8 </tr> 9 <tr> 10 <td><s:text name="login.password"/></td> 11 <td><input type="text" name="user.password"/></td> 12 </tr> 13 <tr> 14 <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td> 15 </tr> 16 </table> 17 </form> 18 </s:i18n>
瀏覽器顯示為:
1 <!-- 局部定義使用哪一種國際化語音 --> 2 <s:i18n name="messages_en_US"> 3 <form action="<%=basePath%>login.action" method="post"> 4 <table> 5 <tr> 6 <td><s:text name="login.username"/></td> 7 <td><input type="text" name="user.userName"/></td> 8 </tr> 9 <tr> 10 <td><s:text name="login.password"/></td> 11 <td><input type="text" name="user.password"/></td> 12 </tr> 13 <tr> 14 <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td> 15 </tr> 16 </table> 17 </form> 18 </s:i18n>
瀏覽器顯示為
新技術在不斷的更新,該文章僅供參考!(最近好像在准備淘汰Struts)