8.6 Sample scenario: NLS bundles

 < Day Day Up > 

In this sample scenario, NLS bundles will be created to support multiple languages. Once you have done this using WebSphere Studio, the JSP that delivers markup content in View mode for portlet MessageReceiver will be enhanced to access the NLS bundles.

The Portlet Messaging application sample scenario from Chapter 7, "Portlet messaging" on page 225 will be used as a base application to add NLS support. The scenario is illustrated in Figure 8-17 on page 267.

Figure 8-17. National Language Support (NLS) scenario

graphics/08fig17.gif

The resource bundle is accessed via the PortletContext object's getText method and you will need to provide the following:

  • Bundle Base Name: the first parameter indicates the base name of the resource bundle. The name includes the path relative to the classes directory. 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 is thrown.

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

In addition, the locale is used by the Portal to select the proper language bundle. However, you cannot set this value when invoking NLS bundles from JSPs.

8.6.1 NLS bundles

In this section, you will use the sample scenario from Chapter 7, "Portlet messaging" on page 225. The portlet application will be enhanced to support NLS. Follow these steps:

  1. If needed, start the IBM WebSphere Studio Site Developer. Click Start-> Programs-> IBM WebSphere Studio-> Site Developer 5.0 .

  2. You will create a new folder with the name nls to store the resource bundles. The following resource bundles will be imported into this folder:

    - NLSLab.properties (default)

    - NLSLab_en.properties (English)

    - NLSLab_es.properties (Spanish)

    - NLSLab_pt_BR.properties (Brazilian Portuguese)

  3. Select your ActionEvent/Java Source folder.

    Figure 8-18. Select Java source to create an nls folder

    graphics/08fig18.jpg

  4. Select File-> New-> Folder .

    Figure 8-19. Select a new folder

    graphics/08fig19.gif

  5. Enter nls for the Folder name field then click Finish .

    Figure 8-20. Enter a name for the new folder (nls)

    graphics/08fig20.jpg

  6. Your directory structure should now look as shown in Figure 8-21.

    Figure 8-21. New nls folder

    graphics/08fig21.jpg

  7. Select the new ActionEvent/Java Source/nls folder.

  8. Click File-> Import .

  9. Select File System and click Next . Browse to C:\LabFiles\NLSLab\Bundles.

    Note : The sample scenario included in this chapter requires that you download the sample code available as additional materials. See Appendix C, "Additional material" on page 543.

  10. Select all four properties files and click Finish to import.

    Figure 8-22. Importing the bundles

    graphics/08fig22.jpg

  11. View the files in the nls folder by double-clicking them. Notice how they are structured.

    Example 8-9. NLSLab.properties (default)
     readystatus=Ready to receive message receivedstatus=Received a message viewmode=Operating in View mode redColor=RED blueColor=BLUE 
    Example 8-10. NLSLab_en.properties (English)
     readystatus=Ready to receive message receivedstatus=Received a message viewmode=Operating in View mode redColor=RED blueColor=BLUE 
    Example 8-11. NLSLab_es.properties (Spanish)
     readystatus=Listo para recibir mensaje receivedstatus=Mensaje recibido viewmode=Operando en modo de visualizacion redColor=ROJO blueColor=AZUL 
    Example 8-12. NLSLab_pt_BR.properties (Brazilian Portuguese)
     readystatus=Pronto para receber mensagem receivedstatus=Mensagem recebida viewmode=Operando em modo de Visualizao redColor=VERMELHA blueColor=AZUL 

8.6.2 Accessing NLS bundles from JSPs

In this section, you will update the JSP MessageReceiverView.jsp to display NLS content based on the locale value (English, Spanish or Brazilian Portuguese). In general, modifications to the JSPs are necessary to allow them to display language-specific content. Follow these steps:

  1. First you have to include the JSP standard tag library in your project as follows :

    1. Right-click the ActionEvent project.

    2. Select Properties .

    3. Select the Web option from the list and in the available Web project features.

    4. Check the Include the JSP Standard Tag library box.

    5. Click OK .

    Figure 8-23. Include JSP Standard Tag Library

    graphics/08fig23.jpg

  2. Open the MessageReceiverView.jsp file in WebSphere Studio Site Developer. This file is located in the /Web Content/messagereceiver/jsp/html/ directory.

    Figure 8-24. MessageReceiverView.jsp

    graphics/08fig24.jpg

  3. In this JSP, you have the following text with static information:

    - Ready to receive message

    - Received a message

  4. Add logic to display messages in the proper language. Updates to this JSP are highlighted in bold in Example 8-13.

    Example 8-13. MessageReceiverView.jsp supporting NLS with bundles
     <%@ page contentType="text/html" import="java.util.*, messagereceiver.*"%> <%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %>  <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>  <portletAPI:init/> <DIV style="margin: 6px"> <H3 style="margin-bottom: 3px">Welcome!</H3> This is a sample <B>view mode</B> page. You have to edit this page to customize it for your own use.<BR> The source file for this page is "/Web Content/messagereceiver/jsp/html/MessageReceiverView.jsp".  <fmt:setBundle basename="nls.NLSLab"/>  <br> <% if (request.getAttribute("MyMessage") == null) { %>    <B>  <fmt:message key="readystatus"/> ...  </B> <% } else { %>    <B>  <fmt:message key="receivedstatus"/>  :</B>    <B><%= request.getAttribute("MyMessage") %></B> <% } %> </DIV> 
  5. Select File-> Save All to save all your changes to the project.

8.6.3 Running the NLS scenario

In this section, you will run the portlet application messaging scenario now enabled for NLS.

  1. Stop the Test Environment server so that next time, the new properties files will be used.

  2. Right-click ActionEvent and select Run on Server .

    Figure 8-25. Running the NLS project

    graphics/08fig25.gif

  3. The message receiver portlet will now display its markup using NLS.

    Figure 8-26. ActionEvent Portlet with no preferred language selected

    graphics/08fig26.gif

  4. Select a new locale value by clicking the Edit my profile icon to select a preferred language, as shown in Figure 8-27.

    Figure 8-27. Edit my profile icon

    graphics/08fig27.gif

    For example, select Brazilian Portuguese as the preferred language for the wpsadmin user (default user for portlet development environment). It may be necessary to enter a first and last name before you can continue. Enter wps for both if this happens.

    Figure 8-28. User profile

    graphics/08fig28.jpg

  5. Click Continue . You will need to click Continue again to confirm your change and view the information displayed in the MessageReceiver portlet in Brazilian Portuguese.

    Note : You should also notice that when the language locale changes, the text of the WebSphere Portal menu at the top of the page also changes.

    Figure 8-29. ActionEvent Portlet in View mode after action selected

    graphics/08fig29.jpg

  6. Edit the user profile again and try Spanish as the new locale.

    Figure 8-30. Selecting Spanish

    graphics/08fig30.jpg

    The content you specified will display in Spanish.

    Figure 8-31. Content in Spanish

    graphics/08fig31.jpg

  7. Edit the user profile again and try French (Frances in Spanish) as the new locale.

    Figure 8-32. Selecting French as the language

    graphics/08fig32.jpg

  8. Since French has not been enabled, Portal will use the default bundle. Your message will display in English (as is specified in the default bundle), but the WebSphere Portal menu at the top will display in French.

    Figure 8-33. In French

    graphics/08fig33.jpg

  9. To change your language back to English before you exit, click Edit my profile and select English (Anglais in French) as your language.

    Figure 8-34. Back to English

    graphics/08fig34.jpg

8.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:

    1. Base name of the resource bundle, including the path relative to the classes directory and without the locale suffix or the properties file type.

    2. Key specified in the properties file.

    3. Locale.

  2. Make the following highlighted updates in the actionPerformed() method.

    Example 8-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();          ........................       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 8-15. NLSLab.properties (default)
     redColor=RED blueColor=BLUE 
    Example 8-16. NLSLab_en.properties (English)
     redColor=RED blueColor=BLUE 
    Example 8-17. NLSLab_es.properties (Spanish)
     redColor=ROJO blueColor=AZUL 
    Example 8-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.

 < 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