|
Internationalization
Part:
1
2
3
4
5
(Continued from previous part...)
Mapping ErrorMessage keys to the default locale:
/**
* ErrorMessage.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class ErrorMessage extends ResourceBundle {
public Object handleGetObject(String key) {
if (key.equals("NO_FILE")) return "File doesn't exist.";
if (key.equals("FILE_EMPTY")) return "File is empty.";
return null;
}
public Enumeration getKeys() {
Vector keys = new Vector();
keys.addElement("NO_FILE");
keys.addElement("FILE_EMPTY");
return keys.elements();
}
}
Note that:
- Only the two required methods handleGetObject() and getKeys() are implemented.
They are declared as abstract in the ResourceBundle class.
Mapping ErrorMessage keys to the fr locale:
/**
* ErrorMessage_fr.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class ErrorMessage_fr extends ErrorMessage {
public Object handleGetObject(String key) {
if (key.equals("NO_FILE")) return "Document n'exist pas.";
if (key.equals("FILE_EMPTY")) return "Rien.";
return null;
}
}
Note that:
- I am extending ErrorMessage_fr from ErrorMessage, so I don't have to repeat
the handleGetObject() method.
- The resource bundle name is the class name of the default locale. The class names
for other locales must have a sufix of language code.
Mapping ErrorMessage keys to the fr and CA locale:
/**
* ErrorMessage_fr_CA.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.util.*;
public class ErrorMessage_fr_CA extends ErrorMessage {
public Object handleGetObject(String key) {
if (key.equals("NO_FILE")) return "Dossier n'exist pas.";
return null;
}
}
Note that:
- The class names have the same hierarchical structure as the locales. Default locale
is at the top level; language specific locales are at the middle level; language and
country specific locales are at the bottom level.
- For a given key and a given locale, the lookup mechanism searches in the class that
matche the locale first. If failed, it will continue with the class at the parent level.
In my example, for the key, "FILE_EMPTY", and the locale, "fr and CA", the lookup mechnism
will search the ErrorMessage_fr_CA class first, then the ErrorMessage_fr class, then the
ErrorMessage class. So the resulting object will be the String, "Rien." from the
ErrorMessage_fr class.
Creating Resource Bundle with ListResourceBundle Classes: WarningMessage
Mapping WarningMessage keys to 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.
Mapping WarningMessage keys to 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."}
};
}
Mapping WarningMessage keys to 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."}
};
}
(Continued on next part...)
Part:
1
2
3
4
5
|