Constructing the Parent Chain of a Result Resource Bundle

Once a result resource bundle is identified (see Step 2 and Step 3 in the previous section), a parent chain for the result source bundle is constructed and returned by the getBundle() method as follows:

  • All candidate bundle names that are below the result bundle name in the candidate bundle list are iterated over, and those candidate bundles that can be instantiated are chained in the order in which they appear in the list.

In our example, the result resource bundle BasicResources_fr was found last in the list of candidate bundle names, and therefore does not get any parent according to this step.

  • Lastly, an attempt is also made to chain the default resource bundle (with the base name) into the parent chain of the result resource bundle which is then returned by the getBundle(baseName, specifiedLocale) method.

In our example, the default resource bundle BasicResources is provided and is therefore set as the parent bundle to the result resource bundle Basic-Resources_fr.

Click here to view code image

BasicResources_fr.properties
BasicResources.properties
(parent)

This will result in the parent chain being searched, if necessary, when a lookup is performed for a resource in this result bundle for the “fr_CA” locale, where the most specific bundle (BasicResources_fr.properties) is researched first and the most general one (BasicResources.properties) last.

Note that an attempt is also made to include the default resource bundle when no result resource bundle can be found in the candidate bundle list, as shown in Step 4 earlier.

Best practice recommends including a default resource bundle as fallback in a resource bundle family. This bundle will then always be searched last to find the value associated with a key.

Table 18.5 shows the result resource bundles with the parent bundle chain from the resource bundle family BasicResources that will be located and loaded for each locale supported by Example 18.3. We have shown property resource file names in the table and indicated the parent resource bundle. Actually, resource bundle classes are instantiated at runtime, based on the contents of the property resource files. Note that the resource bundle BasicResources_fr.properties is searched for both the “fr_FR” locale and the “fr_CA” locale.

Table 18.5 Result and Parent Bundles (See Example 18.3)

Specified localeResult resource bundlesStep that creates the parent chain:
Locale(“en”, “US”)BasicResources.propertiesStep 4
Locale(“no”, “NO”)BasicResources_no_NO.properties BasicResources.properties (parent)Step 2
Locale(“fr”, “FR”)BasicResources_fr.properties BasicResources.properties (parent)Step 2
Locale(“fr”, “CA”)BasicResources_fr.properties BasicResources.properties (parent)Step 2

Leave a Reply

Your email address will not be published. Required fields are marked *