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