org.apache.fulcrum.localization
Class SimpleLocalizationServiceImpl

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.fulcrum.localization.SimpleLocalizationServiceImpl
All Implemented Interfaces:
org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.logger.LogEnabled, SimpleLocalizationService
Direct Known Subclasses:
DefaultLocalizationService

public class SimpleLocalizationServiceImpl
extends org.apache.avalon.framework.logger.AbstractLogEnabled
implements SimpleLocalizationService, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.activity.Initializable

This class is the single point of access to all localization resources. It caches different ResourceBundles for different Locales.

Usage example:

 SimpleLocalizationService ls = (SimpleLocalizationService) TurbineServices
     .getInstance().getService(SimpleLocalizationService.SERVICE_NAME);
 

Then call getString(String, Locale, String), or one of two methods to retrieve a ResourceBundle:

Version:
$Id: DefaultLocalizationService.java 535465 2007-05-05 06:58:06Z tv $
Author:
Jonas Maurus, Jon S. Stevens, Frank Y. Kim, Daniel Rall, Leonard Richardson, Stephen McConnell, Thomas Vandahl

Field Summary
 
Fields inherited from interface org.apache.fulcrum.localization.SimpleLocalizationService
ROLE, SERVICE_NAME
 
Constructor Summary
SimpleLocalizationServiceImpl()
          Creates a new instance.
 
Method Summary
 void configure(org.apache.avalon.framework.configuration.Configuration conf)
          Avalon lifecycle method
 String format(String bundleName, Locale locale, String key, Object arg1)
          Formats a localized value using the provided object.
 String format(String bundleName, Locale locale, String key, Object[] args)
          Looks up the value for key in the ResourceBundle referenced by bundleName, then formats that value for the specified Locale using args.
 String format(String bundleName, Locale locale, String key, Object arg1, Object arg2)
          Formats a localized value using the provided objects.
 ResourceBundle getBundle()
          Convenience method to get the default ResourceBundle.
 ResourceBundle getBundle(String bundleName)
          Returns a ResourceBundle given the bundle name and the default locale information supplied by the configuration.
 ResourceBundle getBundle(String bundleName, Locale locale)
          This method returns a ResourceBundle for the given bundle name and the given Locale.
 String[] getBundleNames()
          Retrieves the list of names of bundles to search by default for ResourceBundle keys (as specified in the config file).
 String getDefaultBundleName()
          Retrieves the name of the default bundle (as specified in the config file), or the first in the list if there are more than one.
 String getDefaultCountry()
          Retrieves the default country (specified in the config file).
 String getDefaultLanguage()
          Retrieves the default language (specified in the config file).
 Locale getDefaultLocale()
          Retrieves the default Locale (as created from default language and default country).
 String getString(String bundleName, Locale locale, String key)
          Tries very hard to return a value, looking first in the specified bundle, then searching list of default bundles (giving precedence to earlier bundles over later bundles).
protected  String getStringOrNull(ResourceBundle rb, String key)
          Gets localized text from a bundle if it's there.
protected  void initBundleNames(String[] intBundleNames)
          Initialize list of default bundle names.
 void initialize()
          Called the first time the Service is used.
 void setBundle(String defaultBundle)
          This method sets the name of the first bundle in the search list (the "default" bundle).
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleLocalizationServiceImpl

public SimpleLocalizationServiceImpl()
Creates a new instance.

Method Detail

configure

public void configure(org.apache.avalon.framework.configuration.Configuration conf)
               throws org.apache.avalon.framework.configuration.ConfigurationException
Avalon lifecycle method

Specified by:
configure in interface org.apache.avalon.framework.configuration.Configurable
Throws:
org.apache.avalon.framework.configuration.ConfigurationException
See Also:
Configurable}

initialize

public void initialize()
                throws Exception
Called the first time the Service is used.

Specified by:
initialize in interface org.apache.avalon.framework.activity.Initializable
Throws:
Exception

initBundleNames

protected void initBundleNames(String[] intBundleNames)
Initialize list of default bundle names.

Parameters:
ignored - names Ignored.

getDefaultLanguage

public String getDefaultLanguage()
Retrieves the default language (specified in the config file).

Specified by:
getDefaultLanguage in interface SimpleLocalizationService

getDefaultCountry

public String getDefaultCountry()
Retrieves the default country (specified in the config file).

Specified by:
getDefaultCountry in interface SimpleLocalizationService

getDefaultLocale

public Locale getDefaultLocale()
Retrieves the default Locale (as created from default language and default country).

Specified by:
getDefaultLocale in interface SimpleLocalizationService

getDefaultBundleName

public String getDefaultBundleName()
Description copied from interface: SimpleLocalizationService
Retrieves the name of the default bundle (as specified in the config file), or the first in the list if there are more than one.

Specified by:
getDefaultBundleName in interface SimpleLocalizationService
See Also:
SimpleLocalizationService.getDefaultBundleName()

getBundleNames

public String[] getBundleNames()
Description copied from interface: SimpleLocalizationService
Retrieves the list of names of bundles to search by default for ResourceBundle keys (as specified in the config file).

Specified by:
getBundleNames in interface SimpleLocalizationService
Returns:
The list of configured bundle names.
See Also:
SimpleLocalizationService.getBundleNames()

getBundle

public ResourceBundle getBundle()
Description copied from interface: SimpleLocalizationService
Convenience method to get the default ResourceBundle.

Specified by:
getBundle in interface SimpleLocalizationService
Returns:
A localized ResourceBundle.
See Also:
SimpleLocalizationService.getBundle()

getBundle

public ResourceBundle getBundle(String bundleName)
Description copied from interface: SimpleLocalizationService
Returns a ResourceBundle given the bundle name and the default locale information supplied by the configuration.

Specified by:
getBundle in interface SimpleLocalizationService
Parameters:
bundleName - Name of bundle.
Returns:
A localized ResourceBundle.
See Also:
SimpleLocalizationService.getBundle(String)

getBundle

public ResourceBundle getBundle(String bundleName,
                                Locale locale)
This method returns a ResourceBundle for the given bundle name and the given Locale.

Specified by:
getBundle in interface SimpleLocalizationService
Parameters:
bundleName - Name of bundle (or null for the default bundle).
locale - The locale (or null for the locale indicated by the default language and country).
Returns:
A localized ResourceBundle.

setBundle

public void setBundle(String defaultBundle)
This method sets the name of the first bundle in the search list (the "default" bundle).

Specified by:
setBundle in interface SimpleLocalizationService
Parameters:
defaultBundle - Name of default bundle.

getString

public String getString(String bundleName,
                        Locale locale,
                        String key)
                 throws MissingResourceException
Description copied from interface: SimpleLocalizationService
Tries very hard to return a value, looking first in the specified bundle, then searching list of default bundles (giving precedence to earlier bundles over later bundles).

Specified by:
getString in interface SimpleLocalizationService
Parameters:
bundleName - Name of the bundle to look in first.
locale - Locale to get text for.
key - Name of the text to retrieve.
Returns:
Localized text.
Throws:
MissingResourceException - Specified key cannot be matched.
See Also:
SimpleLocalizationService.getString(String, Locale, String)

getStringOrNull

protected final String getStringOrNull(ResourceBundle rb,
                                       String key)
Gets localized text from a bundle if it's there. Otherwise, returns null (ignoring a possible MissingResourceException).


format

public String format(String bundleName,
                     Locale locale,
                     String key,
                     Object arg1)
Description copied from interface: SimpleLocalizationService
Formats a localized value using the provided object.

Specified by:
format in interface SimpleLocalizationService
Parameters:
bundleName - The bundle in which to look for the localizable text.
locale - The locale for which to format the text.
key - The identifier for the localized text to retrieve,
arg1 - The object to use as {0} when formatting the localized text.
Returns:
Formatted localized text.
See Also:
SimpleLocalizationService.format(String, Locale, String, Object)

format

public String format(String bundleName,
                     Locale locale,
                     String key,
                     Object arg1,
                     Object arg2)
Description copied from interface: SimpleLocalizationService
Formats a localized value using the provided objects.

Specified by:
format in interface SimpleLocalizationService
Parameters:
bundleName - The bundle in which to look for the localizable text.
locale - The locale for which to format the text.
key - The identifier for the localized text to retrieve,
arg1 - The object to use as {0} when formatting the localized text.
arg2 - The object to use as {1} when formatting the localized text.
Returns:
Formatted localized text.
See Also:
SimpleLocalizationService.format(String, Locale, String, Object, Object)

format

public String format(String bundleName,
                     Locale locale,
                     String key,
                     Object[] args)
Looks up the value for key in the ResourceBundle referenced by bundleName, then formats that value for the specified Locale using args.

Specified by:
format in interface SimpleLocalizationService
Parameters:
bundleName - The bundle in which to look for the localizable text.
locale - The locale for which to format the text.
key - The identifier for the localized text to retrieve,
args - The objects to use as {0}, {1}, etc. when formatting the localized text.
Returns:
Localized, formatted text identified by key.


Copyright © 2005-2009 The Apache Software Foundation. All Rights Reserved.