The first step toward supporting multiple languages is to tell your JSF application which locales it should support. You specify the supported locales with the <locale-config> element in a Faces configuration file, under the <application> node:
<application>The letters “en” and “es” stand for English and Spanish, respectively. So this tells our application to support both English and Spanish, and to use English as the default.
<locale-config>
<default-locale>en</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>es</supported-locale>
</locale-config>
<message-bundle>CustomMessages</message-bundle>
</application>
Creating resource bundles:
Once you’ve configured your JSF application to support different locales, you have to place locale-specific versions of your application’s text strings in resource bundles. Resource bundles aren’t a JSF feature—they’re a fundamental part of the way Java handles internationalization and localization.
LocalizaitonResources_en.properties:
numberOfVisits=You have visited us {0} time(s), {1}. Rock on!
toggleLocale=Translate to Spanish
helloImage=../images/hello.gif
After you have created a localized properties filelanguage), you must convert it to use Unicode characters. This can be done with the native2ascii tool included with the Java Development Kit (JDK).
Using resource bundles with components:
Using a resource bundle in a JSF application is as simple as using a value-binding expression. All you have to do is load the proper resource bundle and make it accessible in an application scope. For JSP, there’s a JSF core tag, <f:loadBundle>, that loads the bundle and stores it in the request scope automatically.Here’s how
you use it:
<f:loadBundle basename="LocalizationResources" var="bundle"/>The basename attribute specifies the name of the resource bundle, prefixed by its location in your classpath. Because our bundles were placed in WEB-INF/classes, no prefix is needed. However, if we had nested them deeper, like in the WEB-INF/classes/org/jia directory, the basename would be "org.jia.LocalizationResources".
We can access value of the key numberOfVisits, with the value-binding expression "#{bundle.halloween}".