國際化是商業系統中不可或缺的一部分,所以無論您學習的是什麼Web框架,它都是必須掌握的技能。 其實,Struts 1.x在此部分已經做得相當不錯了。它極大地簡化了我們程序員在做國際化時所需的工作,例如,如果您要輸出一條國際化的信息,只需在代碼包中加入FILE -NAME_xx_XX.properties(其中FILE-NAME爲默認資源文件的文件名),然後在struts-config.xml中指明其路徑,再在頁面用<bean:message>標誌輸出即可。 不過,所謂“沒有最好,只有更好”。Struts 2.0並沒有在這部分止步,而是在原有的簡單易用的基礎上,將其做得更靈活、更強大。 國際化Hello World下面讓我們看一個例子——HelloWorld。這個例子演示如何根據用戶瀏覽器的設置輸出相應的HelloWorld。
<s:textfield name="name" label="%{getText('UserName')}"/>
資源文件查找順序之所以說Struts 2.0的國際化更靈活是因爲它可以能根據不同需要配置和獲取資源(properties)文件。在Struts 2.0中有下面幾種方法:
上面我列舉了四種配置和訪問資源的方法,它們的範圍分別是從大到小,而Struts 2.0在查找國際化字符串所遵循的是特定的順序,如圖3所示: 500)this.width=500" border=0>圖3 資源文件查找順序圖 假設我們在某個ChildAction中調用了getText("user.title"),Struts 2.0的將會執行以下的操作:
參數化國際化字符串許多情況下,我們都需要在動行時(runtime)爲國際化字符插入一些參數,例如在輸入驗證提示信息的時候。在Struts 2.0中,我們通過以下兩種方法做到這點:
讓用戶方便地選擇語言開發國際化的應用程序時,有一個功能是必不可少的——讓用戶快捷地選擇或切換語言。在Struts 2.0中,通過ActionContext.getContext().setLocale(Locale arg)可以設置用戶的默認語言。不過,由於這是一個比較普遍的應用場景(Scenario),所以Struts 2.0爲您提供了一個名i18n的攔截器(Interceptor),並在默認情況下將其註冊到攔截器鏈(Interceptor chain)中。它的原理爲在執行Action方法前,i18n攔截器查找請求中的一個名爲"request_locale"的參數。如果其存在,攔截器就將其作爲參數實例化Locale對象,並將其設爲用戶默認的區域(Locale),最後,將此Locale對象保存在session的名爲 “WW_TRANS_I18N_LOCALE”的屬性中。 下面,我將提供一完整示例演示它的使用方法。 500)this.width=500" align=top border=0>package tutorial; tutorial/Locales.java500)this.width=500" align=top border=0> 500)this.width=500" align=top border=0>import java.util.Hashtable; 500)this.width=500" align=top border=0>import java.util.Locale; 500)this.width=500" align=top border=0>import java.util.Map; 500)this.width=500" align=top border=0> 500)this.width=500" align=top border=0>500)this.width=500" align=top border=0>publicclass Locales { 500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public Map<String, Locale> getLocales() { 500)this.width=500" align=top border=0> Map<String, Locale> locales =new Hashtable<String, Locale>(2); 500)this.width=500" align=top border=0> locales.put("American English", Locale.US); 500)this.width=500" align=top border=0> locales.put("Simplified Chinese", Locale.CHINA); 500)this.width=500" align=top border=0> return locales; 500)this.width=500" align=top border=0> } 500)this.width=500" align=top border=0>} <%@taglib prefix="s" uri="/struts-tags"%> LangSelector.jsp
<script type="text/javascript"> <!-- function langSelecter_onChanged() { document.langForm.submit(); } //--> </script> <s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/> <s:bean id="locales" name="tutorial.Locales"/> <form action="<s:url includeParams="get" encode="true"/>" name="langForm" style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;"> Language: <s:select label="Language" list="#locales.locales" listKey="value" listValue="key" value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE" name="request_locale" id="langSelecter" onchange="langSelecter_onChanged()" theme="simple"/> </form> 上述代碼的原理爲,LangSelector.jsp先實例化一個Locales對象,並把對象的Map類型的屬性locales賦予下拉列表(select)。如此一來,下拉列表就獲得可用語言的列表。大家看到LangSelector有<s:form>標誌和一段Javascript腳本,它們的作用就是在用戶在下拉列表中選擇了後,提交包含“reqeust_locale”變量的表單到Action。在打開頁面時,爲了下拉列表的選中的當前區域,我們需要到session取得當前區域(鍵爲“WW_TRANS_I18N_LOCALE”的屬性),而該屬性在沒有設置語言前是爲空的,所以通過值棧中locale屬性來取得當前區域(用戶瀏覽器所設置的語言)。 你可以把LangSelector.jsp作爲一個控件使用,方法是在JSP頁面中把它包含進來,代碼如下所示:<s:include value="/LangSelector.jsp"/> 在例1中的HellloWorld.jsp中<body>後加入上述代碼,並在struts.xml中新建Action,代碼如下: <action name="HelloWorld"> <result>/HelloWorld.jsp</result> </action> 或者,如果你多個JSP需要實現上述功能,你可以使用下面的通用配置,而不是爲每一個JSP頁面都新建一個Action。 <action name="*"> <result>/{1}.jsp</result> </action> 分佈運行程序,在瀏覽器的地址欄中輸入http://localhost:8080/Struts2_i18n/HelloWorld.action,出現圖4所示頁面: 500)this.width=500" border=0> 圖3 HelloWorld.action 在下拉列表中,選擇“American English”,出現圖5所示頁面: 500)this.width=500" border=0> 圖4 HelloWorld.action
|
【Struts2.0】在Struts 2.0中國際化(i18n)您的應用程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.