Resource bundling in JSF

Resource bundling in JSF

Resource bundling in JavaServer Faces (JSF) refers to the technique of managing and organizing localized messages and resources used in a web application. It allows developers to centralize and internationalize strings, labels, and other static content, making the application adaptable to different languages and locales. Here’s an explanation followed by a Java example:

Resource bundling in JSF

Explanation

  • 1.  Localization Support:
    • JSF resource bundling supports internationalization (i18n) by allowing developers to store locale-specific content in properties files.
    • Each properties file contains key-value pairs where keys are used in the application code, and values are the corresponding localized messages or text.
  • 2.  Managed by Locale:
    • JSF automatically selects the appropriate properties file based on the user’s locale settings or the application’s configuration.
    • This ensures that users see content in their preferred language without modifying the application code.
  • 3.  Usage in JSF Components:
    • Resource bundles are commonly used with JSF components, such as <h:outputText> or <h:outputLabel>, to display text or messages retrieved from properties files dynamically.

Java Example

Here’s an example demonstrating resource bundling in JSF using properties files:

messages.properties (Default Locale)
properties
welcome.message = Welcome to our application!
button.label = Click Me


  messages_fr.properties (French Locale)

properties
welcome.message = Bienvenue dans notre application!
button.label = Cliquez ici

WelcomeBean.java
java
package com.example.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class WelcomeBean {

    public String getWelcomeMessage() {
        ResourceBundle bundle = ResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale());
        return bundle.getString("welcome.message");
    }

    public String getButtonLabel() {
        ResourceBundle bundle = ResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale());
        return bundle.getString("button.label");
    }
}

welcome.xhtml
xml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Welcome Page</title>
</h:head>
<h:body>
    <h:form>
        <h:outputText value="{welcomeBean.getWelcomeMessage()}" />
        <br/>
        <h:commandButton value="{welcomeBean.getButtonLabel()}" action="{welcomeBean.doSomething()}" />
    </h:form>
</h:body>
</html>

Explanation of Example

  • Properties Files (messages.properties and messages_fr.properties):  These files contain key-value pairs (key = value) where key is a reference used in the application code (like welcome.message or button.label) and value is the corresponding message or text in the specified locale.
  • WelcomeBean.java:  This managed bean retrieves localized messages using ResourceBundle. It fetches messages based on the current locale (FacesContext.getCurrentInstance().getViewRoot().getLocale()). The getWelcomeMessage() and getButtonLabel() methods demonstrate fetching localized messages dynamically.
  • welcome.xhtml:  This JSF page (welcome.xhtml) uses <h:outputText> and <h:commandButton> components to display localized messages retrieved from WelcomeBean. The value attribute of <h:outputText> and value attribute of <h:commandButton> use EL expressions ({welcomeBean.getWelcomeMessage()} and {welcomeBean.getButtonLabel()}) to fetch and display dynamic content.

By leveraging resource bundling in JSF, developers can create applications that are easily adaptable to different languages and regions without changing the application logic. It promotes maintainability and enhances user experience by presenting content in the user’s preferred language.