org.apache.turbine.services.localization
Class TurbineLocalizationService

java.lang.Object
  extended byorg.apache.turbine.services.BaseInitable
      extended byorg.apache.turbine.services.BaseService
          extended byorg.apache.turbine.services.TurbineBaseService
              extended byorg.apache.turbine.services.localization.TurbineLocalizationService
All Implemented Interfaces:
Initable, LocalizationService, Service

public class TurbineLocalizationService
extends TurbineBaseService
implements LocalizationService

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

Usage example:

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

Then call one of four methods to retrieve a ResourceBundle:

Version:
$Id: TurbineLocalizationService.java 534527 2007-05-02 16:10:59Z tv $
Author:
Jonas Maurus, Jon S. Stevens, Frank Y. Kim, Daniel Rall, Leonard Richardson, Henning P. Schmiedehausen

Field Summary
 
Fields inherited from class org.apache.turbine.services.BaseService
configuration, name, serviceBroker
 
Fields inherited from class org.apache.turbine.services.BaseInitable
initableBroker, isInitialized
 
Fields inherited from interface org.apache.turbine.services.localization.LocalizationService
ACCEPT_LANGUAGE, SERVICE_NAME
 
Constructor Summary
TurbineLocalizationService()
          Constructor.
 
Method Summary
 java.lang.String format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object arg1)
          Formats a localized value using the provided object.
 java.lang.String format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object[] args)
          Looks up the value for key in the ResourceBundle referenced by bundleName, then formats that value for the specified Locale using args.
 java.lang.String format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
          Formats a localized value using the provided objects.
 java.util.ResourceBundle getBundle()
          This method returns a ResourceBundle given the bundle name "DEFAULT" and the default Locale information supplied in TurbineProperties.
 java.util.ResourceBundle getBundle(javax.servlet.http.HttpServletRequest req)
          This method returns a ResourceBundle given the Locale information supplied in the HTTP "Accept-Language" header which is stored in HttpServletRequest.
 java.util.ResourceBundle getBundle(RunData data)
          This method returns a ResourceBundle given the Locale information supplied in the HTTP "Accept-Language" header which is stored in RunData.
 java.util.ResourceBundle getBundle(java.lang.String bundleName)
          This method returns a ResourceBundle given the bundle name and the default Locale information supplied in TurbineProperties.
 java.util.ResourceBundle getBundle(java.lang.String bundleName, javax.servlet.http.HttpServletRequest req)
          This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header which is stored in HttpServletRequest.
 java.util.ResourceBundle getBundle(java.lang.String bundleName, java.util.Locale locale)
          This method returns a ResourceBundle for the given bundle name and the given Locale.
 java.util.ResourceBundle getBundle(java.lang.String bundleName, RunData data)
          This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header which is stored in RunData.
 java.util.ResourceBundle getBundle(java.lang.String bundleName, java.lang.String languageHeader)
          This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header.
 java.lang.String[] getBundleNames()
          Retrieves the list of names of bundles to search by default for ResourceBundle keys (as specified in the config file).
 java.lang.String getDefaultBundleName()
          Retrieves the name of the default bundle (as specified in the config file).
 java.lang.String getDefaultCountry()
          Retrieves the default country (specified in the config file).
 java.lang.String getDefaultLanguage()
          Retrieves the default language (specified in the config file).
 java.util.Locale getLocale(javax.servlet.http.HttpServletRequest req)
          Attempts to pull the Accept-Language header out of the HttpServletRequest object and then parse it.
 java.util.Locale getLocale(java.lang.String header)
          This method parses the Accept-Language header and attempts to create a Locale out of it.
 java.lang.String getString(java.lang.String bundleName, java.util.Locale locale, java.lang.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  java.lang.String getStringOrNull(java.util.ResourceBundle rb, java.lang.String key)
          Gets localized text from a bundle if it's there.
 void init()
          Called the first time the Service is used.
protected  void initBundleNames(java.lang.String[] ignored)
          Initialize list of default bundle names.
 void setBundle(java.lang.String defaultBundle)
          This method sets the name of the first bundle in the search list (the "default" bundle).
 
Methods inherited from class org.apache.turbine.services.TurbineBaseService
init, init, init, shutdown
 
Methods inherited from class org.apache.turbine.services.BaseService
getConfiguration, getName, getProperties, getServiceBroker, setName, setServiceBroker
 
Methods inherited from class org.apache.turbine.services.BaseInitable
getInit, getInitableBroker, setInit, setInitableBroker
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.turbine.services.Service
getConfiguration, getName, getProperties, setName, setServiceBroker
 
Methods inherited from interface org.apache.turbine.services.Initable
getInit, init, setInitableBroker, shutdown
 

Constructor Detail

TurbineLocalizationService

public TurbineLocalizationService()
Constructor.

Method Detail

init

public void init()
          throws InitializationException
Called the first time the Service is used.

Specified by:
init in interface Initable
Overrides:
init in class TurbineBaseService
Throws:
InitializationException

initBundleNames

protected void initBundleNames(java.lang.String[] ignored)
Initialize list of default bundle names.

Parameters:
ignored - Ignored.

getDefaultLanguage

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

Specified by:
getDefaultLanguage in interface LocalizationService

getDefaultCountry

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

Specified by:
getDefaultCountry in interface LocalizationService

getDefaultBundleName

public java.lang.String getDefaultBundleName()
Retrieves the name of the default bundle (as specified in the config file).

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

getBundleNames

public java.lang.String[] getBundleNames()
Description copied from interface: LocalizationService
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 LocalizationService
Returns:
The list of configured bundle names.
See Also:
LocalizationService.getBundleNames()

getBundle

public java.util.ResourceBundle getBundle()
This method returns a ResourceBundle given the bundle name "DEFAULT" and the default Locale information supplied in TurbineProperties.

Specified by:
getBundle in interface LocalizationService
Returns:
A localized ResourceBundle.

getBundle

public java.util.ResourceBundle getBundle(java.lang.String bundleName)
This method returns a ResourceBundle given the bundle name and the default Locale information supplied in TurbineProperties.

Specified by:
getBundle in interface LocalizationService
Parameters:
bundleName - Name of bundle.
Returns:
A localized ResourceBundle.

getBundle

public java.util.ResourceBundle getBundle(java.lang.String bundleName,
                                          java.lang.String languageHeader)
This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header.

Specified by:
getBundle in interface LocalizationService
Parameters:
bundleName - Name of bundle.
languageHeader - A String with the language header.
Returns:
A localized ResourceBundle.

getBundle

public java.util.ResourceBundle getBundle(javax.servlet.http.HttpServletRequest req)
This method returns a ResourceBundle given the Locale information supplied in the HTTP "Accept-Language" header which is stored in HttpServletRequest.

Specified by:
getBundle in interface LocalizationService
Parameters:
req - HttpServletRequest.
Returns:
A localized ResourceBundle.

getBundle

public java.util.ResourceBundle getBundle(java.lang.String bundleName,
                                          javax.servlet.http.HttpServletRequest req)
This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header which is stored in HttpServletRequest.

Specified by:
getBundle in interface LocalizationService
Parameters:
bundleName - Name of the bundle to use if the request's locale cannot be resolved.
req - HttpServletRequest.
Returns:
A localized ResourceBundle.

getBundle

public java.util.ResourceBundle getBundle(RunData data)
This method returns a ResourceBundle given the Locale information supplied in the HTTP "Accept-Language" header which is stored in RunData.

Parameters:
data - Turbine information.
Returns:
A localized ResourceBundle.

getBundle

public java.util.ResourceBundle getBundle(java.lang.String bundleName,
                                          RunData data)
This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header which is stored in RunData.

Parameters:
bundleName - Name of bundle.
data - Turbine information.
Returns:
A localized ResourceBundle.

getBundle

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

Specified by:
getBundle in interface LocalizationService
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(java.lang.String defaultBundle)
This method sets the name of the first bundle in the search list (the "default" bundle).

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

getLocale

public final java.util.Locale getLocale(javax.servlet.http.HttpServletRequest req)
Description copied from interface: LocalizationService
Attempts to pull the Accept-Language header out of the HttpServletRequest object and then parse it. If the header is not present, it will return a null Locale.

Specified by:
getLocale in interface LocalizationService
Parameters:
req - The HTTP request to parse the Accept-Language of.
Returns:
The parsed locale.
See Also:
LocalizationService.getLocale(HttpServletRequest)

getLocale

public java.util.Locale getLocale(java.lang.String header)
Description copied from interface: LocalizationService
This method parses the Accept-Language header and attempts to create a Locale out of it.

Specified by:
getLocale in interface LocalizationService
Parameters:
header - The Accept-Language HTTP header.
Returns:
The parsed locale.
See Also:
LocalizationService.getLocale(String)

getString

public java.lang.String getString(java.lang.String bundleName,
                                  java.util.Locale locale,
                                  java.lang.String key)
Description copied from interface: LocalizationService
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 LocalizationService
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:
java.util.MissingResourceException - Specified key cannot be matched.
See Also:
LocalizationService.getString(String, Locale, String)

getStringOrNull

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


format

public java.lang.String format(java.lang.String bundleName,
                               java.util.Locale locale,
                               java.lang.String key,
                               java.lang.Object arg1)
Formats a localized value using the provided object.

Specified by:
format in interface LocalizationService
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:
format(String, Locale, String, Object[])

format

public java.lang.String format(java.lang.String bundleName,
                               java.util.Locale locale,
                               java.lang.String key,
                               java.lang.Object arg1,
                               java.lang.Object arg2)
Formats a localized value using the provided objects.

Specified by:
format in interface LocalizationService
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(String, Locale, String, Object[])

format

public java.lang.String format(java.lang.String bundleName,
                               java.util.Locale locale,
                               java.lang.String key,
                               java.lang.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 LocalizationService
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 when formatting the localized text.
Returns:
Localized, formatted text identified by key.


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.