天天看點

struts2實作國際化

鏈€杩戝湪瀛︿範struts2锛屽涔犺祫鏂欐槸鏉庡垰钁楃殑銆妔truts2鏉冨▉鎸囧崡銆嬶紝杩欐湰涔﹀啓寰楅潪甯稿ソ锛岄潪甯告湁瀛︿範浠峰€箋€傛垜鍦ㄥ涔犺繃绋嬩腑锛岃嚜宸辮窡鐫€鍋氫簡浜涗緥瀛愩€備笅闈㈠氨鏄叧浜庡湪struts2涓厑璁哥敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█鐨勫師鐞嗗拰绀轟緥锛?

鍦ㄥ緢澶氭垚鐔熺殑鍟嗕笟杞歡涓紝鍙互璁╃敤鎴瘋嚜鐢卞垏鎹㈣瑷€锛屽綋鐢ㄦ埛杩涘叆绯葷粺鏃跺€欙紝鍙互鍑虹幇涓€涓笅鎷夊垪琛ㄦ锛岃鐢ㄦ埛閫夋嫨璇█锛屼竴鏃︾敤鎴烽€夋嫨浜嗚嚜宸遍渶瑕佷嬌鐢ㄧ殑璇█鐜锛屾暣涓郴缁熺殑璇█鐜灏嗕竴鐩存槸杩欑璇█鐜銆? Struts2涔熷彲浠ュ厑璁哥敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█銆傝€屼笖锛屽洜涓篠truts2鐨勬敮鎸侊紝鍦ㄧ▼搴忎腑鑷閫夋嫨璇█鐜灏嗗彉寰楁洿鍔犵畝鍗曘€?

涓€. Struts2鍥介檯鍖栫殑杩愯鏈哄埗

鍦⊿truts 2涓紝鎴戜滑鍙互閫氳繃ActionContext.getContext().setLocale(Locale arg)鍙互璁劇疆鐢ㄦ埛鐨勯粯璁よ瑷€銆備笉杩囷紝杩欑鏂瑰紡瀹屽叏鏄竴绉嶆墜鍔ㄦ柟寮忥紝鑰屼笖闇€瑕佺紪绋嬪疄鐜般€? 涓轟簡绠€鍖栬缃敤鎴烽粯璁よ瑷€鐜锛孲truts 2鎻愪緵浜嗕竴涓悕i18n鐨勬嫤鎴櫒锛圛nterceptor锛夛紝骞朵笖灏嗗叾浣忔敞鍐屽湪榛樿鐨勬嫤鎴櫒鏍堜腑锛坉efaultStack锛夈€備笅闈㈡槸Struts2鐨勯粯璁ゆ嫤鎴櫒鏍堢殑閰嶇疆鐗囨锛屼唬鐮佷腑绮椾綋瀛楄〃绀虹殑灏辨槸i18n鐨勬嫤鎴櫒

<interceptor-stack name="defaultStack">

聽<interceptor-ref name="exception"/>

聽<interceptor-ref name="alias"/>

聽<interceptor-ref name="servlet-config"/>

聽<interceptor-ref name="prepare"/>

聽<interceptor-ref name="i18n"/>

聽<interceptor-ref name="chain"/>

聽<interceptor-ref name="debugging"/>

聽<interceptor-ref name="profiling"/>

聽<interceptor-ref name="scoped-model-driven"/>

聽<interceptor-ref name="model-driven"/>

聽<interceptor-ref name="fileUpload"/>

聽<interceptor-ref name="checkbox"/>

聽<interceptor-ref name="static-params"/>

聽<interceptor-ref name="params">

聽聽<param name="excludeParams">dojo\..*</param>

聽</interceptor-ref>

聽<interceptor-ref name="conversionError"/>

聽<interceptor-ref name="validation">

聽聽聽聽 <param name="excludeMethods">input,back,cancel,browse</param>

聽</interceptor-ref>

聽<interceptor-ref name="workflow">

聽聽聽聽 <param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

</interceptor-stack>

i18n鎷︽埅鍣ㄥ湪鎵цAction鏂規硶鍓嶏紝鑷姩鏌ユ壘璇鋒眰涓殑涓€涓悕涓簉equest_locale鐨勫弬鏁般€傚鏋滆鍙傛暟瀛樺湪锛屾嫤鎴櫒灏卞皢鍏朵綔涓哄弬鏁幫紝骞惰漿鎹㈡垚Locale瀵矽薄锛屽苟灏嗗叾璁句負鐢ㄦ埛榛樿鐨凩ocale锛堜唬琛ㄨ瑷€/鍥藉鐜锛夈€? 闄ゆ涔嬪锛宨18n鎷︽埅鍣ㄨ繕浼氬皢涓婇潰鐢熸垚鐨凩ocale瀵矽薄淇濆瓨鍦ㄧ敤鎴稴ession鐨勫悕涓衡€淲W_TRANS_I18N_LOCALE鈥濈殑灞炴€т腑銆備竴鏃︾敤鎴稴ession涓瓨鍦ㄤ竴涓悕涓衡€淲W_TRANS_I18N_LOCALE鈥濈殑灞炴€э紝鍒欒灞炴€ф寚瀹氱殑Locale灏嗕細浣滀負娴忚鑰呯殑榛樿Locale銆?

浜? 鍒涘緩涓嬫媺鍒楄〃妗? 鍩轟簬鍓嶉潰鐨勪粙缁嶏紝涓轟簡瀹炵幇璁╃敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█鐨勫姛鑳斤紝鍙渶鎻愪緵涓€涓笅鎷夊垪琛ㄦ锛岃涓嬫媺鍒楄〃妗嗕腑鍒楀嚭鏈簲鐢ㄦ墍鏀寔鐨勫悇绉嶈瑷€锛屽苟涓旓紝褰撶敤鎴烽€夋嫨涓嬫媺鍒楄〃妗嗕腑鏌愪竴椤規椂锛岀郴缁熷皢璇ヤ笅鎷夐」鐨勫€間綔涓簉equest_locale鍙傛暟鎻愪氦缁橲truts2绯葷粺锛? 涓烘锛屾垜浠皢绯葷粺鎵€鏀寔鐨勮瑷€鏀懼叆涓€涓狹ap涓紝閫氳繃鍦↗SP椤甸潰涓凱浠hMap瀵矽薄锛岄€氳繃杩欑鏂瑰紡锛屽氨鍙互鍦ㄩ〉闈笂鍒楀嚭绯葷粺鎵€鏀寔鐨勫叏閮ㄨ瑷€锛屽苟璁╃敤鎴瘋嚜鐢遍€夋嫨銆? 涓嬮潰瀹氫箟浜咼avaBean锛岃繖涓狫avaBean閲屼繚瀛樹簡褰撳墠搴旂敤鎵€鏀寔鐨勫叏閮ㄨ瑷€锛岃JavaBean鐨勪唬鐮佸涓嬶細

//璇avaBean瀛樻斁浜嗙郴缁熸墍鏀寔鐨勫叏閮ㄨ瑷€銆? public class Locales

{

聽//鍥犱負鏈疄渚嬩篃闇€瑕佸疄鐜闆浗闄呭寲锛屾墍浠ヤ嬌鐢╟urrent浣滀負鐢ㄦ埛褰撳墠鐨凩ocale

聽private Locale current;

聽//鍙栧緱鐢ㄦ埛褰撳墠Locale鐨剆etter鏂規硶

聽public void setCurrent(Locale cur)

聽{

聽聽this.current = cur;

聽}

聽//鍙栧緱鏈郴缁熸墍鏀寔鐨勫叏閮ㄨ瑷€

聽聽聽 聽public Map<String, Locale> getLocales()

聽{

聽聽//灏嗗綋鍓嶇郴缁熸敮鎸佺殑鍏ㄩ儴璇█淇濇寔鍦∕ap瀵矽薄涓? 聽聽聽聽聽聽聽 聽Map<String, Locale> locales = new Hashtable<String, Locale>();

聽聽ResourceBundle bundle = ResourceBundle.getBundle("messageResource" , current);

聽聽//娣誨姞褰撳墠绯葷粺鏀寔鐨勮瑷€锛宬ey鏄郴缁熸敮鎸佽瑷€鐨勬樉绀哄悕瀛楋紝value鏄郴缁熸敮鎸佽瑷€鐨凩ocale瀹炰緥

聽聽聽聽聽聽聽 聽locales.put(bundle.getString("usen"), Locale.US);

聽聽聽聽聽聽聽 聽locales.put(bundle.getString("zhcn"), Locale.CHINA);

聽聽聽聽聽聽聽 聽return locales;

聽聽聽 }

}

鍦ㄤ笂闈avaBean涓紝鎴戜滑浣跨敤浜嗕竴涓狹ap瀵矽薄鏉ヤ繚瀛樻墍鏈夌敤鎴鋒敮鎸佺殑璇█锛岃Map瀵矽薄鐨刱ey鏄墍鏀寔璇█鐨勬樉绀哄悕瀛楋紝鑰岃Map瀵矽薄鐨剉alue鏄墍鏀寔璇█鐨凩ocale瀹炰緥銆傚簲鐢ㄦ敮鎸佽瑷€鐨勬樉绀哄悕瀛楋紝涔熸槸閫氳繃鍥介檯鍖栨秷鎭潵鐢熸垚鐨勩€? 涓€鏃﹀畾涔変簡璇avaBean涔嬪悗锛屽氨鍙互鍦↗SP椤甸潰涓垱寤鴻JavaBean鐨勫疄渚嬶紝骞朵負鍏朵紶鍏ヤ竴涓猚urrent鍙傛暟锛屽喅瀹氳JavaBean涓殑Locale鍙傛暟锛屽氨鍙互鏍規嵁璇ocale鍙傛暟鏉ュ喅瀹氭€庢牱鏄劇ず绯葷粺鎵€鏀寔璇█鏄劇ず鍚嶇О銆? 涓轟簡鍦↗SP椤甸潰涓嬌鐢ㄨJavaBean瀹炰緥锛屼嬌鐢ㄤ笅闈㈡爣绛炬潵鍒涘緩璇avaBean瀹炰緥銆? <!-- 浣跨敤lee.Locales鍒涘緩locales瀹炰緥 -->

<s:bean id="locales" name="lee.Locales">

聽<!-- 涓簂ocales瀹炰緥杩欑current鍙傛暟鍊?-->

聽<s:param name="current"

聽聽value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"/>

</s:bean>

涓婇潰鏍囩鍒涘緩浜唋ee.Locales绫葷殑locales瀹炰緥锛屽苟涓鴻瀹炰緥浼犲叆浜哻urrent鍙傛暟鍊鹼紝璁劇疆璇ュ弬鏁闆€兼椂浣跨敤浜嗕笁鐩繍绠楃锛屽厛鍒ゆ柇SESSION_LOCALE鏄惁涓虹┖锛屽鏋滆SESSION_LOCALE涓虹┖锛屽垯杩斿洖ValueStack涓璴ocale灞炴€у€鹼紙鍗崇敤鎴鋒祻瑙堝櫒璁劇疆鐨凩ocale锛夛紱濡傛灉SESSION_LOCALE涓嶄負绌猴紝鍒欒繑鍥炶SESSION_LOCALE鐨勫€鹼紙鍗崇敤鎴烽€夋嫨鐨凩ocale锛夈€? 涓轟簡璁╄椤甸潰涓寘鍚玈ESSION_LOCALE锛屼嬌鐢⊿truts2鐨?lt;s:set .../>鏍囩灏嗙敤鎴稴ession涓殑鈥淲W_TRANS_I18N_LOCALE鈥濆睘鎬у€艱缃垚SESSION_LOCALE銆? 涓嬮潰鏄畬鎴愯璁劇疆鐨勬爣绛撅細

<!-- 灏嗙敤鎴稴ession涓殑鈥淲W_TRANS_I18N_LOCALE鈥濆睘鎬у€艱缃垚SESSION_LOCALE銆?-->

<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>

涓嬮潰鏄selectlanguage.jsp椤甸潰鐨勪唬鐮侊細

<%@taglib prefix="s" uri="/struts-tags"%>

<script type="text/javascript">

function langSelecter_onChanged()

{

聽document.getElementById("langForm").submit();

}

</script>

<!-- 灏嗙敤鎴稴ession涓殑鈥淲W_TRANS_I18N_LOCALE鈥濆睘鎬у€艱缃垚SESSION_LOCALE銆?-->

<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>

<!-- 浣跨敤lee.Locales鍒涘緩locales瀹炰緥 -->

<s:bean id="locales" name="lee.Locales">

聽<!-- 涓簂ocales瀹炰緥杩欑current鍙傛暟鍊?-->

聽<s:param name="current"

聽聽value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"/>

</s:bean>

<!-- 璁╃敤鎴烽€夋嫨璇█鐨勮〃鍗?-->

<form action="<s:url/>" id="langForm"

聽聽聽 聽style="background-color:#bbbbbb; padding-top: 4px; padding-bottom: 4px;">

聽<!-- 杈撳嚭鍥介檯鍖栨彁绀?-->

聽聽聽 聽<s:text name="languag"/>

聽<!-- 浣跨敤s:select鏍囩杩唬locales瀹炰緥鐨刲ocales Map灞炴€?-->

聽<s:select label="Language" list="#locales.locales" listKey="value" listValue="key"

聽聽聽聽聽聽聽 value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"

聽聽聽聽聽聽聽 name="request_locale" id="langSelecter"

聽聽聽聽聽聽聽 慰nchange="langSelecter_onChanged()" theme="simple"/>

</form>

娉ㄦ剰锛氫笂闈㈤〉闈腑澶ч噺浣跨敤浜哠truts2鐨勬爣绛撅紝鍏充簬Struts2鏍囩鐨勮缁嗙敤娉曪紝璇峰弬鑰冩湰涔︾鍗佺珷鐨勮瑙c€? 涓婇潰椤甸潰鐨勫師鐞嗘槸锛氫嬌鐢?lt;s:set .../>鏍囩瀹炰緥鍖栦竴涓狶ocales瀵矽薄锛屽苟浣跨敤<s:select ..../>鏍囧墠鏉ユ樉绀鴻Locales瀵矽薄鐨刲ocales锛圡ap绫誨瀷锛夊睘鎬э紝 <s:select ..../>鍙互浣跨敤涓€涓笅鎷夊垪琛ㄦ鏉ユ樉绀篗ap绫誨瀷鐨勯泦鍚堬紝鏈簲鐢ㄥ皢璇ap鐨刱ey杈撳嚭鎴愪笅鎷夊垪琛ㄩ」鐨勬樉绀哄悕绉幫紝灏嗚Map鐨剉alue杈撳嚭鎴愪笅鎷夊垪琛ㄩ」鐨勫€箋€? 闄ゆ涔嬪锛岄〉闈笂闈㈣繕鏈変竴娈電畝鍗曠殑Javascript鑴氭湰锛屽畠浼氬湪鐢ㄦ埛鍦ㄩ€夋嫨涓嬫媺鍒楄〃涓煇涓€椤瑰悗锛屾彁浜ゅ寘鍚€渞eqeust_locale鈥濆彉閲忕殑琛ㄥ崟鍒癆ction銆? 涓婇潰椤甸潰鍜孞avaBean涓€鍏變嬌鐢ㄤ簡涓変釜鍥介檯鍖杒ey锛屾墍浠ラ渶瑕佸湪璧勬簮鏂囦歡瀹氫箟杩欎笁涓猭ey瀵瑰簲鐨勫浗闄呭寲娑堟伅銆傚洜涓烘湰绯葷粺浠呮敮鎸佺畝浣撲腑鏂囧拰缇庡紡鑻辮涓ょ鐜锛堝闇€瑕佸鍔犲叾浠栬瑷€涔熼潪甯哥畝鍗曪紝 鍙渶瑕佸鍔犳洿杩囩殑璧勬簮鏂囦歡锛屽苟绠€鍗曚慨鏀筁ocales绫誨嵆鍙級锛屾墍浠ラ渶瑕佸垎鍒湪涓枃璧勬簮鏂囦歡涓鍔犲涓嬩笁椤癸細

languag=閫夋嫨璇█

usen=缇庡紡鑻辮

zhcn=绠€浣撲腑鏂? 鍦ㄨ嫳鏂囪祫婧愭枃浠朵腑澧炲姞濡備笅涓夐」锛? languag=Select Lanuage

usen=American English

zhcn=Simplified Chinese

涓? 閫夋嫨绋嬪簭璇█

鏈簲鐢ㄤ負浜嗘洿濂界殑瀹夊叏鎬э紝灏嗘墍鏈夌殑JSP椤甸潰閮芥斁鍦╓EB-INF/jsp璺緞涓嬶紝浠庤€岄伩鍏嶄簡鐩存帴璁塊棶JSP椤甸潰锛屼負浜嗚鎵€鏈夌殑JSP椤甸潰閮借兘寰楀埌Struts2鐨勫鐞嗭紝鍦╯truts.xml鏂囦歡涓鍔犲涓嬮厤缃墖娈碉細

<!-- 浣跨敤閫氶厤绗﹀彿瀹氫箟Action鐨刵ame -->

<action name="*">

聽<!-- 灏嗚姹傝漿鍙戣WEB-INF/jsp璺緞涓嬪悓鍚嶇殑JSP椤甸潰 -->

<result>/WEB-INF/jsp/{1}.jsp</result>

</action>

濡傛灉鍦ㄦ祻瑙堝櫒涓姹俿electlanguage.action锛屽皢鐪嬪埌閫夋嫨绋嬪簭璇█鐨勯〉闈€? 濡傛灉鐢ㄦ埛閫氳繃涓婇潰鐨勪笅鎷夊垪琛ㄦ閫夋嫨浜嗏€滅編寮忚嫳璇€濋」鍚庯紝灏嗙湅鍒扮敤鎴烽€夋嫨缇庡紡鑻辮鐨勯〉闈€? 涓€鏃﹀畾涔変簡涓婇潰鐨勯〉闈㈠悗锛屾垜浠氨鍙互鍦↗SP椤甸潰涓€氳繃<s:include .../>鏍囩鏉ュ寘鍚椤甸潰锛屽寘鍚椤甸潰鍚庯紝灏卞彲浠ヨ嚜琛岄€夋嫨绋嬪簭璇█浜嗐€? 渚嬪鍦ㄧ櫥闄嗛〉闈腑閫氳繃濡備笅鏍囩鏉ュ寘鍚笂闈㈤€夋嫨绋嬪簭璇█鐨勯〉闈細

<!-- 鍖呭惈璁╃敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█鐨勯〉闈?-->

<s:include value="selectlanguage.jsp"/>

鍦ㄤ換浣曢〉闈腑澧炲姞浜嗕笂闈唬鐮佸悗锛岃椤甸潰灏辮兘鍏佽鐢ㄦ埛鑷閫夋嫨璇█銆傞€氬父锛屾垜浠彧闇€瑕佸湪搴旂敤鐨勭涓€涓〉闈㈣鐢ㄦ埛閫夋嫨绋嬪簭璇█锛屽悗缁〉闈㈢洿鎺ヤ嬌鐢ㄨ璇█鍗沖彲銆? 濡傛灉娴忚鑰呭湪娴忚鍣ㄧ殑鍦闆潃鏍忚闂甶nput.action锛孲truts2鑷姩杩涘叆WEB-INF/jsp/input.jsp椤甸潰锛屽皢鐪嬪埌鍏佽鐢ㄦ埛閫夋嫨绋嬪簭璇█鐨勯〉闈€? 濡傛灉鐢ㄦ埛甯屾湜浣跨敤缇庡紡鑻辮鐨勮瑷€锛屽垯鍦ㄥ厑璁哥敤鎴烽€夋嫨绋嬪簭璇█鐨勯〉闈㈢殑涓嬫媺鍒楄〃妗嗕腑閫変腑鈥滅編寮忚嫳璇€濋」锛屽皢鐪嬪埌浣跨敤缇庡紡鑻辮鐣岄潰鐨勯〉闈€? 濡傛灉鐢ㄦ埛鐧婚檰鎴愬姛锛屽皢鐪嬪埌缇庡紡鑻辮鐜涓嬬櫥闄嗘垚鍔熺殑椤甸潰銆傝椤甸潰鑷姩浣跨敤浜嗙編寮忚嫳璇殑鐜锛岃繖閮芥槸鍥犱負鐢ㄦ埛閫夋嫨浜嗙編寮忚嫳璇殑璇█鍚庯紝绯葷粺灏嗙敤鎴烽€夋嫨璁劇疆鎴愪簡Session鐨刉W_TRANS_I18N_LOCALE灞炴€у€鹼紝璇ession灞炴€у€肩洿鎺ュ喅瀹歋truts2绯葷粺鐨勮瑷€鐜銆?

璧勬簮鏂囦歡鐨勫懡鍚嶏細basename_language_country.properties

JAVA鍥介檯鍖?

濡傛灉绯葷粺鍚屾椂瀛樺湪璧勬簮鏂囦歡銆佺被鏂囦歡锛岀郴缁熷皢浠ョ被鏂囦歡涓轟富锛岃€屼笉浼氳皟鐢ㄨ祫婧愭枃浠躲€?

瀵逛簬绠€浣撲腑鏂囩殑Locale锛孯esourceBundle鎼滅儲璧勬簮鐨勯『搴忔槸锛?

(1)baseName_zh_CN.class

(2)baseName_zh_CN.properties

(3)baseName_zh.class

(4)baseName_zh.properties

(5)baseName.class

(6)baseName.properties

Struts2鐨勫浗闄呭寲鍏ラ棬

Struts2鍥介檯鍖栨槸寤虹珛鍦↗ava鍥介檯鍖栫殑鍩虹涓婄殑锛屼竴鏍鋒槸閫氳繃鎻愪緵涓嶅悓鍥藉/璇█鐜鐨勬秷鎭祫婧愶紝鐒跺悗閫氳繃ResourceBundle

鍔犺澆鎸囧畾Locale瀵瑰簲鐨勮祫婧愭枃浠訛紝鍐嶅彇寰楄璧勬簮鏂囦歡涓寚瀹歬ey瀵瑰簲鐨勬秷鎭?-鏁翠釜杩囩▼涓嶫AVA绋嬪簭鐨勫浗瀹跺寲瀹屽叏鐩稿悓锛屽彧鏄?

Struts2妗嗘灦瀵笿AVA绋嬪簭鍥介檯鍖栬繘琛屼簡杩涗竴姝ュ皝瑁咃紝浠庤€岀畝鍖栦簡搴旂敤绋嬪簭鐨勫浗闄呭寲銆?

Struts2闇€瑕佸浗闄呭寲鐨勯儴鍒?

绫誨瀷杞崲:

鏁版嵁鏍¢獙:

楠岃瘉妗嗘灦xml閰嶇疆鏂囦歡鐨勫浗闄呭寲锛歊egisterAction-validation.xml鏂囦歡<message key="username.xml.invalid"/>

JSP椤甸潰鐨勫浗闄呭寲锛?lt;s:text name="addUser"/>

Action鐨勫浗闄呭寲:鍒╃敤ActionSupport绫繪彁渚涚殑getText()鏂規硶.

Struts2涓姞杞藉叏灞€璧勬簮鏂囦歡

struts.xml

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

鎴?

struts.properties

struts.custom.i18n.resources=baseName

璁塊棶鍥介檯鍖栨秷鎭?

Struts2璁塊棶鍥介檯鍖栨秷鎭富瑕佹湁濡備笅涓夌鏂瑰紡锛?

(1)JSP椤甸潰锛?lt;s:text name="key"/>

(2)Action绫諱腑锛氫嬌鐢ˋctionSupport绫葷殑getText鏂規硶銆?

(3)琛ㄥ崟鍏冪礌鐨凩abel閲岋細涓鴻〃鍗曞厓绱犳寚瀹氫竴涓猭ey灞炴€?

杈撳嚭甯﹀崰浣嶇鐨勫浗闄呭寲娑堟伅

Struts2涓彁渚涗簡濡備笅涓ょ鏂瑰紡鏉ュ~鍏呮秷鎭瓧绗︿覆涓殑鍗犱綅绗?

(1)JSP椤甸潰锛屽湪<s:text.../>鏍囩涓嬌鐢ㄥ涓?lt;s:param.../>鏍囩鏉ュ~鍏呮秷鎭腑鐨勫崰浣嶇銆?

(2)Action涓紝鍦ㄨ皟鐢╣etText鏂規硶鏃朵嬌鐢╣etText(String aTextName,List args)鎴杇etText(String key, String[] args)鏂規硶鏉ュ~鍏呭崰浣嶇銆?

闄ゆ涔嬪锛孲truts2杩樻彁渚涗簡瀵瑰崰浣嶇鐨勪竴绉嶆浛浠f柟寮忥紝杩欑鏂瑰紡鍏佽鍦ㄥ浗闄呭寲娑堟伅璧勬簮鏂囦歡涓嬌鐢ㄨ〃杈懼紡锛屽浜庤繖绉嶆柟寮忥紝鍒欏彲閬垮厤鍦ㄤ嬌鐢ㄥ浗闄呭寲娑堟伅鏃惰繕闇€瑕佷負鍗犱綅绗︿紶鍏ュ弬鏁闆€箋€?

濡備笅鍦ㄦ秷鎭祫婧愪腑浣跨敤琛ㄨ揪寮?

succTip=${username}, 娆㈣繋, 鎮ㄥ凡缁忕櫥褰?

鍦ㄤ笂闈㈢殑娑堟伅璧勬簮涓紝閫氳繃浣跨敤琛ㄨ揪寮忥紝鍙互浠嶸alueStack涓彇鍑鴻username灞炴€у€鹼紝鑷姩濉厖鍒拌娑堟伅璧勬簮涓€?

鍔犺澆璧勬簮鏂囦歡鐨勬柟寮?

(1)鍔犺澆鍏ㄥ眬璧勬簮鏂囦歡锛?<constant name="struts.custom.i18n.resources" value="baseName"/>

(2)鍖呰寖鍥磋祫婧愭枃浠?锛氫負Struts2鎸囧畾鍖呰寖鍥磋祫婧愭枃浠剁殑鏂規硶鏄?鍦ㄥ寘鐨勬牴璺緞涓嬪緩绔嬪涓枃浠跺悕涓簆ackage_language_country.properties鐨勬枃浠訛紝涓€鏃﹀緩绔嬩簡

聽聽聽 杩欎釜绯誨垪鐨勫浗闄呭寲璧勬簮鏂囦歡锛屽簲鐢ㄤ腑澶勪簬璇ュ寘涓嬬殑鎵€鏈堿ction閮藉彲浠ヨ闂璧勬簮鏂囦歡銆傞渶瑕佹敞鎰忕殑鏄笂闈㈢殑鍖呰寖鍥磋祫婧愭枃浠剁殑baseName灏辨槸package锛?

聽涓嶆槸Action鎵€鍦ㄧ殑鍖呭悕銆?

(3)Action鑼冨洿璧勬簮鏂囦歡锛氬湪Action绫繪枃浠舵墍鍦ㄧ殑璺緞寤虹珛澶氫釜鏂囦歡鍚嶄負ActionName_language_country.properties鐨勬枃浠躲€?

(4)涓存椂鎸囧畾璧勬簮鏂囦歡锛?lt;s:i18n.../>鏍囩鐨刵ame灞炴€ф寚瀹氫複鏃剁殑鍥介檯鍖栬祫婧愭枃浠?

鍔犺澆璧勬簮鏂囦歡鐨勯『搴?

Action涓姞杞借祫婧愭枃浠訛紝鍋囪鎴戜滑鍦ㄦ煇涓狢hildAction涓皟鐢ㄤ簡getText("user.title")锛孲truts 2.0鐨勫皢浼氭墽琛屼互涓嬬殑鎿嶄綔锛?

(1)浼樺厛鍔犺澆绯葷粺涓繚瀛樺湪ChildAction鐨勭被鏂囦歡鐩稿悓浣嶇疆锛屼笖baseName涓篊hildAction鐨勭郴鍒楄祫婧愭枃浠躲€?

(2)濡傛灉鍦?1)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝涓擟hildAction鏈夌埗绫籔arentAction锛屽垯鍔犺澆绯葷粺涓繚瀛樺湪ParentAction鐨勭被鏂囦歡鐩稿悓浣嶇疆锛屼笖

聽聽 baseName涓篜arentAction鐨勭郴鍒楄祫婧愭枃浠躲€?

(3)濡傛灉鍦?2)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝涓擟hildAction鏈夊疄鐜版帴鍙ChildAction锛屽垯鍔犺澆绯葷粺涓繚瀛樺湪IChildAction鐨勭被鏂囦歡鐩稿悓浣嶇疆锛屼笖

聽聽 baseName涓篒ChildAction鐨勭郴鍒楄祫婧愭枃浠躲€?

(4)濡傛灉鍦?3)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝涓擟hildAction鏈夊疄鐜版帴鍙odelDriven(鍗充嬌鐢ㄦā鍨嬮┍鍔ㄦā寮?锛屽垯瀵逛簬getModel()鏂規硶杩斿洖鐨刴odel瀵矽薄锛?

聽聽 閲嶆柊鎵ц绗?1)姝ユ搷浣溿€?

(5)濡傛灉鍦?4)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐煡鎵懼綋鍓嶅寘涓媌aseName涓簆ackage鐨勭郴鍒楄祫婧愭枃浠躲€?

(6)濡傛灉鍦?5)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐部鐫€褰撳墠鍖呬笂婧紝鐩村埌鏈€椤跺眰鍖呮潵鏌ユ壘baseName涓簆ackage鐨勭郴鍒楄祫婧愭枃浠躲€?

(7)濡傛灉鍦?6)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐煡鎵緎truts.custom.i18n.resources甯擱噺鎸囧畾baseName鐨勭郴鍒楄祫婧愭枃浠躲€?

(8)濡傛灉缁忚繃涓婇潰鐨勬楠や竴鐩存壘涓嶅埌key瀵瑰簲鐨勬秷鎭紝灏嗙洿鎺ヨ緭鍑鴻key鐨勫瓧绗︿覆鍊箋€?

瀵逛簬鍦↗SP涓闂浗闄呭寲娑堟伅锛屽垯绠€鍗曠殑澶氾紝浠栦滑鍙堝彲浠ュ垎涓轟袱绉嶅艦寮忥細

(1)瀵逛簬浣跨敤<s:i18n.../>鏍囩浣滀負鐖舵爣绛劇殑<s:text.../>鏍囩銆佽〃鍗曟爣绛劇殑褰㈠紡锛?

聽聽 a銆佸皢浠?lt;s:i18n.../>鏍囩鎸囧畾鐨勫浗闄呭寲璧勬簮鏂囦歡涓姞杞芥寚瀹歬ey瀵瑰簲鐨勬秷鎭€?

聽聽 b銆佸鏋滃湪a涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐煡鎵緎truts.custom.i18n.resources甯擱噺鎸囧畾baseName鐨勭郴鍒楄祫婧愭枃浠躲€?

聽聽 c銆佸鏋滅粡杩囦笂闈㈡楠や竴鐩存壘涓嶅埌璇ey瀵瑰簲鐨勬秷鎭紝灏嗙洿鎺ヨ緭鍑鴻key鐨勫瓧绗︿覆鍊箋€?

(2)濡傛灉<s:text.../>鏍囩銆佽〃鍗曟爣绛炬病鏈変嬌鐢?lt;s:i18n.../>鏍囩浣滀負鐖舵爣绛撅細

聽聽聽 鐩存帴鍔犺澆struts.custom.i18n.resources甯擱噺鎸囧畾baseName鐨勭郴鍒楄祫婧愭枃浠躲€傚鏋滄壘涓嶅埌璇ey瀵瑰簲鐨勬秷鎭紝灏嗙洿鎺ヨ緭鍑鴻key鐨勫瓧绗︿覆鍊箋€?

鍏佽鐢ㄦ埛鑷閫夋嫨绋嬪簭璇█

Struts2鍥介檯鍖栫殑杩愯鏈哄埗

聽聽聽 鍦⊿truts2涓紝鍙互閫氳繃ActionContext.getContext().setLocale(Locale arg)璁劇疆鐢ㄦ埛鐨勯粯璁よ瑷€銆?

涓轟簡绠€鍖栬缃敤鎴烽粯璁よ瑷€鐜锛孲truts2鎻愪緵浜嗕竴涓悕涓篿18n鐨勬嫤鎴櫒(Interceptor),骞朵笖灏嗗叾娉ㄥ唽鍦ㄩ粯璁ょ殑鎷︽埅鍣ㄤ腑(defaultStack)銆?

聽聽聽 i18n鎷︽埅鍣ㄥ湪鎵цAction鏂規硶鍓嶏紝鑷姩鏌ユ壘璇鋒眰涓竴涓悕涓簉equest_locale鐨勫弬鏁般€傚鏋滆鍙傛暟瀛樺湪锛屾嫤鎴櫒灏卞皢鍏朵綔涓哄弬鏁幫紝杞崲鎴怢ocale瀵矽薄锛?

骞跺皢鍏惰涓虹敤鎴烽粯璁ょ殑Locale(浠h〃鍥藉/璇█鐜)銆?

聽闄ゆ涔嬪锛宨18n鎷︽埅鍣ㄨ繕浼氬皢涓婇潰鐢熸垚鐨凩ocale瀵矽薄淇濆瓨鍦ㄧ敤鎴稴ession鐨勫悕涓篧W_TRANS_I18N_LOCALE鐨勫睘鎬т腑銆備竴鏃︾敤鎴稴ession涓瓨鍦ㄤ竴涓悕涓?

WW_TRANS_I18N_LOCALE鐨勫睘鎬э紝鍒欒灞炴€ф寚瀹氱殑Locale灏嗕細浣滀負娴忚鑰呯殑榛樿Locale銆?

<%@ page language="java" contentType="text/html; charset=GBK"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<script. type="text/javascript">

function langSelecter_onChanged()

{

聽document.getElementById("langForm").submit();

}

</script>

<%-- 璁劇疆SESSION_LOCALE涓虹敤鎴穝ession涓殑WW_TRANS_I18N_LOCALE灞炴€у€?--%>

<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>

<%-- 浣跨敤lee.Locales鍒涘緩locales瀹炰緥 --%>

<s:bean id="locales" name="lee.Locales">

聽<%-- 涓簂ocales瀹炰緥浼犲叆current鍙傛暟鍊鹼紝濡傛灉SESSION_LOCALE涓虹┖锛屽垯杩斿洖ValueStack涓璴ocale灞炴€у€?鍗崇敤鎴鋒祻瑙堝櫒璁劇疆鐨凩ocale) --%>

聽<s:param name="current" value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"/>

</s:bean>

<%-- 璁╃敤鎴烽€夋嫨璇█鐨勮〃鍗?--%>

<form. action="<s:url/>" id="langForm"

聽聽聽 style="background-color:#bbbbbb; padding-top: 4px; padding-bottom: 4px;">

聽聽聽 <s:text name="languag"/>

聽<s:select label="Language" list="#locales.locales" listKey="value" listValue="key"

聽聽聽聽聽聽聽 value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"

聽聽聽聽聽聽聽 name="request_locale" id="langSelecter"

聽聽聽聽聽聽聽 nchange="langSelecter_onChanged()" theme="simple"/>

</form>

鍦ㄥ叾浠栭〉闈腑鍖呭惈璇ラ〉闈?

<s:include value="selectlanguage.jsp"/>

鍦╯truts.xml鏂囦歡涓鍔燗ction閫氶厤绗︾殑閰嶇疆锛?

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE struts PUBLIC

聽聽聽聽聽聽聽 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

聽聽聽聽聽聽聽 "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

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

聽<constant name="struts.i18n.encoding" value="GBK"/>

聽聽聽 <package name="lee" extends="struts-default">

聽聽<!-- 浣跨敤閫氶厤绗﹀畾涔堿ction鐨刵ame -->

聽聽<action name="*">

聽聽聽<!-- 灏嗚姹傝漿鍙戠粰/WEB-INF/jsp/璺緞涓嬪悓鍚嶇殑JSP椤甸潰 -->

聽聽聽<result>/WEB-INF/jsp/{1}.jsp</result>

聽聽</action>

聽聽聽 </package>

</struts>

繼續閱讀