Introduction to Resource Bundles in ADF applications for centralizing (management of) boilerplate text
Any web application contains boiler plate text: text that is not part of the enterprise data from web services or databases that is manipulated by the end users using the application but that is part of the application definition itself. Text that is shown as prompt, title, message, hint text and in other ways.
Java (Web) applications typically make use of a built in structure for centralizing (and internationalizing) boiler plate text; it is called Resource Bundle. Usually, resource bundles are implemented using property files – one per supported language – that contain key-value pairs; note that the XLIFF file format for resource bundles is gaining ground. The key is referred to in pages, code and wherever a boiler plate text element is required, the value is the language specific and centrally managed translation of the key.
This article will demonstrate how to apply resource bundles in an ADF Faces application. However, the true purpose of this article is to serve as an introduction to a subsequent article that will demonstrate how you can decide to implement your resource bundle using a database table instead of a file. Managing database backed resource bundles can be a lot easier than managing their file based equivalent – through a simple edit page for example, used by an application administrator or business representative. Besides, with resource bundles in the database, changes can be applied without having to go through full redeployment of the application.
In other subsequent articles I will discuss how to support not just different boilerplate text items for different languages but also different sets of boiler plate text for user groups that vary by location, role, department, age or personal preference. We will discuss how to refresh a resource bundle in running application and finally we will see how we can create an in-line resource bundle editing mechanism that allows users to manipulate the boiler plate text in context in a running application.
Steps for adding centralized boilerplate text management to ADF applications using Resource Bundles
1. Open JDeveloper and Create a new Fusion Web application
2. Create a new JSF page and add a few components to the page – say a PanelHeader, an inputText and a button;
note that each of these elements has boiler plate text properties: panel title, input field label (or prompt) and hint text, button label and tooltip. Some of these properties are assigned default values (hard coded) and others can be assigned in line, hard coded. Of course, that is not what we are looking for.
3. Create a new file called ApplicationBundle.properties in an application directory, such as /nl/amis/view/bundles:
4. Edit the new file – add a few entries, like
Note that JDeveloper has built in support for editing resource bundles (either property files like this one or XLIFF style bundles or even Java Classes). From the Application menu,pick the option Edit Resource Bundles:
Browse for the properties file and the editor takes it from there:
5. Nothing at this point connects the page to the bundle – or vice versa. The next step is to make the JSF framework aware that this application uses a resource bundle and that the properties file we just created is its implementation.
Open the faces-config.xml file. Open the Application tab. Add a Resource Bundle. The BaseName is the name of the properties file we just created – including the directory structure expressed as Java package name – but without the extension .properties. In this case: nl.amis.view.bundles.ApplicationBundle. The variable that is specified will be used in EL expression in the pages and code of the application when referring to entries from the resource bundle – and should therefore have a short name.
In the XML source, these lines were added:
6. Replace hard coded boilerplate text in the page with references to entries in the resource bundle, using the variable bnd.
Fortunately, JDeveloper provides code completion help to make defining the bundle references easier:
7. But wait, there is more. JDeveloper has additional support for managing resource bundles. You can configure the ViewController project with one or more resource bundles. We have already seen how JDeveloper can help in editing these bundles. It can also help in finding the resources from those bundles – as well as make it easy to support multiple bundles. Let’s see how this goes:
Open the Project Properties. Select the Resource Bundles category:
here the default bundle configuration is done. We can change the name of the default bundle – to use the one we already created – or we can go to the second tab and add our own bundle:
With this set up, we can edit textual properties from the property palette in a more declarative way, as is done here for the button label:
and the JDeveloper text resource selector appears:
The page is slightly modified (automatically). A variable is added, referring to an ADF managed bean that interprets references we make to resource bundle entries in this page. This is needed to support multiple bundles. Note: we will not be using this JDeveloper/ADF mechanism.
8. Run the page. You will see how the hard coded, default boiler plate values have been replaced at run time by values from the resource bundle.
Download JDeveloper application with the application demonstrated in this article: ADFFacesWithResourceBundle_stepOne_FileBased.
ADF documentation on internationalizing applications using resource bundles: http://docs.oracle.com/cd/E15051_01/web.1111/b31973/af_global.htm.
Programmatically accessing resource bundles, for example from JSF managed beans: http://adfcodebits.blogspot.nl/2010/04/bit-5-accessing-resource-bundle-from.html.
AMIS Blog, Luc Bors on storing modifications on top of resource bundles in database tables: http://technology.amis.nl/2012/07/11/adf-11g-label-modifications-and-persisting-resource-bundle-changes/
Other articles in this series:
- Management of Boilerplate Text in JavaServer Faces Applications
- Personalize Resource Bundles in ADF applications with JHeadstart 10.3.2 run time
- Context Sensitive Resource Bundle entries in JavaServer Faces applications – going beyond plain language, region & variant locales
- ADF Faces 11g – Rich Text Editor: editing WYSIWYG HTML text in JSF Web Applications
- Testing i18n features of JSF applications – Forcing a refresh of the Resource Bundle(s)
- Oracle ADF Virtual Developer Day 2013
- The AMIS Summary of Oracle OpenWorld 2013 is available for download – 60-page white paper
- On the integrity of data in Java applications – presentation from JFall 2013
- OOW13: summarizing one week and 2000 sessions in 3 hours and a bit – the yearly AMIS OOW Review session – 10th October
- Case Study: A Case of Fusion Middleware
- Het Oracle OpenWorld Preview Evenement (5 september 2013) – 15 sprekers & sessies
- ADF 12c: Using WebSockets to implement client to client push (in a scalable manner)
- ADF 12c – The Hierarchical Mystery Tour – Treemap, Sunburst, Treetable, Hierarchy Viewer and Thematic Map
- ADF DVT: Sunburst to quickly inspect the hierarchical composition
- 14 augustus – gratis seminar Oracle ADF 12c, JDeveloper 12c en WebLogic 12c revealed