Home Tutorials I18N Create Resource Bundle in the java code

Related Tutorials


 
 

Share on Google+Share on Google+

Create Resource Bundle in the java code

Advertisement
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.

 

ResourceBundleExample.java


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]);

  System.out.println(locales[i].getDisplayName());
  System.out.println(bundle.getString("Hello"));
  System.out.println(bundle.getString("Goodbye")+"\n");
  }

  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Resource.java 
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;
  }
}

Resource_en_IN.java
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;
  }
}

Resource_fr_FR.java
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)
Bonjour
Au Revoir

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

Download code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: September 22, 2006

Related Tutorials

Discuss: Create Resource Bundle in the java code  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Mijail
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)
DMCA.com