Searching in Resource Bundles – Localization
By Gigi Rosen / July 24, 2021 / No Comments / Compact Number Parsing, Oracle Certifications, The ZonedDateTime Class
Searching in Resource Bundles
Example 18.4 is instructive in understanding which resource bundles for a given locale will be searched when performing key-based lookups for locale-specific values. Each resource bundle in the resource family MyResources has only one key, and the name of the key is different in all resource bundles. The value of the key in each file is the name of the resource bundle. The program prints the value of all the keys found in the resource bundles for a given locale. The method keySet(), called at (1), returns a set with all the keys from the resource bundles that can be searched for the locale specified in the getBundle() method. Any value written out will be the name of the resource bundle that is searched. Since a key set is returned, the key order in the set is not guaranteed. However, this key set is converted to a sorted key set to reflect the hierarchy of resource bundles for each locale.
From the program output, we can see that the default resource bundle is always searched (for all the locales in Example 18.4). Only if no corresponding resource bundle for the given locale can be found in the resource bundle family will the resource bundle for the current default locale be searched (“no_NO” locale in Example 18.4). For a language-country locale, both the country and the language resource bundles are included, if any of them are in the resource bundle family (the “fr_CA” locale in Example 18.4). For a language locale, only the language resource bundle is included, if it is in the resource bundle family (“fr” locale in Example 18.4).
Example 18.4 Locating Resource Bundles
# MyResources_fr_CA.properties
file1 = MyResources_fr_CA
# MyResources_fr.properties
file2 = MyResources_fr
# MyResources_en_US.properties
file3 = MyResources_en_US
# MyResources_en.properties
file4 = MyResources_en
# MyResources.properties
file5 = MyResources
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.TreeSet;
public class LocatingBundles {
public static void main(String[] args) {
Locale[] locales = {
new Locale(“no”, “NO”), // Norway
Locale.CANADA_FRENCH, // Canada (French)
Locale.FRENCH, // French
Locale.getDefault(), // Default: en_US
};
for (Locale locale: locales) {
System.out.println(“Locating resource bundles for ” + locale + ” locale:”);
ResourceBundle resources = ResourceBundle.getBundle(“resources.MyResources”,
locale);
for (String key : new TreeSet<>(resources.keySet())) { // (1)
System.out.println(resources.getString(key));
}
System.out.println();
}
}
}
Output from the program:
Locating resource bundles for no_NO locale:
MyResources_en_US
MyResources_en
MyResources
Locating resource bundles for fr_CA locale:
MyResources_fr_CA
MyResources_fr
MyResources
Locating resource bundles for fr locale:
MyResources_fr
MyResources
Locating resource bundles for en_US locale:
MyResources_en_US
MyResources_en
MyResources