faces-config.xml
:
<faces-config> <application> <locale-config> <default-locale>en_US</default-locale> <supported-locale>zh_CN</supported-locale> </locale-config> ... </application> ... </faces-config>
en_US
, and it also supports
zh_CN
.
faces-config.xml
, there could be more than one
<supported-locale>
elements, but there can only be one
<default-locale>
element.
ResourceBundle
, which is backed up by a set of properties files.
ResourceBundle
available at application startup time:
faces-config.xml
:
<application> <message-bundle>messages</message-bundle> ... </application>
ResourceBundle
containing the translated messages.
faces-config.xml
should now resemble this:
<faces-config> <application> <locale-config> <default-locale>en_US</default-locale> <supported-locale>zh_CN</supported-locale> </locale-config> <message-bundle>messages</message-bundle> </application> ... </faces-config>
message.properties
file under the Java™
src
directory in Eclipse™ dynamic
web project based on the above configuration. This
message.properties
file is used as the default message bundle if no locale is
matched with requested locales.
message.properties
files. For example, put
messages_en_US.properties
and
messages_zh_CN.properties
also under the Java
src
directory. These files are copied to your
classes
directory after compilation. The project hierarchy is shown in
the following figure:
key=value
format. For example, in the
messages.properties
file:
zoomInteractorLink=Adding a zoom interactor overviewLink=Adding an overview ...
messages_zh_CN.properties
), you need to convert the content of the file into the escape
format through the tool
native2ascii
provided in the JDK. This tool converts a file written in your
native encoding to one that represents non-ASCII characters as
Unicode escape sequences. Detailed usage of this tool is
described here
for Java 5 or here
for Java 6.
native2ascii -encoding GBK messages_zh.properties > messages_zh_escaped.properties
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
ResourceBundle
into the web pages. As a convenience, JSF provides a tag that
loads the
ResourceBundle
into a map, then is stored in the requested scope. For
example:
<f:loadBundle basename="messages" var="msg" />
#{msg.key}
to retrieve the corresponding value from the properties file.
Make sure that your
ResourceBundle
actually contains the messages with the key for the locales
that you specify with these tags. The page hierarchy should
now resemble this:
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <f:view> <f:loadBundle basename="messages" var="msg" /> <html> <head> <title><h:outputText value="#{msg.title}" /></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> ... </head> <body> ... </body> </html> </f:view>
ResourceBundle
. You can reference the documentation here The
Java EE 5 Tutorial. See the section "Setting the Resource
Bundle" in chapter "15. Internationalizing and Localizing web
Applications", which introduces two ways of retrieving the
bundle: using the
java.util.ResourceBundle.getBundle()
API or configuring element in the application configuration
file.
ResourceBundle
, which does not require the user to hard code resource bundle
paths,and is also a convenient way to retrieve different
locales when necessary. For more information, see Retrieve the ResourceBundle
in JViews web applications
faces-config.xml
. If there is no match with a locale-specific message property
file, the default
messages.properties
are used.