Resource bundles in java

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.
This allows you to write programs that can:
  • be easily localized, or translated, into different languages
  • handle multiple locales at once
  • be easily modified later to support even more locales
Resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, the base name of a family of resource bundles might be "MyResources". The family should have a default resource bundle which simply has the same name as its family - "MyResources" - and will be used as the bundle of last resort if a specific locale is not supported. The family can then provide as many locale-specific members as needed, for example a German one named "MyResources_de".
Each resource bundle in a family contains the same items, but the items have been translated for the locale represented by that resource bundle. For example, both "MyResources" and "MyResources_de" may have a String that's used on a button for canceling operations. In "MyResources" the String may contain "Cancel" and in "MyResources_de" it may contain "Abbrechen". 

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
 
public class HelloResourceBundleExample {
 
  public static void main(String [] argv) {
    try {
      Locale frenchLocale = new Locale("fr", "FR");
      ResourceBundle rb = ResourceBundle.getBundle("HelloResourceBundle", frenchLocale);
 
      System.out.println(rb.getString("Hello"));
      System.out.println(rb.getString("Goodbye"));
 
    } catch (MissingResourceException mre) {
      mre.printStackTrace();
    }
  }
}



Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email