This section provides a tutorial example on how to create resource bundles (localization key names with their localized text messages) as subclasses of the java.util.ListResourceBundle abstract class.
The ListResourceBundle class, java.util.ListResourceBundle, is an abstract subclass of the
java.util.ResourceBundle class. It implements resource bundles as simple object list.
Now let's define some localization key names and localized text messages for some warning messages
using subclasses of java.util.ListResourceBundle class:
1. Mapping WarningMessage key names to localized text messages for the default locale:
/**
* WarningMessage.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class WarningMessage extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"LAST_MINUTE", "Bus is leaving within one minute."},
{"HIGH_LOAD", "System load is very high."}
};
}
Note that:
The two required methods handleGetObject() and getKeys() are already defined in
the ListResourceBundle class. We only need implement a different required method
getContents(), which is much easier to understand and to implement.
1. Mapping WarningMessage key names to localized text messages for the fr locale:
/**
* WarningMessage_fr.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class WarningMessage_fr extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"LAST_MINUTE", "L'autobus part d'une minute."},
{"HIGH_LOAD", "La charge de système est très haute."}
};
}
3. Mapping WarningMessage key names to localized text messages for the fr and CA locale:
/**
* WarningMessage_fr_CA.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class WarningMessage_fr_CA extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"HIGH_LOAD", "L'utilisation de système est très haute."}
};
}