4.5 Internationalization and Localization
4.5.2 Localization
Once an application has been internationalized it can be localized. This section focuses on techniques for delivering localized content to clients. It also reviews techniques for delivering localized content through the use of resource bundles and language-specific JSP files.
4.5.2.1 Delivering Localized Content
Care must be taken to ensure that the application being developed handles data in code sets other than the default ISO 8859-1 (Latin-1). Many Java virtual machines
will support code sets other than English. A detailed listing of character sets sup- ported by Sun’s Java virtual machine can be found at:
http://java.sun.com/products/jdk1.2/docs/guide/ intl/encoding.doc.html
Depending on what content is delivered to the users, localization can be done in a few different ways. Web applications can be designed to deliver localized content based on a user preference or to automatically deliver localized content based on information in the HTTP request.
When an application allows users to select a language, the preferred language can be stored in the session. The selection can occur through a URL selection or a form post that sets an application-level language preference. The posted prefer- ence data can be maintained as part of a user profile as a cookie on the client’s system using a cookie or in a persistent data store on the server. Giving users the ability to select a language ensures that the user gets the content that they expect.
Applications can also automatically deliver localized content by using
Accept-Language attribute in header information of the HTTP request and mapping it to a supported locale. The Accept-Language attribute is set in the user’s Web browser and differs slightly between browsers. When using automatic application-level locale selection, it is prudent to also provide a mechanism to let the user override the automatic selection and select a preferred language. Auto- matic locale selection also depends on application support for different locales. Care needs to be taken to ensure that unsupported languages are handled properly. 4.5.2.2 Localized Messages
The Java programming language provides facilities for localization. This section discusses methods of providing localized data in a Web application.
In some cases an application may need to support multiple languages on the same JSP page. List resource bundles are also useful when using servlets. Code Example 4.5 shows how to deliver content from a user-specified locale using a
ListResourceBundle.
public class WebMessages extends java.util.ListResourceBundle{ public Object [][] getContents(){
return contents; }
INTERNATIONALIZATION AND LOCALIZATION 93
//Messages
{"com.sunw.messages.welcome",
"Welcome to Java(TM) Pet Store Demo"}, {"com.sunw.messages.any_message",
"Untranslated message},
{"com.sunw.messages.come_back_soon", "Come Back Soon"} }
}
Code Example 4.5 English Resource Bundle
In this example, localized content for messages in each supported language is contained in separate files. Code Example 4.6 demonstrates a similar resource bundle file that contains Japanese messages.
public class WebMessages_ja extends java.util.ListResourceBundle{ public Object [][] getContents(){
return contents; }
static final Object[][] contents = { //Messages
{"com.sunw.messages.welcome",
"Japanese welcome Java(TM) Pet Store Demo"}, {"com.sunw.messages.come_back_soon",
"Japanese Come Back Soon"} }
}
Code Example 4.6 Japanese Resource Bundle
Inside a servlet or JSP page, the messages contained in a resource bundle can be obtained with the code shown in Code Example 4.7.
// set the user's desired locale
session.setValue("preferredLocale", Locale.JAPAN); // load preferred locale
ResourceBundle messages = ResourceBundle.getResource("WebMessages", (Locale)session.getValue("preferredLocale");
Code Example 4.7 Getting Messages From a Resource Bundle
Note that the Japanese resource bundle’s class file name ends with “_ja”. When loading resources, the Japanese version of the resource bundle file will be loaded if Locale.JAPAN is specified in the request or the default application is running in a Japanese locale. Also note that this file contains only the messages that you want to appear in translation. All messages not defined in this file will be used from the default file, which has no extension following its name.
This example shows how to specify and store a user’s preferred target lan- guage and load messages for that language. Once the resource bundle is loaded a message can be obtained by using the command:
messages.getString("com.sunw.messages.welcome");
In this example, messages refers to the name of the resource bundle and
welcomerefers to the message that you would like to load. You need to ensure that thecontentTypeof the page is set to an encoding that supports multiple languages (the next section provides details on setting thecontentType). UTF-8 encoding allows you to display multiple languages on a single Web page. Moreover, UTF-8 encoding is supported by the most commonly used Web browsers.
It may be useful to create a JavaBeans component to assist in loading and managing the messages for an application to save resources. The details of how to create this type of component aren’t covered in this document.
Resource bundles are useful for providing localized content as long as the logic for displaying internationalized text is not going to be greatly changed by the target locale. If the logic changes, it is recommended to use separate JSP files for the content, as described in the following section.
Localized Content in JSP Pages
Where you need to provide messages that vary depending on the target locale, or where the content and display logic are drastically different, it is better to use a com- pletely different JSP file.
Since JSP pages are responsible for the presentation of a Web application’s user interface, they provide an ideal place to put locale-specific information. It is
INTERNATIONALIZATION AND LOCALIZATION 95
important that the JSP pages and the supporting JavaBeans components and tag libraries be able to deal with localized content. This section discusses how to design a localized page and how to integrate this page into a Web application.
The encoding of a JSP page must be specified in order for the Web container to process it. An Application Component Provider sets the encoding of a JSP page using thecontentTypeattribute of thepagedirective. This attribute sets the encod- ing for both the JSP page and the subsequent output stream. The value of con- tentType should be “TYPE” or “TYPE;charset=CHARSET” followed by a “;” and a valid IANA registry value. The default value for TYPEis text/html; the default value for the character encoding isISO-8859-1. The IANA registry values can be found at:
ftp://venera.isi.edu/in-notes/iana/assignments/character-sets
If you are using thecontentType attribute of thepagedirective, the resulting output stream should not be a problem; otherwise, you will need to ensure the output stream is set properly. Keep in mind that when using thepagedirective you can only set the content type once, because apagedirective is set at page compile time. If it is necessary to change the content type dynamically, you can do so with a servlet.
When using servlets it is important to set the response encoding correctly. The
ServletResponseinterface contains asetLocalemethod which should be used to ensure that data is set to the proper locale. The Servlet specification indicates that the locale should be set before calling the getWriter method. For more details, refer to the Servlet specification.
To prepare an application for localization, you should follow these steps: 1. Separate the display logic from the content in the presentation layer (JSP) of
the Web application. This makes localizing content easier and prevents inte- gration errors which could occur if portions of the display logic were localized by accident.
The J2EE programming model recommends that you deliver locale-spe- cific files that follow the naming convention used by resource bundles. This naming convention is the base file name followed by an underscore (_) and the language variant. A country and a variant can also be used:
a. Language
b. Language and country
jsp + _ + language + _ + country
c. Language with country and a variant
jsp + _ + language + _ + country + _ + variant
2. Ensure that the character encoding of the localized files is supported by the Java virtural machine of the system running the Web application. Also, be sure that the correct encoding is listed in thecontentTypetag included in thepage
directive of the JSP page.
A properly internationalized application can be quickly localized for any number of languages without any modifications to the code of the Web applica- tion. It is much easier to internationalize an application the beginning of a devel- opment cycle when application design is first specified.