|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.oreilly.servlet.LocaleNegotiator
public class LocaleNegotiator
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"));
LocaleToCharsetMap
Constructor Summary | |
---|---|
LocaleNegotiator(java.lang.String bundleName,
java.lang.String languages,
java.lang.String charsets)
Constructs a new LocaleNegotiator for the given bundle name, language list, and charset list. |
Method Summary | |
---|---|
java.util.ResourceBundle |
getBundle()
Gets the chosen bundle. |
java.lang.String |
getCharset()
Gets the chosen charset. |
java.util.Locale |
getLocale()
Gets the chosen locale. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public LocaleNegotiator(java.lang.String bundleName, java.lang.String languages, java.lang.String charsets)
bundleName
- the resource bundle namelanguages
- the Accept-Language headercharsets
- the Accept-Charset headerMethod Detail |
---|
public java.util.ResourceBundle getBundle()
public java.util.Locale getLocale()
public java.lang.String getCharset()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |