Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

552 Part II: The Java Library


public class SampleRB extends ListResourceBundle {
protected Object[][] getContents() {
Object[][] resources = new Object[3][2];

resources[0][0] = "title";
resources[0][1] = "My Program";

resources[1][0] = "StopText";
resources[1][1] = "Stop";

resources[2][0] = "StartText";
resources[2][1] = "Start";

return resources;
}
}

The second resource bundle, shown next, is calledSampleRB_de. It contains the
German translation.

import java.util.*;

// German version.
public class SampleRB_de extends ListResourceBundle {
protected Object[][] getContents() {
Object[][] resources = new Object[3][2];

resources[0][0] = "title";
resources[0][1] = "Mein Programm";

resources[1][0] = "StopText";
resources[1][1] = "Anschlag";

resources[2][0] = "StartText";
resources[2][1] = "Anfang";

return resources;
}
}

The following program demonstrates these two resource bundles by displaying the string
associated with each key for both the default (English) version and the German version:

// Demonstrate a resource bundle.
import java.util.*;

class LRBDemo {
public static void main(String args[]) {
// Load the default bundle.
ResourceBundle rd = ResourceBundle.getBundle("SampleRB");

System.out.println("English version: ");
System.out.println("String for Title key : " +
rd.getString("title"));
Free download pdf