Recipe12.1.Detecting Browser Language Settings


Recipe 12.1. Detecting Browser Language Settings

Problem

Your web application needs to support the language settings of the client's browser.

Solution

If you are using the default settings of Struts, you don't have to do anything; the browser's locale will be automatically detected.

Discussion

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 checks if the controller is configured to set the locale automatically. If you don't want the controller to set the locale (which results in the creation of an HttpSession), set the controller's locale attribute to false in the struts-config.xml file.

<controller locale="false"/>

You need to ensure the browser renders your generated HTML appropriately for the user's language. The HTML html tag supports the lang attribute whose value represents the language generally used within the document. Here's how it would look if the page consisted primarily of text written in Russian:

<html lang="ru">

The value of the lang attribute should be set to an ISO-639 standard two-character language code. These codes are essentially the same language codes used by the Java Locale. You can indicate a language dialect by adding a subcode name; the subcode equates to the two-character country code of a Locale. For the lang attribute, separate the language code from the subcode with a hyphen; not an underscore (_) as used by Locale. You would indicate U.S. English as "en-US" and French Canadian as "fr-CA."

Struts can automatically generate an appropriate lang attribute for you. In Struts 1.1, set the locale attribute of the html:html tag to true to create the lang attribute.

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <html:html locale="true">

The html:html tag uses the same heuristic to resolve the current locale as the processLocale( ) method shown in Example 12-1. The difference is that the tag allows you to process the locale on an as-needed basis, whereas the processLocale( ) method fires on every request unless explicitly disabled.

The html:html tag first looks for a Locale in the session; if one is found, it uses it. Otherwise, the tag looks for the language from the HttpServletRequest. The servlet request returns a Locale based on the Accept-Language HTTP header. If this header value is not set, then the server's default Locale is returned. The html:html tag stores the Locale in the session, creating the session if it didn't exist.

Some web applications are written not to use HttpSessions. With Struts 1.1, it was impossible to use the locale attribute of the html:html tag without creating an HttpSession. This problem has been resolved in Struts 1.2. The locale attribute of the html:html tag has been replaced with the lang attribute. Setting the lang attribute to TRue will render an html tag with the lang attribute set to the appropriate language but won't create an HttpSession:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:html lang="true">

See Also

The Struts User's Guide includes a section on internationalization that covers this and other related topics. The relevant section can be found at http://struts.apache.org/userGuide/building_view.html#i18n.

Sun's Java Tutorial has a trail on internationalization at http://java.sun.com/docs/books/tutorial/i18n/.

Information on HTML's support for internationalization, such as the lang attribute of the html tag, can be found in HTML and XHTML: The Definitive Guide by Chuck Musciano and Bill Kennedy (O'Reilly).



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net