DataProvider component for internationalizing Creator applications
By gjmurphy on Aug 23, 2006
The JavaServer Faces standard JSP tag library includes the
loadBundle tag, which allows the developer to bind component values to keys in a Java properties resource bundle. While Java Studio Creator includes this tag on the component palette, there is a lack of good design-time support for binding to keys in the resource bundle, and bound values cannot be displayed at design-time. There have been a lot of complaints in the developer forum about this, and it is something that the Creator development team intends to address in an upcoming release.
In the meantime, I have put together a simple DataProvider component, PropertyResourceProvider, which plays the same role, but has much better design-time support.
How to install the PropertyResourceProvider
Download the PropertyResourceProvider complib to a local directory. From within Creator, open up Tools -> Component Library Manager, and click on "Import". Use the browse feature to locate the downloaded component library, and select it. The library contains a single component, PropertyResourceProvider, which you can import into its own category, or into a pre-existing category.
How to use the PropertyResourceProvider
A PropertyResourceProvider wraps a Java resource bundle file, and makes the keys in the bundle file appear as data fields. In general, you should use one PropertyResourceProvider per resource bundle file. The component has a single property,
baseName, which should contain the qualified base name of the resource bundle file. The property editor for the
baseName property will show a list of all resource bundles available in the current scope, so you can just choose one from the list. If you aren't familiar with Java resource bundles, there is an SDN technical article that provides a good overview.
The overhead involved in creating a PropertyResourceProvider is minimal, so it is probably easiest to add them to each page on which they are needed.
Localizable component values will need to be added to a resource bundle, instead of being set directly on the component. There are a number of ways of adding new keys to a resource bundle file. You can click on the file in the project view to edit it directly (look under the Source Packages node), or you open the file node and right click on a locale node to add key and value pairs, one by one.
To bind a component to a localizable value, right-click on the component and choose "Bind to Data". Select the appropriate PropertyResourceProvider, and the keys currently defined in the resource file will appear as fields. Select a field, and Creator will set the property's value to an expression that binds to the selected field. Close the binding window, and you will see the property value appear in the designer. PropertyResourceProvide is just like any other DataProvider, so you can also edit property values in the component's properites sheet, or by choosing the "Property Bindings..." context menu item.
The PropertyResourceProvider will automatically choose the correct bundle file to load based on the locale of the Faces context for each request. This locale is determined by examining the client locale of the incoming HTTP request, and the list of supported locales given in the application's faces-config file. Typically, adding a locale to a project involves two steps:
- You add a locale-specific version of each bundle file. See the SDN technical article referenced above for more information about naming conventions for bundle files.
- You add the locale to the list of supported locales, in our project's faces-config file. You will find this file in the Files window, under your application's web/WEB-INF directory. For an application that supports two locales, French and Spanish, in addition to the default locale of English, the relevant fragment of the faces-config file would look like this:
<application> <locale-config> <default-locale>en</default-locale> <supported-locale>fr</supported-locale> <supported-locale>es</supported-locale> </locale-config> </application>
Hope this helps!