[ Team LiB ] Previous Section Next Section

Detecting the Browser's Preferred Locale

The browser sends a number of header fields in each request indicating certain preferences. One of these preferences is a list of languages that the browser prefers for Web pages. The ACCEPT-LANGUAGE header value contains a list of languages that the browser is prepared to handle. Using this header value, you can locate a locale that you are prepared to support and deliver content targeted toward that locale.

For example, suppose you have a Web site that is in both German and English. Although each version contains links to allow you to change languages at any time, you might want to default the user to the language his browser prefers. For example, if you see de-DE as the preferred language, you send the user to the German version of the page, and if you see en-US or en-UK, you send the user to the English version.

Listing 22.1 shows a JSP that detects the preferred language sent by the browser and chooses one of two possible pages.

Listing 22.1 Source Code for MyPage.jsp
<%
    String lang = request.getHeader("ACCEPT-LANGUAGE");

    String whatPage = "MyPage_En.html";

    if (lang.startsWith("de"))
    {
        whatPage = "MyPage_De.html";
    }
%><jsp:forward page="<%=whatPage%>"/>

Don't Expect Immediate Changes

graphics/bytheway_icon.gif

When you change the language setting in Internet Explorer, you must shut down the browser and restart it before the change takes effect.


Figure 22.1 shows the JSP when viewed from a browser with a preferred language setting of English (en/US), and Figure 22.2 shows the JSP viewed from a browser that prefers German (de/DE).

Figure 22.1. You can create a multilingual Web site by detecting the preferred language.

graphics/22fig01.gif

Figure 22.2. When you provide alternate language pages, include links to see the other language versions.

graphics/22fig02.gif

Setting a Preferred Language

graphics/bytheway_icon.gif

Both Netscape and Internet Explorer let you set the preferred language for your browser. These settings allow you to view your alternate language pages without changing the language setting for the entire operating system.


    [ Team LiB ] Previous Section Next Section