Referencing Resource Bundle from a Page

JSF provides a good way to implement internationalization using Resource Bundle. Resource Bundle is a file that contains the information as key and value pair (In key=value format).

Referencing Resource Bundle from a Page

Referencing Resource Bundle from a Page

        

JSF provides a good way to implement internationalization using Resource Bundle. Resource Bundle is a file that contains the information as key and value pair (In key=value format). You can store label, button text, messages, dates and times, numbers, currencies etc according to the specific locale.

Steps to Create Resource Bundle:

1. Create a file "Messages.properties" under the classes folder of tomcat.

2. Write some key, value pair values in this file.

Messages.properties

 

# Sample ResourceBundle properties file
inputname_header=Roseindia
name_text=Enter Your Name:
greeting_text=Welcome In Roseindia
button_text=Submit


3. Write the a line of code to get the file in the page.

<f:loadBundle basename="roseindia.Messages" var="message"/>

where "basename" attribute is assigned the string value representing the path of bundle file under the classes folder and "var" attribute is assigned a value which will be used further in the whole page to reference the key in the properties file.

For example:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:loadBundle basename="roseindia.Messages" var="message"/>

<f:view>
<html>
<head><title></title></head>

<body>
<h:form>
<h1><h:outputText value="#{message.inputname_header}"/></h1>
<h:outputText value="#{message.name_text}"/>
<h:inputText value="#{ResourceBean.personName}" />
<h:commandButton action="welcome" value="#{message.button_text}" />
</h:form>
</body>
</html>
</f:view>

Download code for all examples