Java Resource Message getMessage(final String bundleName, final Locale locale, final String key, final Object... params)

Here you can find the source of getMessage(final String bundleName, final Locale locale, final String key, final Object... params)

Description

Get a message from a resource bundle present in classpath.

License

Open Source License

Parameter

Parameter Description
bundleName The bundle name
locale The locale of the message
key The message key
params Message parameters (optional)

Return

The formatted message

Declaration

public static String getMessage(final String bundleName, final Locale locale, final String key,
        final Object... params) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*  w  w  w  . j a v  a 2  s  .co  m*/
     * Get a message from a resource bundle present in classpath.
     * 
     * @param bundleName
     *            The bundle name
     * @param locale
     *            The locale of the message
     * @param key
     *            The message key
     * @param params
     *            Message parameters (optional)
     * @return The formatted message
     */
    public static String getMessage(final String bundleName, final Locale locale, final String key,
            final Object... params) {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        final ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, classLoader);
        String text = bundle.getString(key);
        if (params != null) {
            final MessageFormat mf = new MessageFormat(text, locale);
            text = mf.format(params, new StringBuffer(), null).toString();
        }
        return text;
    }
}

Related

  1. getMessage(final Locale locale, final String messageKey)
  2. getMessage(Locale locale, String key, Object... args)
  3. getMessage(Locale locale, String key, String... args)
  4. getMessage(Properties prop, String key, Object... args)
  5. getMessage(Properties props, String key, Object param)