17.10 Implementing internationalization

In this section, you will use the internationalization feature to create JSF portlets to run in multiple languages. You will include the text constants and error messages into a properties file used by the JSF components and the business logic.

However, in order to simply illustrate the concept, you will only implement a few constants. Since you already have a properties file created by the portlet project wizard, you will use this same file to include the key-value pairs for this internationalization scenario.

Execute the following steps:

1. Open the calculator.nl.CalculatorPortletResource.properties file. Include these lines at the end of the file, then save and close it:
page_comment=* Created using Application Developer *

text_operation=Calculator operation:

text_calculate=Perform Calculation
Note: CalculatorPortletResource.properties file is the name of the default file (no locale suffix). If a locale is not supported by the portlet or the specific properties file is not found, the user gets values from this properties file. For example, if you want to create specific values for English, you will need to create a file with name CalculatorPortletResource_en.properties.

2. Create a properties file for Brazilian Portuguese.

a. Right-click CalculatorPortletResource.properties.

b. Select Copy.

c. Right-click the calculator.nl package.

d. Select Paste.

e. Enter CalculatorPortletResource_pt_BR.properties as the new name for the file. This is the suffix for brazilian Portuguese.

f. Click OK.

g. Change the three last keys, to use Brazilian Portuguese text:
page_comment=* Criado com Application Developer *

text_operation=Operação:

text_calculate=Efetuar Cálculo

h. Save the file and close the editor.

3. Edit the Portlet deployment descriptor.

a. Double-click Portlet Deployment Descriptor.

b. In the Portlet deployment descriptor window, click Portlets | Calculator.

c. Under the supported locales section, click Add. See Figure 17-41.

Figure 17-41 The supported locales section

d. Enter pt_BR in the Locale list.

e. Click OK.

f. Save the file and close the editor.

4. You also need to specify the supported languages in the faces-config.xml file.

a. Open the faces-config.xml file.

b. Within the <application> tag, include the following tags:

<locale-config>

<supported-locale>en</supported-locale>

<supported-locale>pt_BR</supported-locale>

</locale-config>

c. Save the file and close it.

5. In the calculate.jsp Source code, after the jsf/core tag library, enter:
<f:loadBundle var="constants" 
basename="calculator.nl.CalculatorPortletResource"/>

This tag provides access to the text constants for the selected language.

6. In the Design view, select the comment field (Created using Application Developer). In the Properties view, change the value attribute to:
#{constants.page_comment}

7. Similarly change the Operation: text to:
#{constants.text_operation}

8. Change the Calculate button label in the Display options tab to:
#{constants.text_calculate}

9. The page should look like Figure 17-42.

Figure 17-42 Page with components changed to run in multiple languages

10. Run the project again and change your preferred language in the profile to Brazilian Portuguese. Use Edit my profile option and enter the required information (password is wpsadmin).

11. You will have to run the project again so that the JSF framework accepts the change.

12. Notice that the text has changed to the values contained in the resource file.

13. Now try to specify a value for number1 that is out of the permitted range (11-888). You will notice that the error message is also translated to Brazilian Portuguese, although you did not have to be concerned about (Figure 17-43).

Figure 17-43 The calculator internationalized to Brazilian Portuguese


Redbooks
ibm.com/redbooks