org.enhydra.barracuda.core.util.l10n
Class Locales

java.lang.Object
  |
  +--org.enhydra.barracuda.core.util.l10n.Locales

public class Locales
extends java.lang.Object

Simple locale utilities. This class makes it easy to determine the target locale from an event context or a servlet request. You can also set the locale and ask the class to save the information for you (in a cookie and/or the session) so that the locale info will persist across muliple requests


Field Summary
static int COOKIES_AND_SESSION
           
static java.lang.String COUNTRY_KEY
           
static java.lang.String LANGAUGE_KEY
           
protected static org.apache.log4j.Logger logger
           
static int NONE
           
static int PERSIST_DEFAULT
           
static int SESSION
           
static java.lang.String VARIANT_KEY
           
 
Constructor Summary
Locales()
           
 
Method Summary
static int findClosestLocale(java.util.Locale targetLocale, java.util.Locale[] locales, int defaultIndex)
          This utility function will run through a list of Locales and return the index of the locale that matches most closely.
static java.util.Locale getClientLocale(EventContext ec)
          Get the client locale from an EventContext using the default param keys and persist option.
static java.util.Locale getClientLocale(EventContext ec, java.lang.String languageKey, java.lang.String countryKey, java.lang.String variantKey, int persistOption)
          Get the client locale from an EventContext
static java.util.Locale getClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Get the client locale from a ServletRequest using the default param keys and persist option
static java.util.Locale getClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, java.lang.String languageKey, java.lang.String countryKey, java.lang.String variantKey, int persistOption)
          This method attempts to get the client locale.
static void releaseClientLocale(EventContext ec)
          Release the client locale using an EventContext (this effectively removes it from whereever it might have been persisted).
static void releaseClientLocale(EventContext ec, int persistOption)
          Release the client locale using an EventContext (this effectively removes it from whereever it might have been persisted).
static void releaseClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Release a client locale (this effectively removes it from whereever it might have been persisted)
static void releaseClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, int persistOption)
          Release a client locale (this effectively removes it from whereever it might have been persisted)
static void saveClientLocale(EventContext ec, java.util.Locale loc)
          Save the client locale using an EventContext using the default persist option
static void saveClientLocale(EventContext ec, java.util.Locale loc, int persistOption)
          Save the client locale using an EventContext
static void saveClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, java.util.Locale loc)
          Here we actually force a locale to be saved using the default persist option.
static void saveClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, java.util.Locale loc, int persistOption)
          Here we actually force a locale to be saved.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LANGAUGE_KEY

public static java.lang.String LANGAUGE_KEY

COUNTRY_KEY

public static java.lang.String COUNTRY_KEY

VARIANT_KEY

public static java.lang.String VARIANT_KEY

NONE

public static final int NONE
See Also:
Constant Field Values

SESSION

public static final int SESSION
See Also:
Constant Field Values

COOKIES_AND_SESSION

public static final int COOKIES_AND_SESSION
See Also:
Constant Field Values

PERSIST_DEFAULT

public static int PERSIST_DEFAULT

logger

protected static org.apache.log4j.Logger logger
Constructor Detail

Locales

public Locales()
Method Detail

getClientLocale

public static java.util.Locale getClientLocale(EventContext ec)
Get the client locale from an EventContext using the default param keys and persist option. This will probably be the method you use most frequently.

Parameters:
ec - the EventContext from which we'd like to determine Locale
Returns:
the target client Locale

getClientLocale

public static java.util.Locale getClientLocale(EventContext ec,
                                               java.lang.String languageKey,
                                               java.lang.String countryKey,
                                               java.lang.String variantKey,
                                               int persistOption)
Get the client locale from an EventContext

Parameters:
ec - the EventContext from which we'd like to determine Locale
languageKey - the key to be used to look in the request for a language paramter
countryKey - the key to be used to look in the request for a country paramter
variantKey - the key to be used to look in the request for a variant paramter
persistOption - how we'd like to persist the Locale (by default, it will be stored in the SESSION)
Returns:
the target client Locale

getClientLocale

public static java.util.Locale getClientLocale(javax.servlet.http.HttpServletRequest req,
                                               javax.servlet.http.HttpServletResponse resp)
Get the client locale from a ServletRequest using the default param keys and persist option

Parameters:
req - the HttpServletRequest from which we'd like to determine Locale
resp - the HttpServletResponse (needed if we want to save Locale in a cookie otherwise it may be null)
Returns:
the target client Locale

getClientLocale

public static java.util.Locale getClientLocale(javax.servlet.http.HttpServletRequest req,
                                               javax.servlet.http.HttpServletResponse resp,
                                               java.lang.String languageKey,
                                               java.lang.String countryKey,
                                               java.lang.String variantKey,
                                               int persistOption)
This method attempts to get the client locale.

First we look to see if we can determine the locale by the req's form parameters (language, country, variant). If not, we look in the session to see if we can get the information from there. If that fails, we see if we can get the information from a client cookie (where the value is a comma delimited string containing language, country, and variant. If we still haven't got the locale information, we try and retrieve it from the servlet request, and if that fails, we use the default locale.

In each of these cases we save (depending on persistOption) the locale info in both the session and in a client cookie (so as to speed lookups on subsequent requests).

Parameters:
req - the HttpServletRequest from which we'd like to determine Locale
resp - the HttpServletResponse (needed if we want to save Locale in a cookie otherwise it may be null)
languageKey - the key to be used to look in the request for a language paramter
countryKey - the key to be used to look in the request for a country paramter
variantKey - the key to be used to look in the request for a variant paramter
persistOption - how we'd like to persist the Locale (by default, it will be stored in the SESSION)
Returns:
the target client Locale

saveClientLocale

public static void saveClientLocale(EventContext ec,
                                    java.util.Locale loc)
Save the client locale using an EventContext using the default persist option

Parameters:
ec - the EventContext in which we'd like to set Locale
loc - the target client locale we'd like to set

saveClientLocale

public static void saveClientLocale(EventContext ec,
                                    java.util.Locale loc,
                                    int persistOption)
Save the client locale using an EventContext

Parameters:
ec - the EventContext in which we'd like to set Locale
loc - the target client locale we'd like to set
persistOption - the specific persistOption to be used

saveClientLocale

public static void saveClientLocale(javax.servlet.http.HttpServletRequest req,
                                    javax.servlet.http.HttpServletResponse resp,
                                    java.util.Locale loc)
Here we actually force a locale to be saved using the default persist option. It will be stored in a cookie and/or the session.

Parameters:
req - the HttpServletRequest (needed to get the HttpSession)
resp - the HttpServletResponse (needed if we want to save Locale in a cookie otherwise it may be null)
loc - the target client locale we'd like to set

saveClientLocale

public static void saveClientLocale(javax.servlet.http.HttpServletRequest req,
                                    javax.servlet.http.HttpServletResponse resp,
                                    java.util.Locale loc,
                                    int persistOption)
Here we actually force a locale to be saved. It will be stored in a cookie and/or the session.

Parameters:
req - the HttpServletRequest (needed to get the HttpSession)
resp - the HttpServletResponse (needed if we want to save Locale in a cookie otherwise it may be null)
loc - the target client locale we'd like to set
persistOption - the specific persistOption to be used

releaseClientLocale

public static void releaseClientLocale(EventContext ec)
Release the client locale using an EventContext (this effectively removes it from whereever it might have been persisted). This means that on the next request the locale will be determined from scratch again.

Parameters:
ec - the EventContext in which we'd like to set Locale (this tells us where the locale info needs to be removed from)

releaseClientLocale

public static void releaseClientLocale(EventContext ec,
                                       int persistOption)
Release the client locale using an EventContext (this effectively removes it from whereever it might have been persisted). This means that on the next request the locale will be determined from scratch again.

Parameters:
ec - the EventContext in which we'd like to set Locale
persistOption - the specific persistOption to be used (this tells us where the locale info needs to be removed from)

releaseClientLocale

public static void releaseClientLocale(javax.servlet.http.HttpServletRequest req,
                                       javax.servlet.http.HttpServletResponse resp)
Release a client locale (this effectively removes it from whereever it might have been persisted)

Parameters:
req - the HttpServletRequest (needed to get the HttpSession)
resp - the HttpServletResponse (needed if we want to clear Locale from a cookie otherwise it may be null)

releaseClientLocale

public static void releaseClientLocale(javax.servlet.http.HttpServletRequest req,
                                       javax.servlet.http.HttpServletResponse resp,
                                       int persistOption)
Release a client locale (this effectively removes it from whereever it might have been persisted)

Parameters:
req - the HttpServletRequest (needed to get the HttpSession)
resp - the HttpServletResponse (needed if we want to clear Locale from a cookie otherwise it may be null)
persistOption - the specific persistOption to be used (this tells us where the locale info needs to be removed from)

findClosestLocale

public static int findClosestLocale(java.util.Locale targetLocale,
                                    java.util.Locale[] locales,
                                    int defaultIndex)
This utility function will run through a list of Locales and return the index of the locale that matches most closely. If none of them match whatsoever, return the defaultIndex

Returns:
the index of the closest matching locale


Copyright © 2001 Enhydra.org