When to Use Which Multilanguage Feature

 

Introduction

 

WPS provides a number of ways where you can implement Multilanguage support. In fact, there are such a number of them that it is confusing for the inexperienced user. This tip lists down the steps to take according to your Multilanguage requirements.

When you need to support any format of file in multilanguages

Use the portlet context’s getResourceAsStream(). This method lets you open the file for the particular markup and human language.

Steps

  1. Package your multi-language files in the par in the following structure:

      eg

PORTLET-INF

            |- Your regular portlet-inf stuffs……

            |- YourResources

                  |- html

                        |- data.file

                        |- en

                              |- data.file

                              |- uk

                                    |- data.file

                              |- us

                                    |- data.file

                        |- ja

                              |- data.file

                        |- zh

                              |- data.file

                  |- wml

                        |- same structure as html

           

  1. In your portlet code, call the portlet context’s getResouceAsStream() method as follows:

eg

InputStream dataIOS =

               getPortletConfig().getContext().getResourceAsStream(“/PORTLET-INF/YourResources/data.file”,htmlClient,chineseLocale);

 

            The getResourceAsStream() method will know how to search your directory structure for the data.file that matches the markup, locale and variant. See the javadocs of the PortletContext class for the details.

 

When you need to display just text in different languages in portlet code

Use portlet context’s getText() method. This method lets you display a text from a file. The file must be in name-value pairs ie a Properties formatted file.

Steps

  1. Package and name your multi-language files as follows in the par structure:

PORTLET-INF

            |- Your regular portlet-inf stuffs……

            |- classes

                  |- nls

                        |- data.properties

                        |- data_en.properties

                        |- data_ja.properties

                        |- data_zh.properties

    1. The folder structure must be under the /PORTLET-INF/classes directory.
    2. Name your files in the form of data_nl.properties, where nl is the supported language.
    3. The contents of the properties file must be:

key = value

format, where key must appear in all the properties files, and value is in the corresponding language.

 

  1. In your portlet code, access your text as follows:

 

String localizedText = getConfig().getContext().getText(“nls.data”, welcomeHeader, getLocale());

 

The first argument to the getText() method is the dot delimited String of the directory structure and filename. The second argument is the key which you want to extract the value, and the third argument is the Locale object.

 

Note: In the stock sample that comes with WPS, the sample code uses the java.util.ResourceBundle class for retrieving Multilanguage text. It is recommended that you use the method described here rather than that of the stock sample. The reason is the same as described in Step 5 of tip: How to access file in portlets. There is no way you can par your ML files such that the Portlet Installer can expand it to the correct WPS directory.

 

When you need to display just text in different languages in your JSPs

Steps

  1. Package your Multilanguage properties file as in the case for display ML text in java code.
  2. In your JSP, display your text as follows:

eg

<P>portletAPI:text key="welcomeHeader" bundle="nls.data"/></P>

 

When you need to display Portal wide Multilanguage text in portlet code and/or JSPs

Portal wide Multilanguage properties files can be found in the following directory:

<wps_root>\app\web\WEB-INF\classes. You can access the values in these properties file from your portlets and JSPs

Steps

  1. In your portlet code/JSP, include the following code:

eg

java.util.ResourceBundle resourceBundle = com.ibm.wps.puma.ResourceBundleLoader.getBundle("nls.registration",

request.getLocale());

            where registration is one of the ML properties file found under <wps_root>/app/web/WEB-INF/class/nls

 

You can then access the ML value by:

eg

String namePrompt = resourceBundle.getString("givenName");

 

Or in your JSP:

<p><%= resourceBundle.getString("givenName") %>

<INPUT>……

 

When you need to display multilingual JSPs

There are cases when it might just be more convenient for you to have multilingual JSPs, rather than using one JSPs together with many different resource files. A good example would be a JSP containing help instructions.

Steps

  1. Package your JSPs in the par file as follows:

eg

PORTLET-INF

   |- your usual portlet-inf stuffs

   |- helpjsps

         |-html

               |- help.jsp

               |- en

                     |- uk

                           |- help.jsp

                     |- us

                           |- help.jsp

               |- zh

                     |- help.jsp

         |- wml

               |- follows same structure

  1. Display your JSP file in your portlet code as follows:

eg

portletcontext.include("/PORTLET-INF/helpjsps/help.jsp", portletrequest, portletresponse);

 

WPS will be smart enough to pick up the correct ML file for display based on the user’s preferred language and browser type.

 

Hosted by www.Geocities.ws

1