ResourceBundleUtil.java

package com.tradecloud.common.util;

import java.util.*;

public class ResourceBundleUtil {

    private static ResourceBundle resourceBundle;

    @Deprecated
    public static <X extends Enum<?>> Map<String, X> getEnumMap(Class<X> c) {
        resourceBundle = ResourceBundle.getBundle("en-resource-bundle");
        Map<String, X> choices = new TreeMap<>();
        for (X type : c.getEnumConstants()) {
            choices.put(resourceBundle.getString(type.name()), type);
        }
        return choices;
    }

    public static String getResourceValue(String name) {
        resourceBundle = ResourceBundle.getBundle("en-resource-bundle");
        if (name != null) {
            try {
                return resourceBundle.getString(name);
            } catch (Exception e) {

                return "Missing label (" + name + ")";
            }
        } else {
            return "";
        }
    }

    public static String getResourceValue(String name, String... params) {
        return getResourceValue(name, Arrays.asList(params));
    }

    public static String getResourceValue(String name, List<String> params) {
        String bundleValue = getResourceValue(name);

        StringTokenizer tokens = new StringTokenizer(bundleValue, "{}");
        StringBuilder builder = new StringBuilder();

        while (tokens.hasMoreElements()) {
            String tokenKey;
            String tokenValue;

            if (tokens.hasMoreTokens()) {
                tokenKey = tokens.nextToken();
                builder.append(tokenKey);
            }

            if (tokens.hasMoreTokens()) {
                tokenValue = tokens.nextToken();
                builder.append(params.get(Integer.parseInt(tokenValue)));
            }
        }

        return builder.toString();
    }
}