Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the surrounding ??? when message is not found in bundle

In JSF 2.0, if a message is not found in the message bundle, then by default, the key is surrounded with ???. This is a very usable hint during development. However, in my particular case, I really would like that those ??? were not present. I prefer that only the key would be rendered.

E.g. when I do

#{msg.hello}

and the key 'hello' doesn't exist, then the page displays

???hello???

but I would like to show the bare key

hello

The message bundle is loaded in a JSF page as follows:

<f:loadBundle basename="resources.text" var="msg" />

The <f:loadBundle> tag doesn't seem to have an attribute to manipulate the way values are retrieved from that bundle. Should I overwrite some class or how to intercept the way messages are retrieved from the bundle?

I've found a very interesting article on this: Context Sensitive Resource Bundle entries in JavaServer Faces applications – going beyond plain language, region & variant locales. However, in my case, I just want to omit the ???. I think this solution is rather complicated. How can I achieve it anyway?

like image 681
rose Avatar asked Jun 23 '11 08:06

rose


1 Answers

The basename can point to a fullworthy ResourceBundle class. E.g.

<f:loadBundle basename="resources.Text" var="msg" />

with

package resources;

public class Text extends ResourceBundle {

    public Text() {
        setParent(getBundle("resources.text", FacesContext.getCurrentInstance().getViewRoot().getLocale()));
    }

    @Override
    public Enumeration<String> getKeys() {
        return parent.getKeys();
    }

    @Override
    protected Object handleGetObject(String key) {
        return parent.getObject(key);
    }

}

You can overridde the bundle message handling in handleGetObject. JSF by default (by spec) calls getObject(), catches MissingResourceException and returns "???" + key + "???" when caught. You can do it differently.

@Override
protected Object handleGetObject(String key) {
    try {
        return parent.getObject(key);
    } catch (MissingResourceException e) {
        return key;
    }
}
like image 148
BalusC Avatar answered Oct 14 '22 00:10

BalusC