org.barracudamvc.plankton.l10n
Class Locales

java.lang.Object
  extended by org.barracudamvc.plankton.l10n.Locales
Direct Known Subclasses:
Locales

public class Locales
extends 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 String COUNTRY_KEY
           
static String LANGAUGE_KEY
           
protected static org.apache.log4j.Logger logger
           
static int NONE
           
static int PERSIST_DEFAULT
           
static int SESSION
           
static String VARIANT_KEY
           
 
Constructor Summary
Locales()
           
 
Method Summary
static int findClosestLocale(Locale targetLocale, 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 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 Locale getClientLocale(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, String languageKey, String countryKey, String variantKey, int persistOption)
          This method attempts to get the client locale.
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(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, 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, 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 String LANGAUGE_KEY

COUNTRY_KEY

public static String COUNTRY_KEY

VARIANT_KEY

public static 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 final org.apache.log4j.Logger logger
Constructor Detail

Locales

public Locales()
Method Detail

getClientLocale

public static 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 Locale getClientLocale(javax.servlet.http.HttpServletRequest req,
                                     javax.servlet.http.HttpServletResponse resp,
                                     String languageKey,
                                     String countryKey,
                                     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(javax.servlet.http.HttpServletRequest req,
                                    javax.servlet.http.HttpServletResponse resp,
                                    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,
                                    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(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(Locale targetLocale,
                                    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

Parameters:
targetLocale - the target locale
locales - the array of locales to search
defaultIndex - the default index (if no match is found)
Returns:
the index of the closest matching locale


Copyright © 2006 BarracudaMVC.org All Rights Reserved.