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.
public class FacesContextProducer {
@Produces @RequestScoped
public FacesContext getFacesContext() {
FacesContext ctx = FacesContext.getCurrentInstance();
if (ctx == null)
throw new ContextNotActiveException("FacesContext is not active");
return ctx;
}
}
Then you need a LocaleProducer, which uses the FacesContextProducer. I also took it from Seam 3 Alpha.
public class FacesLocaleResolver {
@Inject
FacesContext facesContext;
public boolean isActive() {
return (facesContext != null) && (facesContext.getCurrentPhaseId() != null);
}
@Produces @Faces
public Locale getLocale() {
if (facesContext.getViewRoot() != null)
return facesContext.getViewRoot().getLocale();
else
return facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
}
}
Now you have everything to create a ResourceBundleProducer, which can look like this:
public class ResourceBundleProducer {
@Inject
public Locale locale;
@Inject
public FacesContext facesContext;
@Produces
public ResourceBundle getResourceBundle() {
return ResourceBundle.getBundle("/messages", facesContext.getViewRoot().getLocale() );
}
}
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.
@Named
public class MyBean {
@Inject
private transient ResourceBundle bundle;
public void testMethod() {
bundle.getString("SPECIFIC_BUNDLE_KEY");
}
}
@ManagedProperty("#{msgs}") private ResourceBundle msgs;
(with setter). – Vaudois