This section provides a tutorial example on how to use the java.util.ResourceBundle.getBundle() method to retrieve resource bundle objects by resource bundle names.
In previous sections, we have created 3 resource bundles:
ErrorMessage - Created as subclasses of the java.util.ResourceBundle class in 3 locales: default, fr, and fr_CA.
WarningMessage - Created as subclasses of the java.util.ListResourceBundle class in 3 locales: default, fr, and fr_CA.
NoteMessage - Created as properties files for the java.util.PropertyResourceBundle class in 3 locales: default, fr, and fr_CA.
Now let's write a simple test program to use all 3 resource bundles in all 3 locales:
Here is a sample program that shows how to request a resource bundle and
lookup the bundle object of the a given key and a given locale:
/**
* ResourceBundleTest.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class ResourceBundleTest {
public static void main(String[] a) {
bundleClassTest();
listBundleClassTest();
propertyBundleFileTest();
}
public static void bundleClassTest() {
ResourceBundle rb;
Locale l;
System.out.println("ResourceBundle Test:");
System.out.println("The default resource:");
rb = ResourceBundle.getBundle("ErrorMessage");
System.out.println(" 1:" + rb.getString("NO_FILE"));
System.out.println(" 2:" + rb.getString("FILE_EMPTY"));
l = Locale.FRENCH;
System.out.println("The French resource:");
rb = ResourceBundle.getBundle("ErrorMessage",l);
System.out.println(" 1:" + rb.getString("NO_FILE"));
System.out.println(" 2:" + rb.getString("FILE_EMPTY"));
l = new Locale("fr","CA");
System.out.println("The French (Cananda) resource:");
rb = ResourceBundle.getBundle("ErrorMessage",l);
System.out.println(" 1:" + rb.getString("NO_FILE"));
System.out.println(" 2:" + rb.getString("FILE_EMPTY"));
}
public static void listBundleClassTest() {
ResourceBundle rb;
Locale l;
System.out.println("ListResourceBundle Test:");
System.out.println("The default resource:");
rb = ResourceBundle.getBundle("WarningMessage");
System.out.println(" 1:" + rb.getString("LAST_MINUTE"));
System.out.println(" 2:" + rb.getString("HIGH_LOAD"));
l = Locale.FRENCH;
System.out.println("The French resource:");
rb = ResourceBundle.getBundle("WarningMessage",l);
System.out.println(" 1:" + rb.getString("LAST_MINUTE"));
System.out.println(" 2:" + rb.getString("HIGH_LOAD"));
l = new Locale("fr","CA");
System.out.println("The French (Cananda) resource:");
rb = ResourceBundle.getBundle("WarningMessage",l);
System.out.println(" 1:" + rb.getString("LAST_MINUTE"));
System.out.println(" 2:" + rb.getString("HIGH_LOAD"));
}
public static void propertyBundleFileTest() {
ResourceBundle rb;
Locale l;
System.out.println("PropertyResourceBundle Test:");
System.out.println("The default resource:");
rb = ResourceBundle.getBundle("NoteMessage");
System.out.println(" 1:" + rb.getString("STARTING_SYSTEM"));
System.out.println(" 2:" + rb.getString("LOADING_SETTINGS"));
l = Locale.FRENCH;
System.out.println("The French resource:");
rb = ResourceBundle.getBundle("NoteMessage",l);
System.out.println(" 1:" + rb.getString("STARTING_SYSTEM"));
System.out.println(" 2:" + rb.getString("LOADING_SETTINGS"));
l = new Locale("fr","CA");
System.out.println("The French (Cananda) resource:");
rb = ResourceBundle.getBundle("NoteMessage",l);
System.out.println(" 1:" + rb.getString("STARTING_SYSTEM"));
System.out.println(" 2:" + rb.getString("LOADING_SETTINGS"));
}
}
Output:
ResourceBundle Test:
The default resource:
1:File doesn't exist.
2:File is empty.
The French resource:
1:Document n'exist pas.
2:Rien.
The French (Cananda) resource:
1:Dossier n'exist pas.
2:Rien.
ListResourceBundle Test:
The default resource:
1:Bus is leaving within one minute.
2:System load is very high.
The French resource:
1:L'autobus part d'une minute.
2:La charge de système est très haute.
The French (Cananda) resource:
1:L'autobus part d'une minute.
2:L'utilisation de système est très haute.
PropertyResourceBundle Test:
The default resource:
1:Starting the system...
2:Loading user's settings...
The French resource:
1:Commencer le système...
2:Les arrangements de l'utilisateur de chargement...
The French (Cananda) resource:
1:Commencer le système...
2:Commencer à charger les arrangements de l'utilisateur...
Note that:
All the bundle classes must be compiled. You may need the "-classpath ."
option to help the "javac" command to locate the depending classes to compile
a multi classes program.
You may need the "-cp ." option to help the "java" command to locate the
depending classes and the bundle property files.
Comparing the 3 different ways of internationalize text messages, the property
file is the best: