120 IBM WebSphere Everyplace Access V5, Volume II: Application Development
4.6.4 Accessing NLS bundles in Java portlets
In this section, you will update the ActionEventPortlet.java file to display NLS
content based on the locale value (English, Spanish or Brazilian Portuguese).
You also need to add the new key-value pairs in the associated property file.
1. Open the file ActionEventPortlet.java for editing by double-clicking it. Next,
you will update the code to display the color in the preferred language
selected by the user. The resource bundle is accessed via the PortletContext
object’s getText() method. This method receives three parameters:
a. Base name of the resource bundle, including the path relative to the
classes directory and without the locale suffix or the properties file type.
b. Key specified in the properties file.
c. Locale.
2. Make the following highlighted updates in the actionPerformed() method.
Example 4-14 ActionEventPortlet.java
......
import java.io.IOException;
import java.util.Locale;
......
public void actionPerformed(ActionEvent event) throws PortletException {
if( getPortletLog().isDebugEnabled() )
getPortletLog().debug("ActionListener - actionPerformed called");
// ActionEvent handler
String actionString = event.getActionString();
//get the preferred locale for the user
Locale loc = event.getRequest().getLocale();
// Add action string handler here
if (actionString.equalsIgnoreCase(ACTION_RED)) {
//access the resource bundle via the PortletContext object's getText
method
String red = getPortletConfig().getContext().getText("nls.NLSLab",
"redColor", loc);
//create the string of HTML to be rendered
String value = "Action <FONT color=\"#ff0000\">" + red + "</FONT>";
//create a portlet request
PortletRequest request = event.getRequest();
........................
Chapter 4. National Language Support (NLS) 121
if (actionString.equalsIgnoreCase(ACTION_BLUE)) {
//access the resource bundle via the PortletContext object's getText
method
String blue = getPortletConfig().getContext().getText("nls.NLSLab",
"blueColor", loc);
//create the string of HTML to be rendered
String value = "Action <FONT color=\"#0000ff\">" + blue + "</FONT>";
//create a portlet request
PortletRequest request = event.getRequest();
.......
3. Open the resource bundles located in the Java Source/nls folder and make
sure the following key-value pairs required for this scenario have been
included in the properties files.
Example 4-15 NLSLab.properties (default)
redColor=RED
blueColor=BLUE
Example 4-16 NLSLab_en.properties (English)
redColor=RED
blueColor=BLUE
Example 4-17 NLSLab_es.properties (Spanish)
redColor=ROJO
blueColor=AZUL
Example 4-18 NLSLab_pt_BR.properties (Brazilian Portuguese)
redColor=VERMELHA
blueColor=AZUL
4. Select File -> Save All to save all your changes to the project.
5. Close the browser.
6. Click Run on Server to test your changes.
7. Click Edit my profile to change the preferred languages and execute the
application again to check that portlets display the word BLUE or RED in the
language you have selected.
Note: For simplicity, not all text in this sample scenario has been enabled for
NLS.
122 IBM WebSphere Everyplace Access V5, Volume II: Application Development
4.6.5 Adding PDA markup support
In this section, you will update the sample scenario to add PDA markup support.
Follow these steps:
1. Create new folders with the name pda to store the JSP files for PDA browsers.
Figure 4-36 New pda folders
2. In the PDA directories, include the corresponding JSP files. You can create
new files or copy from the html directories. Don’t forget to include access to
NLS bundles as shown in 4.6.2, “Accessing NLS bundles from JSPs” on
page 110.
Chapter 4. National Language Support (NLS) 123
Figure 4-37 New jsp files
3. Update the portlet.xml file to indicate it supports PDA markup. For each
portlet, add a markup name of pda to the support tags in the portlet.xml file.
Example 4-19 portlet.xml file
......
<portlet-app-name>ActionEvent application</portlet-app-name>
<portlet id="actionevent.ActionEventPortlet"
href="WEB-INF/web.xml#actionevent.ActionEventPortlet" major-version="1"
minor-version="0">
<portlet-name>ActionEvent portlet</portlet-name>
<cache>
<expires>0</expires>
<shared>no</shared>
</cache>
<allows>
<maximized />
<minimized />
</allows>
<supports>
<markup name="pda">
<view />
<edit />
</markup>
<markup name="html">
<view />
<edit />
</markup>
124 IBM WebSphere Everyplace Access V5, Volume II: Application Development
</supports>
</portlet>
<portlet id="messagereceiver.MessageReceiver"
href="WEB-INF/web.xml#messagereceiver.MessageReceiver" major-version="1"
minor-version="0">
<portlet-name>MessageReceiver portlet</portlet-name>
<cache>
<expires>0</expires>
<shared>no</shared>
</cache>
<allows>
<maximized />
<minimized />
</allows>
<supports>
<markup name="pda">
<view />
</markup>
<markup name="html">
<view />
</markup>
</supports>
</portlet>
</portlet-app>
......
4. Select File -> Save All to save all your changes to the project.
5. Run the NLS scenario.
Figure 4-38 Running sample scenario in PDA
Note: For more information about adding PDA markup support, refer to the
WebSphere Everyplace Access InfoCenter documentation.

Get IBM WebSphere Everyplace Access V5 Handbook for Developers and Administrators Volume II: Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.