Search code examples
jsfmessage-bundle

Messagebundle show Keys instead of value


For testing / debugging purpose, I want to show the keys from the message bundle instead of the values in my jsf application. Is that possible?

Example: My messages_en.properties has following entry:

global_today=today   

example jsf page

<h:outputLabel id="myId" value="{Msgs['global_today']}"/>   

Now I want to see the key in the page, "global_today" not today.


Solution

  • Simplest way is to rename the properties file, that way JSF wont find any key and you instead of 'today' you will see ???global_today???

    If you still want to see the global_today you can do the following:

    Lets say you have the following in your faces-config.xml

    <resource-bundle>
        <base-name>my.package.resources.MyText</base-name>
    </resource-bundle>
    

    Rename the MyText into MyTextExtender

    Then in add MyTextExtender.java in the my.package.resources package

    with the following content:

    public class MyTextExtender extends ResourceBundle {
    
        public MyTextExtender() {
            setParent(getBundle("my.package.resources.MyText", FacesContext.getCurrentInstance()
                    .getViewRoot().getLocale()));
        }
    
        @Override
        public Enumeration<String> getKeys() {
            return parent.getKeys();
        }
    
        @Override
        protected Object handleGetObject(String key) {
            return key;
            //The code below will try to turn the annoying ???some_key??? 
            //into "some key" (looks better)
            /*try {
                return parent.getObject(key);
            } catch (MissingResourceException e) {
                if (!StringUtils.isEmpty(key)) {
                    logger.error("Missing key: " + key + " in the properties", e);
                    return key.replace("_", " ");
                } else {
                    logger.error("Key was null???", e);
                    return "";
                }
            }*/
        }
    }