Java Reference
In-Depth Information
package
stripesbook.ext;
public class
MyLocalePicker
extends
DefaultLocalePicker {
public static final
String LOCALE = "locale";
@Override
public
Locale pickLocale(HttpServletRequest request) {
HttpSession session = request.getSession();
// Look in the request.
String locale = request.getParameter(LOCALE);
if
(locale !=
null
) {
session.setAttribute(LOCALE, locale);
}
// Not found in the request? Look in the session.
else
{
locale = (String) session.getAttribute(LOCALE);
}
// Use the locale if found.
if
(locale !=
null
) {
return new
Locale(locale);
}
// Otherwise, use the default.
return super
.pickLocale(request);
}
}
In the
pickLocale
( ) method, the
locale=
request parameter has priority
for choosing the locale. Next comes the last selected locale, which is
stored in the session. Finally, when the user first accesses the applica-
tion, the method falls back to the default behavior of using the value
supplied by the browser until the user clicks the link to change the
language.
Again, we can just add
MyLocalePicker
to the
stripesbook.ext
package, and
it will automatically be loaded by Stripes because we designated this
package in
web.xml
as the package for Stripes extensions. Gotta love
that!
Now we can add a link to switch from one language to the other at the
bottom of each page by adding this code to
layout_main.jsp
:
<fmt:message var="otherLocale" key="layout.otherLocale"/>
<s:link href="${actionBean.lastUrl}">
<s:param name="locale" value="${otherLocale}"/>
<fmt:message key="layout.otherLanguage"/>
</s:link>
Search WWH ::
Custom Search