struts2國際化Tips

1.注意資源包的命名法
全局資源包:com.nutcore.resources.message
全局資源文件:message_en_US.properties      message_zh_CN.properties
其他資源文件(包和動作類的):
package_en_US.properties      package_zh_CN.properties
LoginAction_en_US.properties  LoginAction_zh_CN.properties
2.如何在文件中引入國際化資源
> 對於全局資源
a.配置struts.xml
<constant value="com.nutcore.resources.message"  name="struts.custom.i18n.resources"/>
b.獲得本地語言環境
<%
   //request.getLocale(),獲得當前語言環境
   ResourceBundle bundle = ResourceBundle.getBundle("com.nutcore.resources.message",request.getLocale()); 
    %>
    <%=bundle.getString("key") %><br>
   <!-- 使用struts2標籤獲得語言包
     如果直接訪問jsp頁面,沒有經過動作類,找全局資源包
     如果經過動作類,,找動作類
    -->

<!-- name屬性應該是資源包配置文件的key,如果key不存在,直接顯示內容 -->
   <s:text name="abc"></s:text>    顯示:abc
   <!-- 訪問指定資源包 -->
   <s:i18n name="com.nutcore.resources.message">
    <s:text name="key"></s:text>
   </s:i18n>
3.資源包查找順序

如果資源包不存在,直接訪問,查找全局
是按照資源包的順序去查找,動作類 -> 包 -> 全局

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章