如何删除周围的???当在捆绑中找不到消息时 [英] How to remove the surrounding ??? when message is not found in bundle

查看:100
本文介绍了如何删除周围的???当在捆绑中找不到消息时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSF 2.0中,如果在消息束中未找到消息,则默认情况下,密钥用???包围.这是开发过程中非常有用的提示.但是,在我的特定情况下,我真的希望那些???不存在.我更喜欢只呈现键.

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.

例如当我这样做

#{msg.hello}

并且键"hello"不存在,则显示页面

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

???你好???

???hello???

但我想显示裸键

你好

消息捆绑包按如下方式加载到JSF页面中:

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

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

<f:loadBundle>标记似乎没有属性来控制从该包中检索值的方式.我应该覆盖某些类还是如何拦截从分发包中检索消息的方式?

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?

推荐答案

basename可以指向完全有价值的

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

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

使用

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

}

您可以在handleGetObject中覆盖捆绑消息处理.默认情况下(按规范),JSF调用getObject(),捕获MissingResourceException,并在捕获时返回"???" + 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;
    }
}

这篇关于如何删除周围的???当在捆绑中找不到消息时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆