Posted on 2009-06-25 16:18
landor 閱讀(2543)
評論(1) 編輯 收藏 所屬分類:
struts1
struts的配置配置文件默認是根據瀏覽器的語言來設置locale的,通過locale來讀取相應名稱的資源文件,完成國際化顯示的
比如,資源文件名稱如下
htmlMessages.properties
htmlMessages_en.properties
htmlMessages_zh_CN.properties
htmlMessages_fr.properties
htmlMessages是默認尋找的資源文件,當locale為Engish的時候,struts去尋找htmlMessages_en文件,沒找到就尋找htmlMessages文件
在session建立的時候,struts會把瀏覽器發送的locale信息放入session,而且僅僅在剛剛建立session的時候放入一次,以后的操作便不再執行了,所以我們只需要修改session的這個locale,代碼如下:
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.CHINA); //設置中文
動態修改語言如下:
Locale locale = new Locale(request.getParameter("language"));
request.getSession().setAttribute(Globals.LOCALE_KEY, locale);