Using Locale in Web Experience Factory
I started with a simple provider and consumer application, using a Data Page builder. The result looks like this:
Now to add language-specific labels for Code, Desc, Qty, and Price. WEF uses resource bundles for this. This is nothing more than a property file with name-value pairs. WEF looks in the WebContent/WEB-INF/work/source/resources directory of your project for these files. The resources directory doesn't exist by default, so you have to create one, then create a file in that directory, populate the file, then create a builder which points to it. Here are the steps:
- In Eclipse, navigate to the WebContet/WEB-INF/work/source directory. Right click on source, then choose New -> Folder. Name it "resources".
- Eclipse should create this folder, then move you in the Project Explorer view up to the virtual link to that folder under Java Resources, as in this image:
- Right click on the resources folder. Choose New -> Other. Select File in the dialog box which opens, and click Next. Give the file a name with the extension "properties" and click Finish:
- Enter the values you want to be displayed by default, and save the file.
- Create other files to hold the labels for different languages. The file should have the same name as the first one, but with the country code added before the extension, as in this image:
Here you can see that I added two files, one for Spanish (_es) and one for French (_fr). The builder you are about to create will automatically pick up those files because of the naming convention, and use their values when necessary.
- Create a Localized Resource builder. For the Resource Bundle Name, use the chooser to navigate to the resources directory. Select your default property file, and click OK. Check the box for Create Lookup Table. We'll use that later.
- In the Runtime Localization section of the builder, select Enable Runtime Localization. This will tell the builder to change the labels every time the user changes his or her preferred language. Click OK to save the builder.
- In the Outline, drag this new builder to the top of the list. That will make it available to any builders below it.
- Open the Data Page builder. Go to the bottom of the panel, and open the Label Translation Settings section. For Localized Resource, choose LocaleData, which is the default name given to the builder you just created. Click OK to close the builder. (If you see a warning message for this builder, it might be that you forgot to drag the Localized Resource builder to the top of the builder list.)
Ok, before we test this out, what about dynamically using localized values in places other than column labels? It's easy, because we checked that little box up in step 7 to create a lookup table. WEF will now let us get to the key-value pairs in those property files anywhere we want.
- Create a Text builder. For the text, select a value from the LocaleData variable which WEF created. Then for the lookup table, choose the localedata lookup table also created by WEF. This will dynamically substitute the correct values at runtime.
- Now test out your portlet in WebSphere Portal. Here's how mine looks:
- Now when I change my preferred language to Spanish and reload the page, here's what I see:
As you can see, the Portal tabs also reflect the change in locale; this is done dynamically by the Portal engine, and you are just taking advantage of that in your portlet. (You can also see that I don't know all the Spanish words for these labels, so I just put "SP" in front of the English words. Mea culpa.)
I hope this helps you in your Web Experience Factory development! Here's my example.