February 2005
Intermediate to advanced
528 pages
12h 53m
English
Your web application needs to support the language settings of the client's browser.
If you are using the default settings of Struts, you don't have to do anything; the browser's locale will be automatically detected.
When a request is routed through the Struts
RequestProcessor, the processLocale() method is called. Example 12-1 shows the
implementation of this method in the
RequestProcessor from Struts 1.2.
Example 12-1. How the RequestProcessor sets the locale
/**
* <p>Automatically select a <code>Locale</code> for the current user,
* if requested.
* <strong>NOTE</strong> - configuring Locale selection will trigger
* the creation of a new <code>HttpSession</code> if necessary.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*/
protected void processLocale(HttpServletRequest request,
HttpServletResponse response) {
// Are we configured to select the Locale automatically?
if (!moduleConfig.getControllerConfig( ).getLocale( )) {
return;
}
// Has a Locale already been selected?
HttpSession session = request.getSession( );
if (session.getAttribute(Globals.LOCALE_KEY) != null) {
return;
}
// Use the Locale returned by the servlet container (if any)
Locale locale = request.getLocale( );
if (locale != null) {
if (log.isDebugEnabled( )) {
log.debug(" Setting user locale '" + locale + "'");
}
session.setAttribute(Globals.LOCALE_KEY, locale);
}
}The method first ...