001: package com.anthonyeden.lib.util;
002:
003: import java.text.MessageFormat;
004: import java.util.Locale;
005: import java.util.ResourceBundle;
006:
007: /**
008: * Utilities for retrieving localized messages.
009: *
010: * @author Anthony Eden
011: */
012:
013: public class MessageUtilities {
014:
015: /**
016: * Private constructor so that instances of MessageUtilities are not
017: * constructed.
018: */
019:
020: private MessageUtilities() {
021: // no op
022: }
023:
024: /**
025: * Locate the given message and merge it with the given arguments using
026: * the MessageFormat class. This method uses ResourceBundle to locate
027: * a bundle called org.jpublish.messages and then pulls the appropriate
028: * message.
029: *
030: * @param name The message name
031: * @param args The arguments
032: * @return The message
033: */
034:
035: public static String getMessage(String pkg, String name,
036: Object[] args) {
037: return getMessage(pkg, name, args, Locale.getDefault());
038: }
039:
040: /**
041: * Locate the given message and merge it with the given arguments using
042: * the MessageFormat class. This method uses ResourceBundle to locate
043: * a bundle called org.jpublish.messages and then pulls the appropriate
044: * message.
045: *
046: * <p>The pkg argument is used to prepend a package name to the
047: * bundle name so that different JARs can have different bundles. For
048: * example, if pkg is com.foo then the Bundle should be located by the
049: * classloader at com/foo/messages.properties. pkg can be null in which
050: * case /messages.properties will be used.
051: *
052: * @param pkg The package
053: * @param name The message name
054: * @param args The arguments
055: * @param locale The Locale
056: * @return The message
057: */
058:
059: public static String getMessage(String pkg, String name,
060: Object[] args, Locale locale) {
061: String propertiesPath = null;
062: if (pkg == null) {
063: propertiesPath = "messages";
064: } else {
065: propertiesPath = pkg + ".messages";
066: }
067:
068: ResourceBundle res = ResourceBundle.getBundle(propertiesPath,
069: locale);
070:
071: return MessageFormat.format(res.getString(name), args);
072: }
073:
074: /**
075: * Locate the given message and merge it with the given arguments using
076: * the MessageFormat class. This method uses ResourceBundle to locate
077: * a bundle called org.jpublish.messages and then pulls the appropriate
078: * message. The actual message key is determined by concatenating the
079: * class name and the name parameter.
080: *
081: * @param c The requesting class
082: * @param pkg The package name
083: * @param name The message name
084: * @param args The arguments
085: * @return The message
086: */
087:
088: public static String getMessage(Class c, String pkg, String name,
089: Object[] args) {
090: return getMessage(pkg, c.getName() + "." + name, args, Locale
091: .getDefault());
092: }
093:
094: /**
095: * Locate the given message and merge it with the given arguments using
096: * the MessageFormat class. This method uses ResourceBundle to locate
097: * a bundle called org.jpublish.messages and then pulls the appropriate
098: * message. The actual message key is determined by concatenating the
099: * class name and the name parameter.
100: *
101: * @param c The requesting Class
102: * @param name The message name
103: * @param args The arguments
104: * @param locale The locale
105: * @return The message
106: */
107:
108: public static String getMessage(Class c, String pkg, String name,
109: Object[] args, Locale locale) {
110: return getMessage(pkg, c.getName() + "." + name, args, locale);
111: }
112:
113: }
|