Home  >  Core Java

Java ListResourceBundle

By Arvind Rai, July 15, 2019
Java ListResourceBundle is an abstract class of ResourceBundle that manages resources for a locale. Resources are created as a Java class. To create resource classes, we need to create subclasses of ListResourceBundle for every required locale and override getContents() method. To get a ResourceBundle instance, we need to call ResourceBundle.getBundle method and pass resource base name and locale. To fetch the value for a key from resources, we need to call getObject method on the instance of ResourceBundle. Here we will provide complete example to use ListResourceBundle in our application.

Create ListResourceBundle Subclasses

To create resource classes by extending ListResourceBundle, we need to follow a pattern. The resource class name will be the combination of base name and locale.
Suppose our resource base name is MyDataBundle.
1. For Locale("en", "US") the resource class name will be MyDataBundle_en_US.class.
2. For Locale("de", "DE") the resource class name will be MyDataBundle_de_DE.class.
3. For Locale("fr", "FR") the resource class name will be MyDataBundle_fr_FR.class.

The resource classes need to override getContents() method of ListResourceBundle. The getContents() method returns two-dimensional Object array that represents key-value pair. The first element of each pair is the key that should be a String and the second element is the value associated with that key. The value associated with the key is not necessary to be String but it can be any type of object.
Now find our all resource classes.
MyDataBundle_en_US.java
package com.concretepage.lsbundles;
import java.util.ListResourceBundle;
import com.concretepage.President;
public class MyDataBundle_en_US extends ListResourceBundle  {
  @Override
  protected Object[][] getContents() {
	return contents;
  }
  private Object[][] contents = {
      { "Country", "United States" },
      { "President", new President("Donald Trump", 73) },
      { "Literacy", Double.valueOf(99.20) },
  };  
} 
MyDataBundle_de_DE.java
package com.concretepage.lsbundles;
import java.util.ListResourceBundle;
import com.concretepage.President;
public class MyDataBundle_de_DE extends ListResourceBundle {
  @Override
  protected Object[][] getContents() {
	return contents;
  }
  private Object[][] contents = {
      { "Country", "Germany" },
      { "President", new President("Frank-Walter", 63) },
      { "Literacy", Double.valueOf(98.40) },
  };  
} 
MyDataBundle_fr_FR.java
package com.concretepage.lsbundles;
import java.util.ListResourceBundle;
import com.concretepage.President;
public class MyDataBundle_fr_FR extends ListResourceBundle {
  @Override
  protected Object[][] getContents() {
	return contents;
  }
  private Object[][] contents = {
      { "Country", "France" },
      { "President", new President("Emmanuel Macron", 41) },
      { "Literacy", Double.valueOf(99.50) },
  };  
} 
President.java
package com.concretepage;
public class President {
  private String presidentName;
  private int age;

  public President(String presidentName, int age) {
	this.presidentName = presidentName;
	this.age = age;
  }
  //Setters and getters
} 

Create ResourceBundle and Fetch Localized Objects

To create ResourceBundle, we need to call ResourceBundle.getBundle method and pass resource base name and locale. In our example resource class base name is MyDataBundle and we need to pass it as fully qualified class name. Find the code.
ResourceBundle rs = 
    ResourceBundle.getBundle("com.concretepage.lsbundles.MyDataBundle", new Locale("en", "US")); 
To fetch the value for a key from resource, we need to call getObject method on the instance of ResourceBundle.
String country = (String) rs.getObject("Country");
President president = (President) rs.getObject("President");
Double literacy = (Double) rs.getObject("Literacy"); 
getObject returns object of Object type. We need to typecast it in our required data type.
Now find the class of our example that creates ResourceBundle and fetch localized objects.
MyDataBundleDemo.java
package com.concretepage;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyDataBundleDemo {
  public static void main(String[] args) {
	System.out.println("---For Locale en_US---");
	ResourceBundle rs = ResourceBundle.getBundle("com.concretepage.lsbundles.MyDataBundle", new Locale("en", "US"));
	displayStateDetails(rs);

	System.out.println("---For Locale de_DE---");
	rs = ResourceBundle.getBundle("com.concretepage.lsbundles.MyDataBundle", new Locale("de", "DE"));
	displayStateDetails(rs);

	System.out.println("---For Locale fr_FR---");
	rs = ResourceBundle.getBundle("com.concretepage.lsbundles.MyDataBundle", new Locale("fr", "FR"));
	displayStateDetails(rs);
  }

  private static void displayStateDetails(ResourceBundle rs) {
	String country = (String) rs.getObject("Country");
	President president = (President) rs.getObject("President");
	Double literacy = (Double) rs.getObject("Literacy");

	System.out.println(country);
	System.out.println(president.getPresidentName() + "-" + president.getAge());
	System.out.println(literacy);
  }
} 
Output
---For Locale en_US---
United States
Donald Trump-73
99.2
---For Locale de_DE---
Germany
Frank-Walter-63
98.4
---For Locale fr_FR---
France
Emmanuel Macron-41
99.5 

References

Class ListResourceBundle
Using a ListResourceBundle

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us