Menu
Topics Index
...
`

List Resource Bundle Demo - Java Example Program


List Resource Bundle Demo
import java.util.*;

class ListResourceBundleDemo
{
    public static void main(String arg[])
    {
        /* NOTE: THIS PROGRAM DOES NOT WORK ONLINE. PLEASE DOWNLOAD THIS AND MOVE THE CLASSES ButtonNames AND ButtonNames_de TO THE APPROPRIATE FILES AS MENTIONED BELOW. */
        
        ResourceBundle defaultNames = ResourceBundle.getBundle("ButtonNames");
        
        System.out.println("Default/English version :");
        System.out.println("String for title Key : " + defaultNames.getString("title"));
        System.out.println("String for start Key : " + defaultNames.getString("start"));
        System.out.println("String for stop Key : " + defaultNames.getString("stop"));
        System.out.println();
        
        ResourceBundle germanNames = ResourceBundle.getBundle("ButtonNames", Locale.GERMAN);
        
        System.out.println("German version :");
        System.out.println("String for title Key : " + germanNames.getString("title"));
        System.out.println("String for start Key : " + germanNames.getString("start"));
        System.out.println("String for stop Key : " + germanNames.getString("stop"));    
    }
}

// Should be placed in a separate file ButtonNames.java
import java.util.*;
public class ButtonNames extends ListResourceBundle
{
    protected Object[][] getContents()
    {
        Object[][] resources = new Object[3][2];

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

        resources[1][0] = "stop";
        resources[1][1] = "Please Stop";

        resources[2][0] = "start";
        resources[2][1] = "Go Ahead";

        return resources;
    }
}

// Should be placed in a separate file ButtonNames_de.java
import java.util.*;
class ButtonNames_de extends ListResourceBundle
{
    protected Object[][] getContents()
    {
        Object[][] resources = new Object[3][2];

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

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

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

        return resources;
    }
}
OUTPUT

Default/English version :
String for title Key : My Program
String for start Key : Go Ahead
String for stop Key : Please Stop

German version :
String for title Key : Mein Program
String for start Key : Anfang
String for stop Key : Anschlag

DESCRIPTION

As shown above we created a default/English resource bundle called ButtonNames and a German resource bundle called ButtonNames_de. When loaded according to the Locale, the corresponding resource bundle is returned.

THINGS TO TRY
  • Try creating a new resource bundle for French and see if the appropriate text is returned.

© meritcampus 2019

All Rights Reserved.

Open In App