In this recipe we will access message resource keys from a Java class. This provides much more control over the rendered keys.
We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
Our little secret will be a helper class that will get a message string from a message resource bundle for an indicated locale. Practically, there are two methods:
getClassLoader
: This returns the class loader for the current thread or the class loader of a default objectgetLocaleString
: This returns the message key for the corresponding localeNow, our helper class is as shown next:
public class LocaleHelper { protected static ClassLoader getClassLoader(Object defaultObject) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = defaultObject.getClass().getClassLoader(); } return loader; } public static String getLocaleString( String bundle, String key, Object parameters[], Locale locale) { String message = null; ResourceBundle resourceBundle = ResourceBundle.getBundle(bundle, locale, getClassLoader(parameters)); try { message = resourceBundle.getString(key); } catch (MissingResourceException e) { message = "ERROR MESSAGE!"; } if (parameters != null) { StringBuffer stringBuffer=new StringBuffer(); MessageFormat messageFormat = new MessageFormat(message, locale); message = messageFormat.format(parameters, stringBuffer, null).toString(); } return message; } }
Next, we write a bean class that will show you how to use the previous helper class. The bean class will have getter and setter methods for two properties, user first name and age, and two methods that will provide the messages USER_AGE
and USER_NAME
, rendered to the user depending on the locale:
@ManagedBean @SessionScoped public class UserBean { private int userAge; private String firstName; public int getUserAge(){ return this.userAge; } public void setUserAge(int userAge){ this.userAge=userAge; } public String getFirstName(){ return this.firstName; } public void setFirstName(String firstName){ this.firstName=firstName; } public String getUserAgeInsert() { FacesContext context = FacesContext.getCurrentInstance(); //get default locale Locale myLoc = context.getViewRoot().getLocale(); //manually set a Locale for English //Locale myLoc=new Locale("en"); //manually set a Locale for French //Locale myLoc=new Locale("fr"); String message = LocaleHelper.getLocaleString( context.getApplication().getMessageBundle(), "USER_AGE", null, myLoc); return message; } public String getUserNameInsert() { FacesContext context = FacesContext.getCurrentInstance(); //get default locale Locale myLoc = context.getViewRoot().getLocale(); //manually set a Locale for English //Locale myLoc=new Locale("en"); //manually set a Locale for French //Locale myLoc=new Locale("fr"); String message = LocaleHelper.getLocaleString( context.getApplication().getMessageBundle(), "USER_NAME", null, myLoc); return message; } }
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>Access message resource keys from a Java class</title> </h:head> <f:view locale="en"> <f:loadBundle basename="users.MyMessages" var="msg"/> <h:form id="UserForm"> <h:outputText value="#{userBean.userAgeInsert}"/><br /> <h:inputText id="userAgeID" required="true" value="#{userBean.userAge}"> <f:validateLongRange minimum="18" maximum="50"/> </h:inputText> <h:message showSummary="true" showDetail="false" for="userAgeID" style="color: red; text-decoration:overline"/> <br /> <h:outputText value="#{userBean.userNameInsert}"/><br /> <h:inputText id="userNameID" required="true" value="#{userBean.firstName}"> <f:validateLength minimum="5" maximum="25" /> </h:inputText> <h:message showSummary="true" showDetail="false" for="userNameID" style="color: red; text-decoration:overline"/> <br /> <h:commandButton id="submit" action="response?faces- redirect=true" value="Submit"/> </h:form> </f:view> </html>