每種框價(jià)都會(huì)有國(guó)際化的支持,struts2的國(guó)際化大致上分為頁(yè)面的國(guó)際化,Action的國(guó)際化以及xml的國(guó)際化
首先在struts.properties文件中加入以下內(nèi)容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
資源文件的命名格式: 名稱_語(yǔ)言代碼_國(guó)家代碼. Properties
如果創(chuàng)建中文和英語(yǔ)國(guó)際化,那么資源文件名稱為
messageResource_zh_CN.properties和messageResource_en_US.properties
1. jsp頁(yè)面的國(guó)際化
通過(guò)使用標(biāo)簽<s:text name="label.helloWorld"/>輸出國(guó)際化
label.helloWorld為資源文件中定義的key
在messageResource_en_US.properties加入以下內(nèi)容
label.hello=hello {0}
label.helloWorld=hello,world
在messageResource_zh_CN.properties加入以下內(nèi)容
label.hello=你好 {0}
label.helloWorld=你好,世界
(1). <s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面兩個(gè)都為輸出一個(gè)hello word的兩種表示
<s:textfield name="name" key="label.helloWorld"/>
<s:textfield name="name" label="%{getText('label.helloWorld')}"/>
顯示一個(gè)文本框,文本框的標(biāo)題進(jìn)行國(guó)際化
(2). 使用<s:i18n>標(biāo)簽指定從某個(gè)特定的資源文件中取數(shù)據(jù)
<s:i18n name="messageResource">
?? <s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在從messageResource取資源
(3).
<s:text name="label.hello">
?? <s:param>callan</s:param>
</s:text>
使用帶參數(shù)的資源.<s:param>可以替換label.hello=hello {0}中的{0}
2. Action的國(guó)際化
Action的國(guó)際化主要是通過(guò)getText(String key)方法實(shí)現(xiàn)的
- public String execute() throws Exception { ??
- ??
- ???????? ??
- ??
- ???????? // getText(String) string為key ??
- ??
- ???????? String str1 = getText( "label.helloWorld" ); ??
- ??
- ???????? System.out.println(str1); ??
- ??
- ???????? ??
- ??
- ???????? // 帶參數(shù)的 ??
- ??
- ???????? String str2 = getText( "label.hello" , new String[]{ "fjf" }); ??
- ??
- ???????? System.out.println(str2); ??
- ??
- ???? ??
- ??
- ???????? // 與上一種實(shí)現(xiàn)一樣 ??
- ??
- ???????? List l = new ArrayList(); ??
- ??
- ???????? l.add( "callan" ); ??
- ??
- ???????? String str3 = getText( "label.hello" ,l); ??
- ??
- ???????? System.out.println(str3); ??
- ??
- ???????? ??
- ??
- ???????? return SUCCESS; ??
- ??
- ???? }??
- public String execute() throws Exception { ??
- ??
- ???????? ??
- ??
- ???????? // getText(String) string為key ??
- ??
- ???????? String str1 = getText( "label.helloWorld" ); ??
- ??
- ???????? System.out.println(str1); ??
- ??
- ???????? ??
- ??
- ???????? // 帶參數(shù)的 ??
- ??
- ???????? String str2 = getText( "label.hello" , new String[]{ "fjf" }); ??
- ??
- ???????? System.out.println(str2); ??
- ??
- ???? ??
- ??
- ???????? // 與上一種實(shí)現(xiàn)一樣 ??
- ??
- ???????? List l = new ArrayList(); ??
- ??
- ???????? l.add( "callan" ); ??
- ??
- ???????? String str3 = getText( "label.hello" ,l); ??
- ??
- ???????? System.out.println(str3); ??
- ??
- ???????? ??
- ??
- ???????? return SUCCESS; ??
- ??
- ???? }??
public String execute() throws Exception { // getText(String) string為key String str1 = getText("label.helloWorld"); System.out.println(str1); // 帶參數(shù)的 String str2 = getText("label.hello",new String[]{"fjf"}); System.out.println(str2); // 與上一種實(shí)現(xiàn)一樣 List l = new ArrayList(); l.add("callan"); String str3 = getText("label.hello",l); System.out.println(str3); return SUCCESS; }
3. 參數(shù)化國(guó)際化
在messageResource_en_US.properties加入以下內(nèi)容
userName=userName
userName.required=${getText('userName')} is required
在messageResource_zh_CN.properties加入以下內(nèi)容
userName=用戶名
userName.required=${getText('userName')} 不能為空
在Action中
String str4 = getText("userName.required");
System.out.println(str4);
userName.required=${getText('userName')}會(huì)取國(guó)際化的用戶名
4. 使用校驗(yàn)框價(jià)時(shí),提示信息可以國(guó)際化
?? <field name="userName">
?? <field-validator type="requiredstring">
??? <message key=”userName.required”> </message>
?? </field-validator>
</field>
國(guó)際化資源文件分為三種級(jí)別
(1) 全局資源文件,可以被整個(gè)應(yīng)該程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件
(2) 包級(jí)資源文件,每個(gè)包的根目錄下可以新建資源文件,僅被當(dāng)前包中的類訪問(wèn).文件名格式為:package_語(yǔ)言代碼_國(guó)家代碼.
(3) Action級(jí)資源文件,僅被當(dāng)前Action引用,名稱為action名_語(yǔ)言代碼_國(guó)家代碼
查找順序?yàn)閺男》秶酱蠓秶? Action級(jí)優(yōu)先級(jí)最大
?
轉(zhuǎn)載:http://hi.baidu.com/83300409/blog/item/e3bc19a05c091b83471064ba.html
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
