Package org.apache.fulcrum.localization
Class SimpleLocalizationServiceImpl
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
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:
- getBundle("MyBundleName")
- getBundle("MyBundleName", Locale)
- etc.
- Version:
- $Id: DefaultLocalizationService.java 535465 2007-05-05 06:58:06Z tv $ avalon.component name="localization" lifestyle="singleton" avalon.service type="org.apache.fulcrum.localization.SimpleLocalizationService"
- 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
configure
(org.apache.avalon.framework.configuration.Configuration conf) Avalon lifecycle methodConfigurable
Looks up the value forkey
in theResourceBundle
referenced bybundleName
, then formats that value for the specifiedLocale
usingargs
.Formats a localized value using the provided objects.Convenience method to get the defaultResourceBundle
.Returns a ResourceBundle given the bundle name and the default locale information supplied by the configuration.Convenience method to get a ResourceBundle based on name and Locale.String[]
Retrieves the default country (specified in the config file).Retrieves the default language (specified in the config file).Retrieves the default Locale (as created from default language and default country).Returns the value for the key in the default bundle and the default locale.protected final 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
Called the first time the Service is used.void
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
-
Constructor Details
-
SimpleLocalizationServiceImpl
public SimpleLocalizationServiceImpl()Creates a new instance.
-
-
Method Details
-
configure
public void configure(org.apache.avalon.framework.configuration.Configuration conf) throws org.apache.avalon.framework.configuration.ConfigurationException Avalon lifecycle methodConfigurable
- Specified by:
configure
in interfaceorg.apache.avalon.framework.configuration.Configurable
- Parameters:
conf
- the configuration- Throws:
org.apache.avalon.framework.configuration.ConfigurationException
- if failed to configure
-
initialize
Called the first time the Service is used.- Specified by:
initialize
in interfaceorg.apache.avalon.framework.activity.Initializable
- Throws:
Exception
- generic exception
-
initBundleNames
Initialize list of default bundle names.- Parameters:
intBundleNames
- set bundle names
-
getDefaultLanguage
Retrieves the default language (specified in the config file).- Specified by:
getDefaultLanguage
in interfaceSimpleLocalizationService
- Returns:
- the default language
-
getDefaultCountry
Retrieves the default country (specified in the config file).- Specified by:
getDefaultCountry
in interfaceSimpleLocalizationService
- Returns:
- the default country
-
getDefaultLocale
Retrieves the default Locale (as created from default language and default country).- Specified by:
getDefaultLocale
in interfaceSimpleLocalizationService
- Returns:
- the default locale
-
getDefaultBundleName
- Specified by:
getDefaultBundleName
in interfaceSimpleLocalizationService
- Returns:
- the default bundle name
-
getBundleNames
- Specified by:
getBundleNames
in interfaceSimpleLocalizationService
- Returns:
- list of bundle names available
-
getBundle
Description copied from interface:SimpleLocalizationService
Convenience method to get the defaultResourceBundle
.- Specified by:
getBundle
in interfaceSimpleLocalizationService
- Returns:
- the default resource bundle
- See Also:
-
getBundle
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 interfaceSimpleLocalizationService
- Parameters:
bundleName
- the name of a bundle- Returns:
- the resource bundle
- See Also:
-
getBundle
Description copied from interface:SimpleLocalizationService
Convenience method to get a ResourceBundle based on name and Locale.- Specified by:
getBundle
in interfaceSimpleLocalizationService
- Parameters:
bundleName
- Name of bundle (ornull
for the default bundle).locale
- The locale (ornull
for the locale indicated by the default language and country).- Returns:
- A localized ResourceBundle.
- See Also:
-
setBundle
This method sets the name of the first bundle in the search list (the "default" bundle).- Specified by:
setBundle
in interfaceSimpleLocalizationService
- Parameters:
defaultBundle
- Name of default bundle.
-
getString
public String getString(String bundleName, Locale locale, String key) throws MissingResourceException - Specified by:
getString
in interfaceSimpleLocalizationService
- 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.
-
getString
Returns the value for the key in the default bundle and the default locale.- Specified by:
getString
in interfaceSimpleLocalizationService
- Parameters:
key
- The key to retrieve the value for.- Returns:
- The value mapped to the key.
-
getStringOrNull
Gets localized text from a bundle if it's there. Otherwise, returnsnull
(ignoring a possibleMissingResourceException
).- Parameters:
rb
- resource bundlekey
- The key to retrieve the value for.- Returns:
- name of resource
-
format
- Specified by:
format
in interfaceSimpleLocalizationService
- Parameters:
bundleName
- the bundle namelocale
- localekey
- key to lookuparg1
- bundle arguments- Returns:
- Formatted localized text.
- See Also:
-
format
Description copied from interface:SimpleLocalizationService
Formats a localized value using the provided objects.- Specified by:
format
in interfaceSimpleLocalizationService
- 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:
-
format
Looks up the value forkey
in theResourceBundle
referenced bybundleName
, then formats that value for the specifiedLocale
usingargs
. Iflocale
isnull
,getDefaultLocale()
will be checked. IfbundleName
isnull
,getDefaultBundleName()
will be checked (cft.getBundle(String, Locale)
.- Specified by:
format
in interfaceSimpleLocalizationService
- 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
.
-