net.sourceforge.stripes.localization
Class DefaultLocalePicker

java.lang.Object
  extended by net.sourceforge.stripes.localization.DefaultLocalePicker
All Implemented Interfaces:
ConfigurableComponent, LocalePicker

public class DefaultLocalePicker
extends Object
implements LocalePicker

Default locale picker that uses a comma separated list of locales in the servlet init parameters to determine the set of locales that are supported by the application. Then at request time matches the user's preference order list as specified by the headers included in the request until it finds one of those locales in the system list. If a match cannot be found, the first locale in the system list will be picked. If there is no list of configured locales then the picker will default the list to a one entry list containing the system locale.

Locales are hierarchical, with up to three levels designating language, country and variant. Only the first level (language) is required. To provide the best match possible the DefaultLocalePicker tracks the one-level matches, two-level matches and three-level matches. If a three level match is found, it will be returned. If not the first two-level match will be returned if one was found. If not, the first one-level match will be returned. If not even a one-level match is found, the first locale supported by the system is returned.

Author:
Tim Fennell

Field Summary
protected  Configuration configuration
          Stores a reference to the configuration passed in at initialization.
protected  Map<Locale,String> encodings
          Contains a map of Locale to preferred character encoding.
static String LOCALE_LIST
          The configuration parameter that is used to lookup a comma separated list of locales that the system supports.
protected  List<Locale> locales
          Stores the configured set of Locales that the system supports, looked up at init time.
 
Constructor Summary
DefaultLocalePicker()
           
 
Method Summary
 void init(Configuration configuration)
          Attempts to read the
 String pickCharacterEncoding(HttpServletRequest request, Locale locale)
          Returns the character encoding to use for the request and locale if one has been specified in the configuration.
 Locale pickLocale(HttpServletRequest request)
          Uses a preference matching algorithm to pick a Locale for the user's request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALE_LIST

public static final String LOCALE_LIST
The configuration parameter that is used to lookup a comma separated list of locales that the system supports.

See Also:
Constant Field Values

configuration

protected Configuration configuration
Stores a reference to the configuration passed in at initialization.


locales

protected List<Locale> locales
Stores the configured set of Locales that the system supports, looked up at init time.


encodings

protected Map<Locale,String> encodings
Contains a map of Locale to preferred character encoding.

Constructor Detail

DefaultLocalePicker

public DefaultLocalePicker()
Method Detail

init

public void init(Configuration configuration)
          throws Exception
Attempts to read the

Specified by:
init in interface ConfigurableComponent
Parameters:
configuration -
Throws:
Exception

pickLocale

public Locale pickLocale(HttpServletRequest request)
Uses a preference matching algorithm to pick a Locale for the user's request. Iterates through the user's acceptable list of Locales, matching them against the system list. On the way through the list records the first Locale to match on Language, and the first locale to match on both Language and Country. If a match is found for all three, Language, Country and Variant, it will be returned. If no three-way match is found the first two-way match found will be returned. If no two-way match way found the first one-way match found will be returned. If no one way match was found, the default system locale will be returned.

Specified by:
pickLocale in interface LocalePicker
Parameters:
request - the request being processed
Returns:
a Locale to use in processing the request

pickCharacterEncoding

public String pickCharacterEncoding(HttpServletRequest request,
                                    Locale locale)
Returns the character encoding to use for the request and locale if one has been specified in the configuration. If no value has been specified, returns null.

Specified by:
pickCharacterEncoding in interface LocalePicker
Parameters:
request - the current request
locale - the locale picked for the request
Returns:
a valid character encoding or null


? Copyright 2005-2006, Stripes Development Team.