4
我有一個用戶直接加載的.jsp頁面。用類似下面的URL請求它: http://www.example.com/myfile.jsp?country=CA&language=fr
如何在JSP上設置用戶的語言環境
在JSP中,我拉的網址,以獲取參數,並嘗試使用它們如下設置語言環境:
<% String myLanguage = request.getParameter("language"); String myCountry = request.getParameter("country"); Locale myLocale = new Locale(myLanguage, myCountry); pageContext.setAttribute("myLocale", myLocale, PageContext.PAGE_SCOPE); %> <fmt:setLocale value="${myLocale}" scope="page" />
中有幾個地方然後JSP使用來自Struts的<bean:message bundle="ts" key="..." />
顯示從本地化資源包中提取的消息。
對於此頁面的第一個請求(在更改URL中的語言之後),它以美國英語(默認區域設置)返回,然後後續刷新將返回正確本地化的內容。
哦,使用錯誤的標準進行搜索的樂趣,認爲你知道的比你更多。 – ebynum 2010-06-09 21:54:54
歡迎:) – BalusC 2010-06-09 21:57:28
注意:在Struts2中設置語言環境的傳統方式是使用[* I18nInterceptor *攔截器](http://struts.apache.org/maven/xwork-core/apidocs/index html的?COM/OpenSymphony的/ xwork2 /攔截器/ I18nInterceptor.html)。在這篇文章中詳細闡述:http://stackoverflow.com/a/19546961/814702 – informatik01 2015-03-10 16:04:53