Java Resource Message getMessageNoKey(String messageName)

Here you can find the source of getMessageNoKey(String messageName)

Description

This method does the same thing as getMessage() but does not prepend the message key to the message.

License

Apache License

Parameter

Parameter Description
messageName The key to the desired message (see MessageConstants)

Return

The desired message. The messageName will be returned if the requested message is not found.

Declaration

public static String getMessageNoKey(String messageName) 

Method Source Code


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

import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    public static final String MESSAGE_FILE_NAME = ".message";
    public static final String ERROR_FILE_NAME = ".error";
    /**/*  ww w.  j a v a 2s .c  om*/
     * Loads the properties file.  The file loaded will be named
     * "message_en.properties" if the default locale is "en" or
     * "message_fr.properties" if the default locale is "fr".  If it cannot
     * find the proper file, message.properties will be loaded.  If that file
     * is missing
     */
    protected static Hashtable<String, ResourceBundle> MESSAGES = new Hashtable<>();

    /**
     * This method does the same thing as getMessage() but does not prepend the message key
     * to the message.  If the message is not found, the key is returned.
     *
     * @param messageName The key to the desired message (see MessageConstants)
     * @return The desired message.  The messageName will be returned if the
     *         requested message is not found.
     */
    public static String getMessageNoKey(String messageName) {
        try {
            return findMessage(messageName);
        } catch (Exception e) {
            return messageName;
        }
    }

    /**
     * Does the same thing as getMessage() but does not prepend the message key to the message.
     * The message key is returned if the message is not found.
     *
     * @param messageName The key to the desired message (see MessageConstants)
     * @param param       The parameters to use for substitution in the message.
     * @return The desired string with the parameters substituted.
     */
    public static String getMessageNoKey(String messageName, Object[] param) {
        try {
            return MessageFormat.format(findMessage(messageName), param);
        } catch (Exception e) {
            return messageName;
        }
    }

    /**
     * Searches for the string key in the existing ResourceBundles.  If it is
     * not found, this method will try loading a new message bundle based on
     * the "dot" notation in the key (see loadResource).
     *
     * @param key The message key to search for.
     * @return The message found, or key if no message can be found.
     */
    private static String findMessage(String key) {
        if (key == null) {
            return null;
        }

        synchronized (MESSAGES) {
            Enumeration values = MESSAGES.elements();

            while (values.hasMoreElements()) {
                ResourceBundle aBundle = (ResourceBundle) (values.nextElement());

                try {
                    return aBundle.getString(key);
                } catch (MissingResourceException me) {
                    // indicates that this bundle does not have the key.. continue
                }
            }
        }

        // Did not find the key - try loading a new bundle
        return loadBundle(key);
    }

    /**
     * Tries to load a ResourceBundle based on a key.  Returns the message
     * for the key if the resource bundle is found and it contains the key.
     *
     * @param key The key to the message to be returned.
     * @return The message corresponding to key, if found, key otherwise.
     */
    private static String loadBundle(String key) {
        int location = key.lastIndexOf(".");
        if (location == key.length() - 1) {
            // Indicates that the "." is at the end of the string.  If this is
            // the case, this may not be a message key at all, but a message that
            // ends with a period.  Don't search for a message in that case.
            // Look for the next "."
            String tempString = key.substring(0, location - 1);
            location = tempString.lastIndexOf(".");
        }
        String subString = "";
        if (location != -1) {
            subString = key.substring(0, location);
        }
        // true when we find the bundle to load
        boolean foundBundle = false;

        // true when we found the bundle already loaded but missing the key we are looking for.
        boolean haveMessageBundle = false;
        boolean haveErrorBundle = false;
        String reply = null;

        while ((location != -1) && !foundBundle) {
            if (haveMessageBundle && haveErrorBundle) {
                // we found both bundles, but the message key isn't in them - break here.
                break;
            }

            try {
                String name = subString + MESSAGE_FILE_NAME;

                // if we already have this bundle, don't reload
                if (MESSAGES.containsKey(name)) {
                    haveMessageBundle = true;
                } else {
                    ResourceBundle newBundle = ResourceBundle.getBundle(name);
                    foundBundle = true;
                    synchronized (MESSAGES) {
                        MESSAGES.put(name, newBundle);
                    }

                    // See if we got the message in this resource bundle
                    reply = newBundle.getString(key);
                }
            } catch (MissingResourceException me) {
                // means the file wasn't found, or the message was not in 
                // the new bundle, so continue
            }
            try {
                String name = subString + ERROR_FILE_NAME;

                // if we already have this bundle, don't reload
                if (MESSAGES.containsKey(name)) {
                    haveErrorBundle = true;
                } else {
                    ResourceBundle newBundle = ResourceBundle.getBundle(name);
                    foundBundle = true;
                    synchronized (MESSAGES) {
                        MESSAGES.put(name, newBundle);
                    }

                    if (reply == null) {
                        reply = newBundle.getString(key);
                    }
                }
            } catch (MissingResourceException me) {
                // means the file wasn't found, or the message was not in 
                // the new bundle, so continue
            }

            location = subString.lastIndexOf(".");
            if (location != -1) {
                subString = subString.substring(0, location);
            }
        }
        if (reply != null) {
            return reply;
        }

        return key;
    }
}

Related

  1. getMessage(String pkg, String name, Object[] args)
  2. getMessage(String value, Object... args)
  3. getMessageCreateTime()
  4. getMessageFormat(final String bundleKey, final String messageKey, final Locale locale)
  5. getMessageFormattedTime(Date date)
  6. getMessages(final Class cls)
  7. getMessageString(String bundleName, String key, Object[] params, Locale locale)
  8. getMessageString(String key, Locale locale)
  9. getMessageString(String key, Locale locale)