0

I manage two properties is one for the generic messages and one for each module belonging to my system, but the problem I'm having is that when invoking the first method getMessage() always invokes the properties of the resource, when it should go the message.

public class ResourceBundleUtil implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String MESSAGE_PATH = "messages";
    public static final String RESOURCE_PATH = "resources";
    private static HashMap<String, Object> messageBundles = new HashMap<String, Object>();

    public static String getMessage(String key) {
        if (key == null) {
            return null;
        }
        try {
            Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
            if (locale == null) {
                locale = Locale.ENGLISH;
            }
            ResourceBundle messages = (ResourceBundle) messageBundles.get(locale.toString());
            if (messages == null) {
                messages = ResourceBundle.getBundle(MESSAGE_PATH, locale);
                messageBundles.put(locale.toString(), messages);
            }
            return messages.getString(key);
        } catch (Exception e) {
            return key;
        }
    }

    public static String getResource(String key) {
        if (key == null) {
            return null;
        }
        try {
            Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
            if (locale == null) {
                locale = Locale.ENGLISH;
            }
            ResourceBundle messages = (ResourceBundle) messageBundles.get(locale.toString());
            if (messages == null) {
                messages = ResourceBundle.getBundle(RESOURCE_PATH, locale);
                messageBundles.put(locale.toString(), messages);
            }
            return messages.getString(key);
        } catch (Exception e) {
            return key;
        }
    }

}

The problem I have is that the getMessage() method always invokes the resource propertie, when should the message:

if (messages == null) {
       messages = ResourceBundle.getBundle(MESSAGE_PATH, locale);
       messageBundles.put(locale.toString(), messages);
}

No enters the conditional, and noticed that comes with name value is resource.

Unlike the method getResource() if I notice that operating normally.

Please I could comment on that problem should, thanks.

Hernando
  • 83
  • 4
  • 15

1 Answers1

0

Looks like the problem is that you store both kinds of bundles in single map. So if resources bundle is used first, it is added to map. The key is current locale (e.g. "en").

Then if you try to retrieve messages bundle with the same locale you in fact get resources bundle instead of messages one because you use the same key.

To solve this problem either use two separate maps or prepend key with bundle identifier like this:

messageBundles.put("resources_" + locale.toString(), messages);

Btw. you use standard Java bundles. More hints on how to use bundles defined in faces-config.xml can be found here: How can I get a message bundle string from inside a managed bean?

Community
  • 1
  • 1
Dawid Pytel
  • 2,750
  • 1
  • 23
  • 30