Friday, June 11, 2010

JSF 2.0 - Automatic resource bundle resolution

User message localization is achived by placing messages in resource bundles (userLoginMessages_en_US.properties for example).

In JSF 1.2, these resource bundles had to be configured in faces-config as follows:


...
    <application>
        <resource-bundle>
            <base-name>test.Messages</base-name> 1 References the file Messages.properties anywhere in the classpath
            <var>msgs</var> 2 Messages in Messages.properties will be referenced in xhtml using "msgs" prefix e.g. #{msgs.userName}
        </resource-bundle>
    </application>
...


Messages in Messages.properties could then be accessed as follows in xhtml pages

<h:outputText
    value="#{msgs.userNameLabel}"/>


In JSF 2.0, the faces-config entry has been made optional. To avoid making the faces-config entry,
1. Rename the properties file to match the xhtml file name. For example, if the xhtml file is named userCreate.xhtml, name the properties file as userCreate.properties.
2. Place the .xhtml and .properties files in the same directory. In our example, place userCreate.xhtml and userCreate.properties in the same directory
3. Change the references in .xhtml to #{cc.resourceBundleMap.<symbol_in_properties_file> where <symbol_in_properties_file> is any symbol in the properties file. For example, if the userCreate.properties has a line as "userNameLabel=User Name" then reference this symbol as follows

<h:outputText
    value="#{cc.resourceBundleMap.userNameLabel}"/>


No comments: