I have a properties file for localization:
foo=Bar
title=Widget Application
This is tied in as a resource-bundle in the faces-config:
<resource-bundle>
<base-name>com.example.messages.messages</base-name>
<var>msgs</var>
</resource-bundle>
I can access this just fine in the facelets view using EL:
<title>#{msgs.title}</title>
However, if there are things like SQLExceptions, I need to be able to write messages from the managed bean. This is all working also:
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "There was an error saving this widget.", null);
FacesContext.getCurrentInstance().addMessage(null, message);
Here is the issue: I want to have those messages come from the properties file so that they, too, can be changed based on the locale. Is there an easy way to access the properties file using injection?
I asked a quite related question on SO:
How to inject a non-serializable class (like java.util.ResourceBundle) with Weld
And inside the Seam Forum:
http://seamframework.org/Community/HowToCreateAnInjectableResourcebundleWithWeld
To summarize:
I realized an injectable ResourceBundle with 3 Producers.
First you need a FacesContextProducer. I took the one from the Seam 3 Alpha sources.
Then you need a LocaleProducer, which uses the FacesContextProducer. I also took it from Seam 3 Alpha.
Now you have everything to create a ResourceBundleProducer, which can look like this:
Now you can @Inject the ResourceBundle into your beans. Pay attention that it has to be injected into a transient attribute, otherwise you’ll get an exception complaining that ResourceBundle is not serializable.