JSF Message Localization

At a recent Sun Tech Days conference in Kuala Lumpur, Malaysia, a respected developer said, "Localization is to difficult." I responded, "It is easy, once you know how."

Below are instructions to create a simple localized JSF NetBeans project.
Click here, to download my sample localized JSF NetBeans project.
Click here, to download a more comprehensive localized JSF NetBeans project.
jsfLocale2 moves the message language choice from a JSP into a servlet which makes the choice of language more flexible in the application. jsfLocale2 also uses a JSF backing bean to save and print the logged in user name.


Here is how to localize application messages in JSF:

0. Setup a development environment.
Click here for instruction to setup a NetBeans development environment.

1. Create a NetBeans project named jsfLocale1.
In the web.xml file, update the welcome-file value to make the index.jsp be processed through the JSF control servlet:

	<welcome-file>
            faces/index.jsp
        </welcome-file>

2. Create the default language message property file:
Right mouse button click on
Projects tab/jsfLocale1/Source Packages/<default package>.
Select New/"File/Folder"/Other/Properties File, click Next.
Give the File Name: messages. Click Finish.

3. Create the other language message files, one for each languages your web site is to have. In this example,
3.1 Add German: Right mouse button click on
Projects tab/jsfLocale/Source Packages/<default package>/messages.properties.
Select Add Locale, select de_DE - German / Germany
3.2 Add Chinese: Right mouse button click on
Projects tab/jsfLocale/Source Packages/<default package>/messages.properties.
Select Add Locale, select zh_CN - Chinese / China

4. In the JSF configuration file, faces-config.xml, add the language codes:

...<faces-config ... >
    <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>de_DE</supported-locale>
            <supported-locale>zh_CN</supported-locale>
        </locale-config>
    </application>
</faces-config>

5. Enter all the application messages into each of the message files.
Click here to download the sample NetBeans project which contains the message files.

Note, Chinese Characters need to be converted into unicode.
Example: from 中文, to \\u4e2d\\u6587
Sample Windows conversion command:
native2ascii.exe -encoding UTF-8 messages_zh_CN.properties.txt messages_zh_CN.properties
\*\*\* Note, due to a bug, remove the unicode character "\\ufeff", from the first line of the file messages_zh_CN.properties.

6. Run the project. Make changes, re-run.
When making JSP changes, re-load the web page to see the changes.
When making configuration changes, example face-config.xml changes, re-run the project.

Comments:

[Trackback] Courtesy of Stacy Thurston, community advocate for Sun Learning Services.

Posted by Marina Sum's Blog on March 28, 2007 at 03:24 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tech event, training, business, in east and south Asia.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today