Referencing a Localized Message

Customizing your application to support a specific
location is known as Localization. If your application supports locale of “fr_FR”,
then it can present the application for French speaking users (in France). In
the same way, “en_US” locale presents an application for English speaking
users (in US).
If you want your application to work in different locale like English,
French, German etc. then you would need to replace the output according to the
locale. For this you can create many properties files for different
locales. A properties file is used to hold locale specific information.
This file 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.
For example, If you have the properties file for
the default locale, for example, Messages.properties, then the properties
file for the french locale can be created in the following format:
Messages_fr_FR.properties (In
the DefaultPropertiesFileName_language_country.properties
format)
|
Messages_fr_FR.properties:
# Sample ResourceBundle properties file
inputname_header=Roseindia
name_text=Entrez votre nom:
greeting_text=Bienvenue dans Roseindia
button_text=Envoyer |
Now this file can be used when the locale is set for
France. Edit the faces-config.xml file. resource-bundle element is used to
provide the value to the base name and var. Here "basename" is assigned the
value representing the path of bundle file under the classes folder and
"var" is assigned a value which will be used further in the jsp pages to
reference the key in the properties file.
faces-config.xml
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr_FR</supported-locale>
</locale-config>
<resource-bundle>
<base-name>roseindia.Messages</base-name>
<var>message</var>
</resource-bundle>
</application> |
Now, when the locale is set for France then locale
specific information is gathered from the Messages_fr_FR.properties
file. For example,
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%
javax.faces.context.FacesContext ctx =javax.faces.context.FacesContext.getCurrentInstance( );
ctx.getViewRoot( ).setLocale(new java.util.Locale("fr", "FR"));
%>
<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

|