001: package jimm.util;
002:
003: import java.util.HashMap;
004: import java.util.ResourceBundle;
005: import java.util.Locale;
006: import java.util.MissingResourceException;
007:
008: /**
009: * This class finds the local version of any string. It also contains
010: * a method for changing the language.
011: * <p>
012: * Each language should have a file called datavision_XX_YY.properties,
013: * where XX is the language code (e.g., "en" for English, "fr" for French)
014: * and YY is the country code (e.g., "US", "FR").
015: *
016: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
017: */
018: public class I18N {
019:
020: public static final String RESOURCE_FILE_PREFIX = "datavision";
021: public static final String MENU_FILE_PREFIX = "menu";
022: public static final String PAPER_FILE_PREFIX = "paper";
023:
024: protected static Locale locale;
025: protected static HashMap bundles;
026:
027: // Initialize the language.
028: static {
029: setLanguage(Locale.getDefault());
030: }
031:
032: /**
033: * Given a locale, start using the code short phrases for that lanuage.
034: * Normally, you won't have to call this method. It gets called at startup
035: * and sets the locale to the default one.
036: *
037: * @param l the new locale
038: */
039: public static void setLanguage(Locale l) {
040: if (!l.equals(locale)) {
041: locale = l;
042: bundles = new HashMap();
043: }
044: }
045:
046: /**
047: * Returns the string corresponding to the specified string. Returns
048: * <var>key</var> if <var>key</var> is either <code>null</code> or the
049: * empty string. Reports an error if <var>key</var> does not exist.
050: *
051: * @param key the lookup key
052: * @return the string corresponding to the specified lookup key
053: * or <code>null</code> if there isn't one; if <var>key</var> is the
054: * empty string, return it
055: */
056: public static String get(String key) {
057: return get(RESOURCE_FILE_PREFIX, key);
058: }
059:
060: /**
061: * Returns the string corresponding to the specified string in the bundle
062: * file corresponding to the name <var>prefix</var>. Returns <var>key</var>
063: * if <var>key</var> is either <code>null</code> or the empty string.
064: * Reports an error if <var>key</var> does not exist.
065: *
066: * @param prefix the bundle file name prefix
067: * @param key the lookup key
068: * @return the string corresponding to the specified lookup key
069: * or <code>null</code> if there isn't one; if <var>key</var> is the
070: * empty string, return it
071: */
072: public static String get(String prefix, String key) {
073: if (key == null || prefix == null || prefix.length() == 0)
074: return null;
075: if (key.length() == 0)
076: return "";
077:
078: String val = "";
079: try {
080: ResourceBundle strings = getBundle(prefix);
081: val = strings.getString(key);
082: if (val == null)
083: val = key;
084: else
085: val = val.trim();
086: } catch (MissingResourceException ex) {
087: val = key;
088: }
089: return val;
090: }
091:
092: /**
093: * Returns the string corresponding to the specified string. Returns
094: * <var>key</var> if <var>key</var> is either <code>null</code> or the
095: * empty string. Reports <code>null</code> if <var>key</var> does not exist.
096: *
097: * @param key the lookup key
098: * @return the string corresponding to the specified lookup key
099: * or <code>null</code> if there isn't one; if <var>key</var> is the
100: * empty string, return it
101: */
102: public static String getNullIfMissing(String key) {
103: return getNullIfMissing(RESOURCE_FILE_PREFIX, key);
104: }
105:
106: /**
107: * Returns the string corresponding to the specified string in the bundle
108: * file corresponding to the name <var>prefix</var>. Returns <var>key</var>
109: * if <var>key</var> is either <code>null</code> or the empty string.
110: * Reports <code>null</code> if <var>key</var> does not exist.
111: *
112: * @param prefix the bundle file name prefix
113: * @param key the lookup key
114: * @return the string corresponding to the specified lookup key
115: * or <code>null</code> if there isn't one; if <var>key</var> is the
116: * empty string, return it
117: */
118: public static String getNullIfMissing(String prefix, String key) {
119: if (key == null || prefix == null || prefix.length() == 0)
120: return null;
121: if (key.length() == 0)
122: return "";
123:
124: String val = null;
125: try {
126: ResourceBundle strings = getBundle(prefix);
127: val = strings.getString(key);
128: if (val == null)
129: val = "";
130: else
131: val = val.trim();
132: } catch (MissingResourceException ex) {
133: val = null;
134: }
135: return val;
136: }
137:
138: protected static ResourceBundle getBundle(String prefix) {
139: ResourceBundle bundle = (ResourceBundle) bundles.get(prefix);
140: if (bundle == null) {
141: bundle = ResourceBundle.getBundle(prefix, locale);
142: bundles.put(prefix, bundle);
143: }
144: return bundle;
145: }
146:
147: }
|