org.apache.commons.i18n
Class MessageManager

java.lang.Object
  extended by org.apache.commons.i18n.MessageManager

public class MessageManager
extends java.lang.Object

The MessageManager provides methods for retrieving localized messages and adding custom message providers. This class should not be called directly for other purposes than registering a custom MessageProvider or retrieving information about available message entries.

To access localized messages a subclass of the LocalizedBundle class such as LocalizedText should be used:

 LocalizedText welcome = new LocalizedText("welcome"); 
 // Get the german translacion of the retrieved welcome text 
 System.out.println(welcome.getText(Locale.GERMAN));       
 

You can call getText directly, but if you do so, you have to ensure that the given entry key really exists and to deal with the MessageNotFoundException exception that will be thrown if you try to access a not existing entry.


Constructor Summary
MessageManager()
           
 
Method Summary
static void addMessageProvider(java.lang.String providerId, MessageProvider messageProvider)
          Add a custom MessageProvider to the MessageManager.
static java.util.Map getEntries(java.lang.String id, java.util.Locale locale)
          Returns a map containing all available message entries for the given locale.
static java.util.Map getEntries(java.lang.String providerId, java.lang.String id, java.util.Locale locale)
          Returns a map containing all available message entries for the given locale.
static java.lang.String getText(java.lang.String id, java.lang.String entry, java.lang.Object[] arguments, java.util.Locale locale)
          Iterates over all registered message providers in order to find the given entry in the requested message bundle.
static java.lang.String getText(java.lang.String id, java.lang.String entry, java.lang.Object[] arguments, java.util.Locale locale, java.lang.String defaultText)
          Iterates over all registered message providers in order to find the given entry in the requested message bundle.
static java.lang.String getText(java.lang.String providerId, java.lang.String id, java.lang.String entry, java.lang.Object[] arguments, java.util.Locale locale)
          Tries to find the desired entry in the named message provider.
static java.lang.String getText(java.lang.String providerId, java.lang.String id, java.lang.String entry, java.lang.Object[] arguments, java.util.Locale locale, java.lang.String defaultText)
          Iterates over all registered message providers in order to find the given entry in the requested message bundle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageManager

public MessageManager()
Method Detail

addMessageProvider

public static void addMessageProvider(java.lang.String providerId,
                                      MessageProvider messageProvider)
Add a custom MessageProvider to the MessageManager. It will be incorporated in later calls of the getText or getEntriesmethods.

Parameters:
providerId - Id of the provider used for uninstallation and qualified naming.
messageProvider - The MessageProvider to be added.

getText

public static java.lang.String getText(java.lang.String id,
                                       java.lang.String entry,
                                       java.lang.Object[] arguments,
                                       java.util.Locale locale)
                                throws MessageNotFoundException
Iterates over all registered message providers in order to find the given entry in the requested message bundle.

Parameters:
id - The identifier that will be used to retrieve the message bundle
entry - The desired message entry
arguments - The dynamic parts of the message that will be evaluated using the standard java text formatting abilities.
locale - The locale in which the message will be printed
Returns:
The localized text
Throws:
MessageNotFoundException - Will be thrown if no message bundle can be found for the given id or if the desired message entry is missing in the retrieved bundle

getText

public static java.lang.String getText(java.lang.String id,
                                       java.lang.String entry,
                                       java.lang.Object[] arguments,
                                       java.util.Locale locale,
                                       java.lang.String defaultText)
Iterates over all registered message providers in order to find the given entry in the requested message bundle.

Parameters:
id - The identifier that will be used to retrieve the message bundle
entry - The desired message entry
arguments - The dynamic parts of the message that will be evaluated using the standard java text formatting abilities.
locale - The locale in which the message will be printed
defaultText - If no message bundle or message entry could be found for the specified parameters, the default text will be returned.
Returns:
The localized text or the default text if the message could not be found

getText

public static java.lang.String getText(java.lang.String providerId,
                                       java.lang.String id,
                                       java.lang.String entry,
                                       java.lang.Object[] arguments,
                                       java.util.Locale locale)
                                throws MessageNotFoundException
Tries to find the desired entry in the named message provider.

Parameters:
providerId - The name of the message provider (i.e. source) to use for the message
id - The identifier that will be used to retrieve the message bundle
entry - The desired message entry
arguments - The dynamic parts of the message that will be evaluated using the standard java text formatting abilities.
locale - The locale in which the message will be printed
Returns:
The localized text
Throws:
MessageNotFoundException - Will be thrown if the requested message provider cannot be found or no message bundle can be found for the given id or if the desired message entry is missing in the retrieved bundle

getText

public static java.lang.String getText(java.lang.String providerId,
                                       java.lang.String id,
                                       java.lang.String entry,
                                       java.lang.Object[] arguments,
                                       java.util.Locale locale,
                                       java.lang.String defaultText)
Iterates over all registered message providers in order to find the given entry in the requested message bundle.

Parameters:
providerId - The name of the message provider (i.e. source) to use for the message
id - The identifier that will be used to retrieve the message bundle
entry - The desired message entry
arguments - The dynamic parts of the message that will be evaluated using the standard java text formatting abilities.
locale - The locale in which the message will be printed
defaultText - If no message bundle or message entry could be found for the specified parameters, the default text will be returned.
Returns:
The localized text or the default text if the message could not be found

getEntries

public static java.util.Map getEntries(java.lang.String id,
                                       java.util.Locale locale)
                                throws MessageNotFoundException
Returns a map containing all available message entries for the given locale. The map contains keys of type Stringcontaining the keys of the available message entries and values of type String containing the localized message entries.

Throws:
MessageNotFoundException

getEntries

public static java.util.Map getEntries(java.lang.String providerId,
                                       java.lang.String id,
                                       java.util.Locale locale)
                                throws MessageNotFoundException
Returns a map containing all available message entries for the given locale. The map contains keys of type Stringcontaining the keys of the available message entries and values of type String containing the localized message entries.

Throws:
MessageNotFoundException


Copyright 2004-2008 The Apache Software Foundation. All Rights Reserved.