org.ceryle.util
Class Bundlizer

java.lang.Object
  extended by org.ceryle.util.Bundlizer

public class Bundlizer
extends Object

A class that handles String replacement using ResourceBundles.

The first parameter id may be an ID reference from the localized ListResourceBundle, or if not recognized as such, will be treated simply as a message String. Following the initial id, there are additional methods available with one to four additional passed parameters, each returning the localized result.

  public String message( String id )
  public String message( String id, String arg1 )
  public String message( String id, String arg1, String arg2 )
  public String message( String id, String arg1, String arg2, String arg3 )
  public String message( String id, String arg1, String arg2, String arg3, String arg4 )
 

In use, it's simply a matter of passing a reference to a ResourceBundle ID (e.g., "MessageId.M147" below) plus any arguments:

    String s = bndlizer.get(MessageId.M147,arg1,arg2);
 

Optional ResourceBundles

There are additional methods for setting multiple ResourceBundles, referenced by baseName, so that once bundles have been registered with the internal Hashtable, the specific one in use can be altered via setBundle(String).

Since:
JDK1.3
Version:
$Id: Bundlizer.java,v 3.5 2007-06-15 12:09:55 altheim Exp $
Author:
Murray Altheim

Field Summary
static String defaultBundleName
          A reference to the class baseName for the default ResourceBundle.
protected  Hashtable m_bundles
           
static String[] NParams
          The numbered String-replacement parameters "%0" through "%4".
 
Constructor Summary
Bundlizer()
          Default constructor.
 
Method Summary
 boolean addBundle(String baseName, Locale locale)
          Adds a named ResourceBundle for the given Locale to the registry by reference to its baseName.
 ResourceBundle getDefaultBundle()
          Returns the default ResourceBundle for the default locale.
 String localize(String id)
          Return a localized message given a message id and no arguments.
 String localize(String id, String arg1)
          Return a localized message given a message id and one argument.
 String localize(String id, String arg1, String arg2)
          Return a localized message given a message id and two arguments.
 String localize(String id, String arg1, String arg2, String arg3)
          Return a localized message given a message id and three arguments.
 String localize(String id, String arg1, String arg2, String arg3, String arg4)
          Return a localized message given a message id and four arguments.
 String processMessage(String id, Object[] args)
          Get message text from the default, localized ResourceBundle using id as key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text.
 String processMessage(String id, Object[] args, ResourceBundle bundle)
          Get message text from the supplied ResourceBundle using id as key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text.
 boolean removeBundle(String baseName)
          Removes the ResourceBundle from the registry by its baseName.
 void setBundle(String baseName)
          Sets the ResourceBundle to be used by its baseName.
static String sub(String src, String srch, String repl)
          Replace the first occurrence of a string srch in string src by replacement repl.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultBundleName

public static String defaultBundleName
A reference to the class baseName for the default ResourceBundle. The default value is "org.ceryle.util.Messages".


NParams

public static final String[] NParams
The numbered String-replacement parameters "%0" through "%4".


m_bundles

protected Hashtable m_bundles
Constructor Detail

Bundlizer

public Bundlizer()
Default constructor.

Method Detail

getDefaultBundle

public ResourceBundle getDefaultBundle()
Returns the default ResourceBundle for the default locale.


setBundle

public void setBundle(String baseName)
Sets the ResourceBundle to be used by its baseName. This must be a ResourceBundle previously added via addBundle(String,Locale). If no ResourceBundle matches the provided baseName, the default remains in place.


addBundle

public boolean addBundle(String baseName,
                         Locale locale)
Adds a named ResourceBundle for the given Locale to the registry by reference to its baseName. The value for the baseName is a Java package name. If the Locale is null, the default is used. If a ResourceBundle matches the provided baseName, the new bundle overwrites the old in the registry.

Returns:
true if the bundle was added to the registry.

removeBundle

public boolean removeBundle(String baseName)
Removes the ResourceBundle from the registry by its baseName.

Returns:
true if the bundle was removed

localize

public String localize(String id)
Return a localized message given a message id and no arguments. Note that the id can simply be a text message.

Parameters:
id - the MessageId within the ListResourceBundle Messages.
Returns:
a localized version of the message.

localize

public String localize(String id,
                       String arg1)
Return a localized message given a message id and one argument.

Parameters:
id - the MessageId within the ListResourceBundle Messages.
arg1 - argument substituting %1 in the message string.
Returns:
a localized version of the message.

localize

public String localize(String id,
                       String arg1,
                       String arg2)
Return a localized message given a message id and two arguments.

Parameters:
id - the MessageId within the ListResourceBundle Messages.
arg1 - argument substituting %1 in the message string.
arg2 - argument substituting %2 in the message string.
Returns:
a localized version of the message.

localize

public String localize(String id,
                       String arg1,
                       String arg2,
                       String arg3)
Return a localized message given a message id and three arguments.

Parameters:
id - the MessageId within the ListResourceBundle Messages.
arg1 - argument substituting %1 in the message string.
arg2 - argument substituting %2 in the message string.
arg3 - argument substituting %3 in the message string.
Returns:
a localized version of the message.

localize

public String localize(String id,
                       String arg1,
                       String arg2,
                       String arg3,
                       String arg4)
Return a localized message given a message id and four arguments.

Parameters:
id - the MessageId within the ListResourceBundle Messages.
arg1 - argument substituting %1 in the message string.
arg2 - argument substituting %2 in the message string.
arg3 - argument substituting %3 in the message string.
arg4 - argument substituting %4 in the message string.
Returns:
a localized version of the message.

processMessage

public String processMessage(String id,
                             Object[] args)
Get message text from the default, localized ResourceBundle using id as key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text. This uses the default ResourceBundle unless one has been set.


processMessage

public String processMessage(String id,
                             Object[] args,
                             ResourceBundle bundle)
Get message text from the supplied ResourceBundle using id as key, then substitute arguments into (%1, %2, %3, etc.) placeholders in text.


sub

public static final String sub(String src,
                               String srch,
                               String repl)
Replace the first occurrence of a string srch in string src by replacement repl. If the pattern isn't found, the string is returned unchanged.

Parameters:
src - the source string.
srch - the string to search for.
repl - the replacement string.
Returns:
Returns the string with the first instance of the search pattern replaced by the replacement string. If no occurrences of the search pattern are found, returns the string unchanged.


The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.