读取托管bean中的资源包属性 [英] Read resource bundle properties in a managed bean

查看:74
本文介绍了读取托管bean中的资源包属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用<resource-bundle>文件,我可以在JSF页面中使用i18n文本.

Using <resource-bundle> files I'm able to have i18n text in my JSF pages.

但是是否可以在托管bean中访问这些相同的属性,以便我可以使用i18n值设置面孔消息?

But is it possible to access these same properties in my managed bean so I can set faces messages with i18n values?

推荐答案

假定您已按照以下方式进行配置:

Assuming that you've configured it as follows:

<resource-bundle>
    <base-name>com.example.i18n.text</base-name>
    <var>text</var>
</resource-bundle>

如果您的bean是请求范围的,则可以通过<var><resource-bundle>注入为@ManagedProperty:

If your bean is request scoped, you can just inject the <resource-bundle> as @ManagedProperty by its <var>:

@ManagedProperty("#{text}")
private ResourceBundle text;

public void someAction() {
    String someKey = text.getString("some.key");
    // ... 
}

或者如果您只需要一些特定的密钥:

Or if you just need some specific key:

@ManagedProperty("#{text['some.key']}")
private String someKey;

public void someAction() {
    // ... 
}


但是,如果您的bean的范围更广,则可以在本地方法范围内以编程方式评估#{text}:

public void someAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    ResourceBundle text = context.getApplication().evaluateExpressionGet(context, "#{text}", ResourceBundle.class);
    String someKey = text.getString("some.key");
    // ... 
}

或者如果您只需要一些特定的密钥:

Or if you only need some specific key:

public void someAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    String someKey = context.getApplication().evaluateExpressionGet(context, "#{text['some.key']}", String.class);
    // ... 
}


您甚至可以通过标准ResourceBundle API来获得它,就像JSF本身已经在幕后所做的一样,您只需要在代码中重复基本名称:


You can even just get it by the standard ResourceBundle API the same way as JSF itself is already doing under the covers, you'd only need to repeat the base name in code:

public void someAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    ResourceBundle text = ResourceBundle.getBundle("com.example.i18n.text", context.getViewRoot().getLocale());
    String someKey = text.getString("some.key");
    // ... 
}


或者,如果要通过CDI而不是JSF管理bean,则可以为此创建一个@Producer:

public class BundleProducer {

    @Produces
    public PropertyResourceBundle getBundle() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getApplication().evaluateExpressionGet(context, "#{text}", PropertyResourceBundle.class);
    }

}

并按如下所示注入它:

@Inject
private PropertyResourceBundle text;


或者,如果您使用的是JSF实用程序库 OmniFaces Messages类,则只需设置它的解析器一次,以允许所有Message方法利用该捆绑软件.


Alternatively, if you're using the Messages class of the JSF utility library OmniFaces, then you can just set its resolver once to let all Message methods utilize the bundle.

Messages.setResolver(new Messages.Resolver() {
    public String getMessage(String message, Object... params) {
        ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", Faces.getLocale());
        if (bundle.containsKey(message)) {
            message = bundle.getString(message);
        }
        return MessageFormat.format(message, params);
    }
});

另请参见 javadoc 中的示例, 展示页面.

See also the example in the javadoc and the showcase page.

这篇关于读取托管bean中的资源包属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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