Get message from resource bundle - Java java.util

Java examples for java.util:ResourceBundle

Description

Get message from resource bundle

Demo Code


import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.component.html.HtmlInputHidden;

public class Main{
    public static void main(String[] argv) throws Exception{
        String msgKey = "java2s.com";
        System.out.println(getMessage(msgKey));
    }/*  w ww  .j ava 2s  . com*/
    public static final String RESOUCE_URI = "properties.AuditResources";

    public static String getMessage(String msgKey, String param) {
        if (StringUtil.isEmpty(param)) {
            return getMessage(msgKey);
        } else {
            return getMessage(msgKey, new Object[] { param });
        }
    }
    public static String getMessage(String msgKey, Object param) {
        if (null == param) {
            return getMessage(msgKey);
        } else {
            return getMessage(msgKey, new Object[] { param });
        }
    }
    public static String getMessage(String msgKey, Object[] params) {
        String resourceMsg = getMessage(msgKey,
                LocaleUtil.getCurrentLocale(), RESOUCE_URI, params);
        return resourceMsg;
    }
    /**
     * Get message from resource bundle
     * 
     * @param key
     * @param locale
     * @param bundleName
     * @param params
     * @return
     */
    public static String getMessage(String key, Locale locale,
            String bundleName, Object params[]) {

        String text = null;

        ResourceBundle bundle = ResourceBundle
                .getBundle(bundleName, locale);

        try {
            text = bundle.getString(key);
        } catch (Exception e) {
            text = null;
        }

        if (params != null) {
            MessageFormat mf = new MessageFormat(text, locale);
            text = mf.format(params, new StringBuffer(), null).toString();
        }

        return text;
    }
}

Related Tutorials