8.1 Resource bundles

 <  Day Day Up  >  

A resource bundle is a simple text file that contains key-value pairs. The key is used by a Java class to retrieve a locale-specific value. To provide support for a new locale, you need only create a new resource bundle with the same key names and translated values.

Example 8-1 demonstrates a base resource bundle. Example 8-2 demonstrates the resource bundle translated for Spanish. Notice the key names do not change, only the value is translated.

Example 8-1. NLSExample.properties resource bundle
 welcome = hello goodbye = goodbye message = This is the NLSExample portlet 
Example 8-2. NLSExample_es.properties resource bundle
 welcome = hola goodbye = adis message = ste es el portlet NLSExample 

The file name of the resource bundle is very important. The file must of type properties . All translated copies of the default resource bundle must include the locale in their title. This is illustrated in Figure 8-1.

Figure 8-1. Translated resource bundles

graphics/08fig01.gif

The name is important because the Portal will locate the appropriate bundle for you based on the locale you provide. You need only provide the base name of the bundle and it will append the appropriate locale. The Group 1 locales are listed in Table 8-1.

Table 8-1. Group 1 languages

Locale Code

Language

ar

Arabic

cs

Czech

da

Danish

de

German

el

Greek

en

English

ru

Russian

sv

Swedish

es

Spanish

tr

Turkish

fi

Finnish

fr

French

zh

Simplified Chinese

zh_TW

Traditional Chinese

hu

Hungarian

it

Italian

iw

Hebrew

ja

Japanese

ko

Korean

nl

Dutch

no

Norwegian

pl

Polish

pt

Portuguese

pt_BR

Brazilian Portuguese

8.1.1 Creating resource bundles in WebSphere Studio

The resource bundles need to be created in the Java Source directory as illustrated in Figure 8-1 on page 250. Though not required, as a matter of good practice, you should place the files in a dedicated directory such as nls. To create a new resource bundle in WebSphere Studio, open the navigator view in the portlet perspective. Locate the Java Source directory of the portlet you are enabling and right-click. From the context menu, select New-> Other .

Figure 8-2. Creating new folder.

graphics/08fig02.jpg

Select Simple in the left panel and Folder in the right panel and click Next .

Figure 8-3. Creating nls folder

graphics/08fig03.jpg

Enter the name of the new folder, typically nls as shown in Figure 8-4.

Figure 8-4. Creating the nls folder in WebSphere Studio Application Developer

graphics/08fig04.jpg

In the nls folder, you need to create the default properties files. Select the nls folder and right-click. From the context menu, select New-> Other-> Simple-> File . Be sure the correct directory is selected and enter the name of the default properties file as illustrated in Figure 8-5. Do not include any language codes in the name, or include any spaces in the name of the resource bundle.

Figure 8-5. Creating the default properties bundle

graphics/08fig05.jpg

When you are done, double-click the properties file in the navigator to open the simple text editor. Using the text editor, define your keys and the default values, such as those shown in Figure 8-1 on page 250. Use CTRL-S to save the file.

Tip

If you create your nls folder directly in the Java Source subdirectory, WebSphere Studio Site Developer deletes this folder when it rebuilds the project.


8.1.2 Translating resource bundles

Once you have defined your default resource bundle with all the keys that will be used by the portlets and JSPs in your application, you must provide translations. It is possible to use the copy functionality in WebSphere Studio. However, there are several reasons you may choose not to. It is a cumbersome process in that simple CTRL-C commands are not recognized when copying whole files. Also, renaming requires a selection from the context menu. While these may seem trivial issues, when creating dozens of resources bundles, they can be frustrating.

Instead, you may find it easier to work directly with the source files on the file system. Locate the directory containing the current workspace. You can obtain this path by right-clicking the portlet application and selecting Properties from the context menu. The Info option will display the file system location of the application. This is illustrated in Figure 8-6.

Figure 8-6. Locating the application on the file system

graphics/08fig06.jpg

Open the directory containing the application and use the normal system copy/paste and rename functionality to create the new resource bundles. Each new bundle should have a unique locale appended. In practice, you may at development time only have the default and English properties files. This same approach can later be used to import translated files received from an outside source.

Once you have created the bundles you want, you need to make them available in the WebSphere Studio environment. To do this, simply select the nls folder, right-click and select Refresh as shown in Figure 8-7 on page 256.

Figure 8-7. Loading resource bundles into WebSphere Studio Site Developer

graphics/08fig07.jpg

When you are done, the folder should appear as in Example 8-1 on page 250, depending of course on the number of languages you choose to support.

8.1.3 Accessing resource bundles in portlets

If you are printing out content directly from the portlet, you can use the portlet API to access the resource bundles quite easily. Most of your development will adhere to a good MVC approach; you can use this approach for setting the title, predefining parameters in a PortletURI or if you are providing some content via the beginPage or endPage methods .

The resource bundle is accessed via the PortletContext object's getText method as displayed in Example 8-3.

Example 8-3. getText API
 PortletContext.getText("  Bundle Base Name", "Key", Locale  ) 
  • Bundle Base Name : the first parameter indicates the base name of the resource bundle. The name includes the path relative to the classes directory as shown in Example 8-4 on page 257. The name does not specify the locale suffix or the properties file type. If the base file name cannot be found, or the key is not present in the properties file, a PortletException will be thrown.

  • Key : this parameter maps to a key value in the properties file. If the key is not found, a PortletException is thrown.

  • Locale : this is used by the Portal to create the complete resource bundle name. You are free to use any locale you like but to ensure the user's locale is returned, the code in Example 8-4 works well. The getLocale method returns the preferred locale for the user. The Portal Server determines the locale by first retrieving the user 's preferred language set during registration. If the preferred language is not set, the locale is retrieved from the accept-language header supplied by the client.

Example 8-4. Accessing resource bundles via the API
 getPortletConfig().getContext().getText("nls.NLSExample", "welcome", request.getLocale()); 

8.1.4 Accessing resource bundles in JSPs

When you employ a well designed MVC approach to your portlet development, the vast majority of NLS enablement work will need to take place in the view space. This section will guide you through providing locale-specific strings in a JSP. Section 8.2, "Translating whole resources" on page 258 will guide you through providing a unique JSP for each locale you choose to support.

To access resource bundles in JSP, you need to include the JSP Standard Tag Library. Right-click in your portlet application, select Properties and then Web . In Available Web project features, check the JSP Standard Tag Library and click OK . Your JSP files can access resource bundles in two ways, as shown in the following examples.

Example 8-5. Accessing resource bundles in a portlet JSP
 <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <fmt:setBundle basename="nls.NLSExample"/> <fmt:message key="message"/> 
Example 8-6. Accessing resource bundles in a portlet JSP
 <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <fmt:bundle basename="nls.NLSExample"> <fmt:message key="message"/> </fmt:bundle> 

As with specifying bundles in portlet code, the bundle name here must include the package name relative to the classes directory.

If the key cannot be located in the properties file, you will see the key written between question marks.

Figure 8-8. Key not found in a properties file

graphics/08fig08.gif

 <  Day Day Up  >  


IBM WebSphere Portal V5 A Guide for Portlet Application Development
IBM Websphere Portal V5: A Guide for Portlet Application Development
ISBN: 0738498513
EAN: 2147483647
Year: 2004
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net