Read resource bundle properties in a managed bean

后端 未结 3 413
时光取名叫无心
时光取名叫无心 2020-11-28 09:57

Using files I\'m able to have i18n text in my JSF pages.

But is it possible to access these same properties in my managed bean

3条回答
  •  我在风中等你
    2020-11-28 10:49

    Assuming that you've configured it as follows:

    
        com.example.i18n.text
        text
    
    

    If your bean is request scoped, you can just inject the as @ManagedProperty by its :

    @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() {
        // ... 
    }
    

    If your bean is however in a broader scope, then evaluate #{text} programmatically in method local scope:

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

    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");
        // ... 
    }
    

    Or if you're managing beans by CDI instead of JSF, then you can create a @Producer for that:

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

    And inject it as below:

    @Inject
    private PropertyResourceBundle text;
    

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

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

提交回复
热议问题