Menu
Topics Index
...
`


More Utility Classes >
Siva Nookala - 06 Oct 2016
Java Util package includes ResourceBundle, ListResourceBundle and PropertyResourceBundle to help the internationalization (i18n) of the applications. Internationalization is the process of giving user understandable text or content. As the products become global, it is necessary that the users from various countries and languages should be able to use them with out having to learn new language.

ResourceBundle which is an abstract class helps in managing a collection of locale-sensitive or language dependent resources. These primarily include strings, for e.g., Google uses internationalization to give its products like google search or gmail to be accessible in most of the popular languages across the world. The underlying product is the same, but how the names for the buttons and how the search results are displayed is dependent upon the users language and the country.

There will be separate resource bundles for supporting different users. For e.g., there will be general resource bundle called SampleRB and there could language specific resource bundle like SampleRB_de or SampleRB_ru for German and Russian respectively. Sometimes we might want to give more specific customization and use the country code along with the langague. So we could have SampleRB_en, SampleRB_en_US, SampleRB_en_IN etc.,.

  • PropertyResourceBundle : This manages the resources using the property files and it adds no methods of its own. An e.g., property file will look like.
# This is the default SampleRB.properties file
s1 = Computer
s2 = Disk
s3 = Monitor
s4 = Keyboard
# This is the SampleRB_de.properties file
s1 = Computer
s2 = Platte
s3 = Monitor
s4 = Tastatur
  • ListResourceBundle : This is an abstract class and it manages the resources using key/value pairs. Any class extending from this should implement the method getContents.
protected abstract Object[][] getContents()

Lets look at example to understand this more.
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.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App