You can specify the strings appearing in a web application using a properties file. The string properties can be accessed in XHTML files using EL bundle properties expressions. This makes it easier to change the strings.
JSF also provides an internationalization mechanism that selects a properties file from a message bundle based on locale preference information sent by a browser. To use this mechanism you must declare the message bundle in a configuration file.
To manage the strings displayed on pages, they can be stored in a properties file. A properties file consists of a line for each string that it defines. Each line has the form
string-name=string-value
The string-value is substituted for string-name wherever it appears in a web page.
A properties file is stored in a Java source package. It must have the .properties extension.
A string value defined in a properties file can use parameterization. This allows the string value to depend in the values of JSF beans.
A string value defined in a properties file can have parameters using
{n}
notation, where n is the
parameter number starting with 0.
In this simple form the parameter supplied by a web page is simply
substituted for the "{n}".
The value of the string can depend on the parameter in more complex ways. If you want to display parameterized text about counts with good grammar you need a string definition like the following.
carCount=You have {0,choice,0#no cars|1#one car|2#{0} cars}.
This leads to the following values for the carCount string.
Parameter 0 Value | carCount Value |
---|---|
0 | You have no cars. |
1 | You have one car. |
2 | You have 2 cars. |
80 | You have 80 cars. |
A message bundle is a group of properties files, all in the same source package. There should be one properties file for each supported locale. They all define the same string names but have different values for different locales.
The JSF framework uses a file naming convention to select the
appropriate properties file.
Each file name has the form
file-base-name_locale-suffix.properties
.
The locale-suffix part is omitted for the default locale.
The bundle path has the form
source-package-name.file-base-name
.
This path is needed in the declaration of the bundle.
You declare a message bundle in a <application>
tag
in the faces-config.xml file.
There, the resource bundle files are specified and the bundle is given a
name for use in the JSF pages.
<application> <locale-config> <default-locale>en</default-locale> <supported-locale>de</supported-locale> </locale-config> <resource-bundle> <base-name>bundle-path</base-name> <var>bundle-name</var> </resource-bundle> </application>
The <locale-config>
tag specifies the default locale
and all other supported locales using the standard two-letter locale
codes.
<default-locale>
subtag)
<supported-locale>
subtags)
The <resource-bundle>
tag specifies
<base-name>
subtag)
<var>
subtag)
A string in a message bundle can be referenced with the notation #{bundle-name.string-name}, where
When a string in a message bundle is parameterized a special tag is used to supply parameter values. This tag can contain references to bean data.
A string access in a web page can supply bean data for parameters using
<f:param>
tags.
This tag is a subtag of a <h:outputFormat>
tag as
shown below.
<h:outputFormat value="#{string-reference}"> <f:param value="#{bean-reference}"/> </h:outputFormat>
The <h:outputFormat>
tag should contain the same
number of <f:param>
tags as the number of parameters
in the value for string-reference.
Use of the <f:param>
tag requires declaration
of the "f:" namespace in the <html>
tag of the XHTML file:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
The numberquiz application can be run here. To manage application strings with internationalization the application
com.corejsf.messages
,
msgs
, and
The com.corejsf.messages
bundle for the numberquiz application
consists of the following two files.
This is the properties file for strings for the the default locale English.
This is the properties file for strings for the German locale. The string names are the same as in messages.properties, but their values are in German.
The locale is indicated by the "_de" in the file name. Each locale has a standard two-letter abbreviation. "de" is the abbreviation for the German locale.