Using MyFaces with Oracle JDeveloper 10g (10.1.3) Preview

Localizing JSF and ADF Faces applications

Written by Jonas Jacobi, Oracle Corporation
August, 2005
Revision 1.0

An article about localizing a JSF and ADF Faces application

This article explains how to internationalize an ADF Faces and JSF application. The article will also make some comparisons with JSF core components and ADF Faces equivalent components regards to what level of support for locales are provided. Oracle JDeveloper 10g (10.1.3) Preview will be used to illustrate and build the actual application. This article is divided into two parts - one that is explaining the core setup to enable locale support with JSF core components, and the second part is covering the same scenario using Oracle ADF Faces components and comparing the two component sets. For more information and articles, please, refer to the JDeveloper page, ADF Faces page, and JSF Resource page on Oracle Technology Network (OTN).

Prerequisites
Making a JSF application localized

  Internationalize your application
  Configure your application to support multiple languages
Localize an application built with ADF Faces

  Localize the page
  Adding ADF Faces components to the page
More information

Prerequisites

In order to follow this HowTo you need the Oracle JDeveloper 10g (10.1.3) Developer Preview, ADF Faces, and access to Internet.

Making a JSF application localized

As many of you are already aware locale means not just a language it can also mean language AND region. In some cases it may by just enough to specify a language e.g. Finish is only spoken in Finland whereas Swedish spoken in Sweden is different than Swedish spoken in Finland. Same scenario applies to US English and British English. There is a difference in the term localize and internationalize. The first term - localize - means that you have prepared your application by extracting labels and text to a resource bundle, which allow labels and text to be determined by the locale. This resource bundle can then be shipped off for translation and used to internationalize your application.

Let's start with building a simple page using components provided by the Sun RI:

  1. First download JDeveloper 10g (10.1.3) Developer Preview if you do not already have installed.
  2. Start JDeveloper 10.1.3 Developer Preview.
  3. Create a new Application by selecting File -> New -> General -> Application. Click OK.
  4. In the Create Application dialog enter 'Localize' as application name. Keep the rest as default and click OK.
  5. In the Create Project dialog enter 'view' as project name.
  6. To add a new JSF JSP page to the project right click on the view project and select New ...
  7. Expand the Web Tier node and select JSF category.
  8. In the right pane select JSF JSP and click OK.
  9. In the Create New Page wizard set the file name to jsf_core and the page type to JSP Document.
    Note: The page type is optional.
  10. Click Finish.

You have now created the page that we are going to use for the JSF core localization. Before we start to localize the page we need to add some controls that can be used for this exercise.

  1. Open the jsf_core.jspx page in the visual editor by double clicking on the node in the project.
  2. In the Component Palette switch to the JSF HTML category.
  3. Drag and drop an outputText component from JSF HTML category onto the page. This is going to be our title.
  4. Set the Value property to Welcome. This will be changed later to use the resource bundle that we are going to create.
  5. Hit Enter key after the outputText field to create a new row in the page.
  6. Drag and drop an outputText component onto the page, and set the Value property to 'label'.
    Note: The For property is used to "wire" the outputLabel component to the "parent" component using the label.
  7. Drag and drop an inputText component onto the page just after the outputText component that is our label.
  8. Drag and drop a commandButton onto the page and drop it after the inputText field.
  9. Set the Value property on the button to 'Press here!'.
  10. For additional chrome on the page set apply a H2 style to the outputText component at the top of the page since it is our title.
  11. In the Component Palette change category to CSS and drag and drop a the JDeveloper stylesheet onto the page.

Your page should look like this:

   

Now it is time to isolate our labels and text messages and extract them and insert them into a resource bundle. A resource bundle can either be a Java file or a .properties file. In this sample we are going to use the .properties approach. As y ou have noted this sample does not contain that many strings to extract, but the format for which we enter our translatable strings is key. The format for which you enter in the property file is key, an equal sign, and the text string representing the label or message. Here is an example:

Key Equal sign Message
myLabel = Welcome Mr/Mrs,
myButton = Press here!

You place the key/value pair in a plain text file with the file extension .properties. It is important that you follow a certain pattern when you name your properties file. If the file will contain your default language you only need to give the .properties file a name e.g. myBundle.properties, but if it is depending on a specific locale you will have to append the _LOCALE to file following the rule <name_of_base_file>_LOCALE.properties e.g. myBundle_de.properties for German, myBundle_sv.properties for Swedish.

Let's create a simple property file in JDeveloper.

  1. Right click on the view project node in the Application Navigator and select New...
  2. Select category General and in the right pane select File and click OK.
  3. Set the name and file extension of the file to - myBundle.properties.


     
  4. Click on Browse to change the directory where we are going to store our resource bundle.
  5. In the Choose Directory dialog select the 'src' folder under the view directory. This is to ensure that the resource bundle is in your class path.
  6. Press the Create New Subdirectory button at the top right and set the name of the new directory to "myLocale".


     
  7. Click OK to close the Create New Directory dialog.
  8. When done creating the 'myLocale' directory make sure that the 'myLocale' directory is selected and press Select to close the Choose Directory dialog.
  9. Click OK to close the Create File dialog.

The file should now show up in JDeveloper, so that you can start editing it. There is only one minor thing we need to do (if you don't see the file and the parent folder in the Application Navigator) and that is to add our base folder for our bundles to the Application Navigator.

  1. Double click on the view project node to open the Project Properties dialog.
  2. In the Project Properties dialog expand the Project Content node and select Resources.
  3. Make sure that the 'Include Subfolders' option is checked for the added directory before clicking OK to close the Project Properties.



     
  4. You should now see your directory structure in the Application Navigator.
  5. If not open in the editor, double click on the myBundle.properties file to open.
  6. In the file add the key/value pairs:

    myTitle = Welcome Mr/Mrs,
    myLabel = Enter your name:
    myButtonText = Press here!

     
  7. Save the file.

Now when we have one properties file containing one locale we can start making our page utilizing this. In JSF there is component called <f:loadBundle> that lets you load a resource bundle into your page and access the keys in this bundle using VBL e.g. #{myBundle.myBundle.myTitle}. The <f:loadBundle> contains a var property that can be used to define a variable or access point to the properties file. You can then use this variable to access your keys and if for some reason the resource bundle changes you only have to change it in one place <f:loadBundle basename="myBundle.myBundle" var="sampleBundle">. Here is an example of how it could be used:

.....
<f:loadBundle basename="myBundle.myBundle" var="sampleBundle">

.....

<f:commandButton value="sampleBundle.myButtonText"/>
<f:outputText value="sampleBundle.myTitle"/>
 

It is important that you add the <f:loadBundle> just below the <f:view> component at the top of your page, otherwise components referring to this bundle will not be able to "see" the var handle.

Let's use the myBundle.properties file in our page.

  1. Open the jsf_core.jspx file in the visual editor
  2. From the Component Palette drag and drop the <f:loadBundle> located in the JSF Core category onto the page.
  3. In the Insert LoadBundle dialog enter 'myLocale.myBundle' for the basename property and 'sampleBundle' for the var property.
  4. Make sure it is at top of the page just underneath the <f:view> component.
    Note: If it is hard to get it in the right place you use the Structure Window to move it.

    Before:


    After:

     
  5. Select the outputText component that represent our title and set the Value property to the following:

    #{sampleBundle.myTitle}
     
  6.  Select the outputText component that represent our label and set the Value property to the following:

    #{sampleBundle.myLabel}
     
  7. Select the commandButton component and set the Value property to the following:

    #{sampleBundle.myButtonText}
     
  8. Save your page. You should now see the resource string applied to the page's components.


     

Internationalize your application

Now when we have localized our application it is much easier to turn our application in to an internationalized application.

Adding another resource file

Let's add another properties file with a translated version of our extensive resource bundle.

  1. Right click on the myLocale package node in the Application Navigator and select New...
  2. Select category General and in the right pane select File and click OK.
  3. Set the name and file extension of the file to - myBundle_se.properties.
  4. Click OK to close the Create File dialog. Your new file should be located in the same folder as the myBundle.properties file.
  5. Enter the following translated keys:

    myTitle = Välkommen Herr/Fru,
    myLabel = Skriv ditt namn:
    myButtonText = Tryck här!

     
  6. Save your work.

Configure your application to support multiple languages

  1. Locate the faces-config.xml file and double click to open in the editor. The file is located under the WEB-INF directory
  2. In the faces-config.xml editor select the Overview tab.


     
  3. In the list to the left select Application. You should now see the following:

  4. In the 'Locale Config' section set the default locale to - en
  5. Click on the New button to add a new locale. In the dialog enter - sv
  6. Save your work.

You can now run your application and the application will pick up the locale from the browser settings. If you want to change the local and see the effect immediately in the browser after a refresh you will have to perform the following configuration steps.

  1. Locate the web application deployment descriptor - WEB.XML.
  2. Right click on the WEB.XML file and select Properties from the popup menu.
  3. Click on the Context Initialization node and click on the Add button.
  4. In the dialog that appear add the following:

    Name = "javax.faces.STATE_SAVING_METHOD"
    Value = "client"


     
  5. Click OK.
  6. Close the Properties dialog by clicking OK.
  7. Run the jsf_core.jspx file.

Localize an application built with ADF Faces

You have now done all your localization and internationalized your application to support both English and Swedish. You now want to use ADF Faces components in your application.

  1. Right click on the View project and select New...
  2. Expand the Web Tier node and select JSF category.
  3. In the right pane select JSF JSP and click OK.
  4. In the Create New Page wizard set the file name to adf_faces and the page type to JSP Document.
  5. Select Next.
  6. In this step add the two ADF Faces libraries to your page - ADF Faces components EAxx and ADF Faces HTML EAxx
  7. Click Finish.

Localize the page

Same procedure as with the jsf_core.jspx page.

  1. From the Component Palette drag and drop the <f:loadBundle> located in the JSF Core category onto the page.
  2. In the Insert LoadBundle dialog enter 'myLocale.myBundle' for the basename property and 'sampleBundle' for the var property.
  3. Make sure it is at top of the page just underneath the <f:view> component.

Adding ADF Faces components to the page

  1. From the Component Palette under the ADF Faces Layout category drag and drop a PanelPage component onto the page.
  2. Set the Title property on this component to - #{sampleBundle.myTitle}
  3. From the Component Palette under the ADF Faces Input category drag and drop a inputText component onto the page. As you can see this component is similar to the core JSF inputText component except that you don't need to attach a label to it, basically it is simpler to use.
  4. Set the Label property on this component to - #{sampleBundle.myLabel}
  5. From the Component Palette under the ADF Faces Navigation category drag and drop a commandButton component onto the page.
  6. Set the Text property on this component to - #{sampleBundle.myButtonText}
  7. Save your work and run your page.

In order to show the full benefit of using ADF Faces components compared to the components provided by the RI you need to add iw to the 'Locale Config' in the faces-config.xml file and when you run the page change your browser language settings to Hebrew. This will show off the built-in support for different locales in ADF Faces (in this case without translated strings to Hebrew). As you will see ADF Faces provides the ability to read and write from right to left, which is not which is not provided by the core components.

More Information

For more information about JSF and Oracle JDeveloper 10g (10.1.3) Developer Preview please visit the Oracle JDeveloper home page at Oracle Technology Network.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy