get Message Resource String - Java java.util

Java examples for java.util:ResourceBundle

Description

get Message Resource String

Demo Code


//package com.java2s;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    public static String getMessageResourceString(String bundleName,
            String key, Object params[], Locale locale) {
        String text = null;/*  w ww  .j  a v  a2  s  . c  o  m*/
        ResourceBundle bundle = ResourceBundle.getBundle(bundleName,
                locale, getCurrentClassLoader(params));
        try {
            text = bundle.getString(key);
        } catch (MissingResourceException e) {
            text = "key " + key + " not found";
        }
        if (params != null) {
            MessageFormat mf = new MessageFormat(text, locale);
            text = mf.format(params, new StringBuffer(), null).toString();
        }
        return text;
    }

    protected static ClassLoader getCurrentClassLoader(Object defaultObject) {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = defaultObject.getClass().getClassLoader();
        }
        return loader;
    }
}

Related Tutorials