All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.oreilly.servlet.LocaleNegotiator

java.lang.Object
   |
   +----com.oreilly.servlet.LocaleNegotiator

public class LocaleNegotiator
extends Object
A class to aid in servlet internationalization. It determines, from a client request, the best charset, locale, and resource bundle to use with the response.

LocaleNegotiator works by scanning through the client's language preferences (sent by browsers in the Accept-Language header) looking for any language for which there exists is a corresponding resource bundle. When it finds a correspondence, it uses the LocaleToCharsetMap class to determine the charset. If there's any problem, it tries to fall back to US English. The logic currently ignores the client's charset preferences (sent in the Accept-Charset header).

It can be used like this:

 String bundleName = "BundleName";
 String acceptLanguage = req.getHeader("Accept-Language");
 String acceptCharset = req.getHeader("Accept-Charset");
  
 LocaleNegotiator negotiator =
   new LocaleNegotiator(bundleName, acceptLanguage, acceptCharset);
  
 Locale locale = negotiator.getLocale();
 String charset = negotiator.getCharset();
 ResourceBundle bundle = negotiator.getBundle();  // may be null
  
 res.setContentType("text/plain; charset=" + charset);
 res.setHeader("Content-Language", locale.getLanguage());
 res.setHeader("Vary", "Accept-Language");
  
 PrintWriter out = res.getWriter();
  
 out.println(bundle.getString("resource"));
 

Version:
1.0, 98/09/18
Author:
Jason Hunter, Copyright © 1998
See Also:
LocaleToCharsetMap

Constructor Index

 o LocaleNegotiator(String, String, String)
Constructs a new LocaleNegotiator for the given bundle name, language list, and charset list.

Method Index

 o getBundle()
Gets the chosen bundle.
 o getCharset()
Gets the chosen charset.
 o getCharsetForLocale(Locale, String)
Gets the best charset for a given locale, selecting from a charset list.
 o getLocale()
Gets the chosen locale.

Constructors

 o LocaleNegotiator
 public LocaleNegotiator(String bundleName,
                         String languages,
                         String charsets)
Constructs a new LocaleNegotiator for the given bundle name, language list, and charset list.

Parameters:
bundleName - the resource bundle name
languages - the Accept-Language header
charsets - the Accept-Charset header

Methods

 o getBundle
 public ResourceBundle getBundle()
Gets the chosen bundle.

Returns:
the chosen bundle
 o getLocale
 public Locale getLocale()
Gets the chosen locale.

Returns:
the chosen locale
 o getCharset
 public String getCharset()
Gets the chosen charset.

Returns:
the chosen charset
 o getCharsetForLocale
 protected String getCharsetForLocale(Locale loc,
                                      String charsets)
Gets the best charset for a given locale, selecting from a charset list. Currently ignores the charset list. Subclasses can override this method to take the list into account.

Parameters:
loc - the locale
charsets - a comma-separated charset list
Returns:
the best charset for the given locale from the given list

All Packages  Class Hierarchy  This Package  Previous  Next  Index