1. 程式人生 > >struts2國際化

struts2國際化

在struts2中需要做國際化的有:

jsp頁面的國際化,action錯誤資訊的國際化,轉換錯誤資訊的國際化,校驗錯誤資訊的國際化

jsp頁面的國際化

第一步

在src目錄下新建tanggao_en_US.properties,內容為
username=username
password=password

tanggao_zh_CN.properties檔案,內容為

username=\u7528\u6237\u540D
password=\u5BC6\u7801

這裡寫圖片描述

第二步
在struts.xml中配置

    <constant name="struts.custom.i18n.resources"
value="tanggao"></constant>

其中的tanggao就是國際化資原始檔的baseNmae

第三步 在jsp頁面中顯示 方法

<s:debug></s:debug>
    <s:text name="username"></s:text>
    <s:form action="Hello"  >
        <s:textfield name="username" key="username"></s:textfield>
        <s:password
name="password" key="password">
</s:password> <s:submit></s:submit> </s:form>

結果如下圖:

這裡寫圖片描述
action錯誤的國際化

這裡寫圖片描述

在tanggao_en_US.properties中增加以下內容

username.invalid=username invalid…

在tanggao_zh_CN.properties中增加以下內容

username.invalid=\u7528\u6237\u540d\u4e0d\u5408\u6cd5…

修改HelloAction中的validate方法,將錯誤加到ActionError中,在這裡將使用到ActionSupport中的getText方法獲得和國際化資原始檔相關的資訊。

以username驗證為例:

 if (null == username || username.length() = "" || username.length() > 10) {   

        this.addActionError(this.getText("username.invalid"));   

    }   

驗證框架的國際化(field級別錯誤)

在message_en_US.properties檔案中增加以下內容

username.xml.invalid=validate information

在message_zh_CN.properties檔案中增加以下內容

username.xml.invalid=\u9a8c\u8bc1\u6846\u67b6\u4fe1\u606f

然後修改驗證框架,需要將在properties檔案中的內容增加到框架中。

以username為例

<field name="username">   
    <field-validator type="requiredstring">   
        <param name="trim">true</param>   
        <message key="username.xml.invalid"></message>   
    </field-validator>   
</field>   

在message標籤中增加屬性key,值為properties檔案中的key

標籤中key大多是和國際化相關的

國際化資原始檔的分類

當應用程式很大時,需要國際化的東西會很多,因此需要將國際化資原始檔進行分類。

需要知道的是在src中的properties檔案是全域性資原始檔,另外還可以分為包級別的和類級別的

首先看看包級別的

命名規則為package_language_country.properties

新建package_en_US.properties,內容為

username.xml.invalid=package validate information

新建package_zh_CN.properties,內容為

username.xml.invalid=\u5305\u9a8c\u8bc1\u4fe1\u606f

可以看到輸出的資訊為“包驗證資訊”,由此可見包級別的國際化資原始檔的優先順序高於全域性國際化資原始檔。

類級別

新建HelloAction_en_US.properties,內容為

username.xml.invalid=class validate information

新建HellAction_zh_CN.properties,內容為

username.xml.invalid=\u7c7b\u9a8c\u8bc1\u4fe1\u606f

此時可以看到輸出的資訊為“類驗證資訊”。

由此可以得到國際化資原始檔的優先順序

全域性<包級別<類級別

另外要進行表單的國際化時,要去掉theme=”simple”

在HelloAction_en_US.properties中增加

username.name=username

在HelloAction_zh_CN.properties中增加

username.name=\u7528\u6237\u540d

這裡寫圖片描述
修改表單標籤

<s:textfield name="username" key="username.name"></s:textfield>   

注意到key一般是和國際化相關的。

另外除了用

另外除了用這個標籤外,還可以使用這個標籤

<s:i18n name="temp"></s:i18n>   

標籤中包含name,代表著可以定義資原始檔的baseName,如可以定義成temp,那麼對應著

temp_en_US.properties和temp_zh_CN.properties這兩個資原始檔。

如定義:

<s:i18n name="hello">   
    <s:text name="world">   
        <s:param>struts2</s:param>   
    </s:text>   
</s:i18n>   

注意到可以在標籤中增加 標籤。

在hello_en_US.properties檔案中增加

world=hello {0}

hello_zh_CN.properties中增加

world=\u4f60\u597d,struts2

在struts2的預設攔截器棧中已經定義了i18n攔截器,所以struts2已經是一個國際化的框架了。

struts2會查詢從客戶端提交的request_locale屬性,並存到session中的WW_TRANS_I18N_LOCALE欄位

中。

這個 標籤外,還可以使用 這個標籤

<s:i18n name="temp"></s:i18n>   

總結一下顯示方法:

<s:textname="username"></s:text>

getText("username.invalid") 

<message key="username.xml.invalid"></message>  

<s:textfield name="username" key="username.name"></s:textfield>    

<s:i18n name="temp"></s:i18n>