JSF: translation of messages in managed bean

In JSF managed beans you can add messages to the context for use in views. For instance when an error occurs you can add a message to be displayed to the user. This is how:

FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("message"));

The null is an identifier you can provide in order to identify an element of the view. I don’t want that kind of information in my managed bean so I left this out. But that isn’t the problem. My problem is that the message wasn’t translated this way. So I figured out how to get to the resourcebundle configured in the faces-context definition:

ApplicationFactory factory = 
(ApplicationFactory) FactoryFinder
.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory..getApplication();
ResouceBundle bundle = ResourceBundle.getBundle(
application.getMessageBundle());

Leave a Reply

Your email address will not be published. Required fields are marked *

Please reload

Please Wait