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

冷暖自知 提交于 2019-11-27 23:07:53

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;
    }
}

You could also create a simple bean that takes care of the string manipulation. This approach is a lot better if you don't need to remove the default surroundings everywhere but only on a specific place(s). The second function is a lot safer to use, since it also takes care of the case where translation starts and ends with the ???.

@ApplicationScoped
@Named
public class LocaleUtils {

    public String getMessage(String s) {
        return clearMessage(s);
    }

    public Object getMessage(ResourceBundle propertyResourceBundle, String key) {
        try {
            return propertyResourceBundle.getObject(key);
        }
        catch (MissingResourceException e) {
            return clearMessage(key);
        }
    }

    private static String clearMessage(String s) {
        String clearMessage = s;
        String prefix = "???", suffix = "???";

        if (s != null && s.startsWith(prefix) && s.endsWith(suffix)) {
            s = s.substring(prefix.length());
            clearMessage = s.substring(0, s.length() - suffix.length());
        }
        return clearMessage;
    }
}

Usage:

<h:outputText value="#{localeUtils.getMessage(msg['hello'])}"/>
<h:outputText value="#{localeUtils.getMessage(msg, 'hello')}"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!