All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----com.oreilly.servlet.LocaleNegotiator
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"));
public LocaleNegotiator(String bundleName, String languages, String charsets)
public ResourceBundle getBundle()
public Locale getLocale()
public String getCharset()
protected String getCharsetForLocale(Locale loc, String charsets)
All Packages Class Hierarchy This Package Previous Next Index