I have a .jsp page that the user loads directly. The request it with a URL like the following:
http://www.example.com/myfile.jsp?country=CA&language=fr
In the JSP, I pull the URL GET parameters and attempt to set the locale using them as follows:
<% 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" />
There are several places in the JSP that then display a message pulled from a localized resource bundle using <bean:message bundle="ts" key="..." />
from Struts.
On the first request for this page (after changing the language in the URL), it is returned in US English (the default Locale), and then subsequent refreshes will return the properly localized content.
Set the Locale
as a session attribute with Globals.LOCALE_KEY
as key.
session.setAttribute(Globals.LOCALE_KEY, myLocale);
See also its javadoc:
The session attributes key under which the user's selected java.util.Locale is stored, if any. If no such attribute is found, the system default locale will be used when retrieving internationalized messages. If used, this attribute is typically set during user login processing.
You actually don't need the JSTL fmt:setLocale
.
That said, I'd do this in a single Filter
rather than in every JSP.