0
votes

How can I get a properties file from CDI bean, I mean properties file for internationalization purposes as mentioned here.

In @ManagedBean everything is simple @ManagedProperty(name="....") but I can't encounter the way to achieve the same in CDI bean.

Thank you very much.

2

2 Answers

1
votes

As far as I know, CDI doesn't support the kind of field-level access that @ManagedProperty gives (where you can have @ManagedProperty(name="#{msgs.title}")). If you want that level of control in CDI, you'll have to write a CDI Producer.

Considering that the resource bundle is simply a class of ResourceBundle, you could easily obtain your defined bundle with:

    FacesContext ctxt = FacesContext.getCurrentInstance();
    ResourceBundle bundle = ctxt.getApplication().getResourceBundle(ctxt, aValue);
    bundle.get("title");

Alternatively, you could simply inject either your FacesContext or Application into your bean:

    @Inject
    Application theApplication


    public void getBundle{
        ResourceBundle bundle = theApplication.getResourceBundle(ctxt, aValue);

    }
1
votes

If you're looking for internationalization support within CDI for JSF purposes, you may want to look at DeltaSpike's JSF module. It builds off of the core i18n support available in core.