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

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

问题描述

使用 文件,我可以在 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?

推荐答案

假设你已经配置如下:

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

如果您的 bean 是请求范围的,您只需将 作为 @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 实用程序库 OmniFacesMessages 类,那么您只需设置一次解析器即可让所有 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天全站免登陆