org.springframework.extensions.surf.util
Class I18NUtil

java.lang.Object
  extended by org.springframework.extensions.surf.util.I18NUtil

public class I18NUtil
extends Object

Utility class providing methods to access the Locale of the current thread and to get Localised strings.

Author:
Roy Wetherall

Constructor Summary
I18NUtil()
           
 
Method Summary
static Map<String,String> getAllMessages()
           
static Map<String,String> getAllMessages(Locale locale)
           
static Locale getContentLocale()
          Get the content local for the current thread.
This will revert to getLocale() if no value has been defined.
static Locale getContentLocaleOrNull()
          Get the content local for the current thread.
This will revert null if no value has been defined.
static Locale getLocale()
          Get the general local for the current thread, will revert to the default locale if none specified for this thread.
static String getMessage(String messageKey)
          Get message from registered resource bundle.
static String getMessage(String messageKey, Locale locale)
          Get a localised message string
static String getMessage(String messageKey, Locale locale, Object... params)
          Get a localised message string, parameterized using standard MessageFormatter.
static String getMessage(String messageKey, Object... params)
          Get a localised message string, parameterized using standard MessageFormatter.
static Locale getNearestLocale(Locale templateLocale, Set<Locale> options)
          Searches for the nearest locale from the available options.
static Locale parseLocale(String localeStr)
          Factory method to create a Locale from a lang_country_variant string.
static void registerResourceBundle(String bundleBaseName)
          Register a resource bundle.
static void setContentLocale(Locale locale)
          Set the content locale for the current thread.
static void setLocale(Locale locale)
          Set the locale for the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

I18NUtil

public I18NUtil()
Method Detail

setLocale

public static void setLocale(Locale locale)
Set the locale for the current thread.

Parameters:
locale - the locale

getLocale

public static Locale getLocale()
Get the general local for the current thread, will revert to the default locale if none specified for this thread.

Returns:
the general locale

setContentLocale

public static void setContentLocale(Locale locale)
Set the content locale for the current thread.

Parameters:
locale - the content locale

getContentLocale

public static Locale getContentLocale()
Get the content local for the current thread.
This will revert to getLocale() if no value has been defined.

Returns:
Returns the content locale

getContentLocaleOrNull

public static Locale getContentLocaleOrNull()
Get the content local for the current thread.
This will revert null if no value has been defined.

Returns:
Returns the content locale

getNearestLocale

public static Locale getNearestLocale(Locale templateLocale,
                                      Set<Locale> options)
Searches for the nearest locale from the available options. To match any locale, pass in null.

Parameters:
templateLocale - the template to search for or null to match any locale
options - the available locales to search from
Returns:
Returns the best match from the available options, or the null if all matches fail

parseLocale

public static Locale parseLocale(String localeStr)
Factory method to create a Locale from a lang_country_variant string.

Parameters:
localeStr - e.g. fr_FR
Returns:
Returns the locale instance, or the default if the string is invalid

registerResourceBundle

public static void registerResourceBundle(String bundleBaseName)
Register a resource bundle.

This should be the bundle base name eg, spring-surf.messages.errors

Once registered the messges will be available via getMessage

Parameters:
bundleBaseName - the bundle base name

getMessage

public static String getMessage(String messageKey)
Get message from registered resource bundle.

Parameters:
messageKey - message key
Returns:
localised message string, null if not found

getMessage

public static String getMessage(String messageKey,
                                Locale locale)
Get a localised message string

Parameters:
messageKey - the message key
locale - override the current locale
Returns:
the localised message string, null if not found

getMessage

public static String getMessage(String messageKey,
                                Object... params)
Get a localised message string, parameterized using standard MessageFormatter.

Parameters:
messageKey - message key
params - format parameters
Returns:
the localised string, null if not found

getMessage

public static String getMessage(String messageKey,
                                Locale locale,
                                Object... params)
Get a localised message string, parameterized using standard MessageFormatter.

Parameters:
messageKey - the message key
locale - override current locale
params - the localised message string
Returns:
the localaised string, null if not found

getAllMessages

public static Map<String,String> getAllMessages()
Returns:
the map of all available messages for the current locale

getAllMessages

public static Map<String,String> getAllMessages(Locale locale)
Returns:
the map of all available messages for the specified locale


Copyright © 2009 SpringSource, Inc. All Rights Reserved.