JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Internationalization

Part:   1  2  3  4  5  

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

Creating Resource Bundle with Property Files: NoteMessage

Mapping NoteMessage keys to the default locale, file name: NoteMessage.properties

STARTING_SYSTEM=Starting the system...
LOADING_SETTINGS=Loading user's settings...

Note that:

  • The property file names follow the same convention as the bundle class names.
  • When the NoteMessage bundle is requested, the system will actually read in the property file, and convert it into a PropertyResourceBundle.

Mapping NoteMessage keys to the fr locale, file name: NoteMessage_fr.properties

STARTING_SYSTEM=Commencer le système...
LOADING_SETTINGS=Les arrangements de l'utilisateur de chargement...

Mapping NoteMessage keys to the fr and CA locale, file name: NoteMessage_fr_CA.properties

LOADING_SETTINGS=Commencer à charger les arrangements de l'utilisateur..

Using Resource Bundles

Here is a sample program that shows how to request a resource bundle and lookup the bundle object of the a given key and a given locale:

/**
 * ResourceBundleTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
public class ResourceBundleTest {
   public static void main(String[] a) {
      bundleClassTest();
      listBundleClassTest();
      propertyBundleFileTest();
   }
   public static void bundleClassTest() {
      ResourceBundle rb;
      Locale l;
      System.out.println("ResourceBundle Test:");
      System.out.println("The default resource:");
      rb = ResourceBundle.getBundle("ErrorMessage");
      System.out.println("  1:" + rb.getString("NO_FILE"));
      System.out.println("  2:" + rb.getString("FILE_EMPTY"));
      l = Locale.FRENCH;
      System.out.println("The French resource:");
      rb = ResourceBundle.getBundle("ErrorMessage",l);
      System.out.println("  1:" + rb.getString("NO_FILE"));
      System.out.println("  2:" + rb.getString("FILE_EMPTY"));
      l = new Locale("fr","CA");
      System.out.println("The French (Cananda) resource:");
      rb = ResourceBundle.getBundle("ErrorMessage",l);
      System.out.println("  1:" + rb.getString("NO_FILE"));
      System.out.println("  2:" + rb.getString("FILE_EMPTY"));
   }
   public static void listBundleClassTest() {
      ResourceBundle rb;
      Locale l;
      System.out.println("ListResourceBundle Test:");
      System.out.println("The default resource:");
      rb = ResourceBundle.getBundle("WarningMessage");
      System.out.println("  1:" + rb.getString("LAST_MINUTE"));
      System.out.println("  2:" + rb.getString("HIGH_LOAD"));
      l = Locale.FRENCH;
      System.out.println("The French resource:");
      rb = ResourceBundle.getBundle("WarningMessage",l);
      System.out.println("  1:" + rb.getString("LAST_MINUTE"));
      System.out.println("  2:" + rb.getString("HIGH_LOAD"));
      l = new Locale("fr","CA");
      System.out.println("The French (Cananda) resource:");
      rb = ResourceBundle.getBundle("WarningMessage",l);
      System.out.println("  1:" + rb.getString("LAST_MINUTE"));
      System.out.println("  2:" + rb.getString("HIGH_LOAD"));
   }
   public static void propertyBundleFileTest() {
      ResourceBundle rb;
      Locale l;
      System.out.println("PropertyResourceBundle Test:");
      System.out.println("The default resource:");
      rb = ResourceBundle.getBundle("NoteMessage");
      System.out.println("  1:" + rb.getString("STARTING_SYSTEM"));
      System.out.println("  2:" + rb.getString("LOADING_SETTINGS"));
      l = Locale.FRENCH;
      System.out.println("The French resource:");
      rb = ResourceBundle.getBundle("NoteMessage",l);
      System.out.println("  1:" + rb.getString("STARTING_SYSTEM"));
      System.out.println("  2:" + rb.getString("LOADING_SETTINGS"));
      l = new Locale("fr","CA");
      System.out.println("The French (Cananda) resource:");
      rb = ResourceBundle.getBundle("NoteMessage",l);
      System.out.println("  1:" + rb.getString("STARTING_SYSTEM"));
      System.out.println("  2:" + rb.getString("LOADING_SETTINGS"));
   }
}

Output

ResourceBundle Test:
The default resource:
  1:File doesn't exist.
  2:File is empty.
The French resource:
  1:Document n'exist pas.
  2:Rien.
The French (Cananda) resource:
  1:Dossier n'exist pas.
  2:Rien.
ListResourceBundle Test:
The default resource:
  1:Bus is leaving within one minute.
  2:System load is very high.
The French resource:
  1:L'autobus part d'une minute.
  2:La charge de système est très haute.
The French (Cananda) resource:
  1:L'autobus part d'une minute.
  2:L'utilisation de système est très haute.
PropertyResourceBundle Test:
The default resource:
  1:Starting the system...
  2:Loading user's settings...
The French resource:
  1:Commencer le système...
  2:Les arrangements de l'utilisateur de chargement...
The French (Cananda) resource:
  1:Commencer le système...
  2:Commencer à charger les arrangements de l'utilisateur...

Note that:

  • All the bundle classes must be compiled. You may need the "-classpath ." option to help the "javac" command to locate the depending classes to compile a multi classes program.
  • You may need the "-cp ." option to help the "java" command to locate the depending classes and the bundle property files.

Comparing the 3 different ways of internationalize text messages, the property file is the best:

  • Easy to implement.
  • No re-compilation is needed after modification.

Part:   1  2  3  4  5  

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Internationalization