Java Resource Message getMessage(String pkg, String name, Object[] args)

Here you can find the source of getMessage(String pkg, String name, Object[] args)

Description

Locate the given message and merge it with the given arguments using the MessageFormat class.

License

Apache License

Parameter

Parameter Description
name The message name
args The arguments

Return

The message

Declaration


public static String getMessage(String pkg, String name, Object[] args) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class Main {
    /**/*from w  w  w . j  a v  a 2s .co m*/
     * Locate the given message and merge it with the given arguments using
     * the MessageFormat class.  This method uses ResourceBundle to locate 
     * a bundle called org.jpublish.messages and then pulls the appropriate
     * message.
     *
     * @param name The message name
     * @param args The arguments
     * @return The message
     */

    public static String getMessage(String pkg, String name, Object[] args) {
        return getMessage(pkg, name, args, Locale.getDefault());
    }

    /**
     * Locate the given message and merge it with the given arguments using
     * the MessageFormat class.  This method uses ResourceBundle to locate 
     * a bundle called org.jpublish.messages and then pulls the appropriate
     * message. 
     * 
     * <p>The pkg argument is used to prepend a package name to the 
     * bundle name so that different JARs can have different bundles. For
     * example, if pkg is com.foo then the Bundle should be located by the
     * classloader at com/foo/messages.properties.  pkg can be null in which
     * case /messages.properties will be used.
     *
     * @param pkg The package
     * @param name The message name
     * @param args The arguments
     * @param locale The Locale
     * @return The message
     */

    public static String getMessage(String pkg, String name, Object[] args, Locale locale) {
        String propertiesPath = null;
        if (pkg == null) {
            propertiesPath = "messages";
        } else {
            propertiesPath = pkg + ".messages";
        }

        ResourceBundle res = ResourceBundle.getBundle(propertiesPath, locale);

        return MessageFormat.format(res.getString(name), args);
    }

    /**
     * Locate the given message and merge it with the given arguments using
     * the MessageFormat class.  This method uses ResourceBundle to locate 
     * a bundle called org.jpublish.messages and then pulls the appropriate
     * message.  The actual message key is determined by concatenating the
     * class name and the name parameter.
     *
     * @param c The requesting class
     * @param pkg The package name
     * @param name The message name
     * @param args The arguments
     * @return The message
     */

    public static String getMessage(Class c, String pkg, String name, Object[] args) {
        return getMessage(pkg, c.getName() + "." + name, args, Locale.getDefault());
    }

    /**
     * Locate the given message and merge it with the given arguments using
     * the MessageFormat class.  This method uses ResourceBundle to locate 
     * a bundle called org.jpublish.messages and then pulls the appropriate
     * message.  The actual message key is determined by concatenating the
     * class name and the name parameter.
     *
     * @param c The requesting Class
     * @param name The message name
     * @param args The arguments
     * @param locale The locale
     * @return The message
     */

    public static String getMessage(Class c, String pkg, String name, Object[] args, Locale locale) {
        return getMessage(pkg, c.getName() + "." + name, args, locale);
    }
}

Related

  1. getMessage(String key, Object... contents)
  2. getMessage(String key, Object... values)
  3. getMessage(String key, String... fill)
  4. getMessage(String messageKey)
  5. getMessage(String messageKey, Object[] params)
  6. getMessage(String value, Object... args)
  7. getMessageCreateTime()
  8. getMessageFormat(final String bundleKey, final String messageKey, final Locale locale)
  9. getMessageFormattedTime(Date date)