struts的配置配置文件默認(rèn)是根據(jù)瀏覽器的語言來設(shè)置locale的,通過locale來讀取相應(yīng)名稱的資源文件,完成國際化顯示的
比如,資源文件名稱如下
htmlMessages.properties
htmlMessages_en.properties
htmlMessages_zh_CN.properties
htmlMessages_fr.properties
htmlMessages是默認(rèn)尋找的資源文件,當(dāng)locale為Engish的時候,struts去尋找htmlMessages_en文件,沒找到就尋找htmlMessages文件
在session建立的時候,struts會把瀏覽器發(fā)送的locale信息放入session,而且僅僅在剛剛建立session的時候放入一次,以后的操作便不再執(zhí)行了,所以我們只需要修改session的這個locale,代碼如下:
比如,資源文件名稱如下
htmlMessages.properties
htmlMessages_en.properties
htmlMessages_zh_CN.properties
htmlMessages_fr.properties
htmlMessages是默認(rèn)尋找的資源文件,當(dāng)locale為Engish的時候,struts去尋找htmlMessages_en文件,沒找到就尋找htmlMessages文件
在session建立的時候,struts會把瀏覽器發(fā)送的locale信息放入session,而且僅僅在剛剛建立session的時候放入一次,以后的操作便不再執(zhí)行了,所以我們只需要修改session的這個locale,代碼如下:
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.CHINA); //設(shè)置中文
動態(tài)修改語言如下:Locale locale = new Locale(request.getParameter("language"));
request.getSession().setAttribute(Globals.LOCALE_KEY, locale);
request.getSession().setAttribute(Globals.LOCALE_KEY, locale);