Share on Google+Share on Google+

Create Resource Bundle in the java code

Tutorial on Creating Resource Bundle in the java code

Create Resource Bundle in the java code


This Example shows you how to create resource bundle. In the code given below we are create resource bundle for a locale.

Methods used in this example are described below :

ResourceBundle.getBundle() : ResourceBundle class object holds locale-specific objects. When a program needs a locale-specific resource on that time program can load locale from the resource bundle that is suitable for the current user's locale. Method getBundle() returns a ResourceBundle class object using the specified name and locale.

import java.util.*;

public class ResourceBundleExample {

  public static void main(String args[]) {
  try {
  Locale[] locales = new Locale[]{
  new Locale("fr""FR")new Locale("en""IN")
  ResourceBundle bundle = null;
  for (int i = 0; i < locales.length; i++) {
  bundle = ResourceBundle.getBundle("Resource", locales[i]);


  catch (Exception e) {
import java.util.*;

public class Resource extends java.util.ResourceBundle {
  String keys = "Hello Goodbye";

  public Object handleGetObject(String key) {
  if (key.equals("Hello")) {
  return "Hello";
  if (key.equals("Goodbye")) {
  return "Goodbye";
  return null;

  public Enumeration getKeys() {
  StringTokenizer key = new StringTokenizer(keys);
  return key;
import java.util.*;

public class Resource_en_IN extends Resource{

  public Object handleGetObject(String key) {
  if (key.equals("Goodbye")) return "Have a nice day ...";
 if (key.equals("Hello")) return "Hi, How are you ...";

  return null;
import java.util.*;

public class Resource_fr_FR extends Resource{

  public Object handleGetObject(String key) {
  if (key.equals("Goodbye")) return "Au Revoir";
 if (key.equals("Hello")) return "Bonjour";

  return null;

Output :
French (France)
Au Revoir

English (India)
Hi, How are you ...
Have a nice day ...

Download code



Posted on: September 22, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Create Resource Bundle in the java code  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
December 5, 2011
Doesnt load

I copied exactly as you wrote it, give the files the same names, and I placed them in their own package, but gives me the following result: java.util.MissingResourceException: Can't find bundle for base name Resource, locale fr_FR BUILD SUCCESSFUL (total time: 0 seconds)