KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > util > I18N


1 package jimm.util;
2 import java.util.HashMap JavaDoc;
3 import java.util.ResourceBundle JavaDoc;
4 import java.util.Locale JavaDoc;
5 import java.util.MissingResourceException JavaDoc;
6
7 /**
8  * This class finds the local version of any string. It also contains
9  * a method for changing the language.
10  * <p>
11  * Each language should have a file called datavision_XX_YY.properties,
12  * where XX is the language code (e.g., "en" for English, "fr" for French)
13  * and YY is the country code (e.g., "US", "FR").
14  *
15  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
16  */

17 public class I18N {
18
19 public static final String JavaDoc RESOURCE_FILE_PREFIX = "datavision";
20 public static final String JavaDoc MENU_FILE_PREFIX = "menu";
21 public static final String JavaDoc PAPER_FILE_PREFIX = "paper";
22
23 protected static Locale JavaDoc locale;
24 protected static HashMap JavaDoc bundles;
25
26 // Initialize the language.
27
static {
28     setLanguage(Locale.getDefault());
29 }
30
31 /**
32  * Given a locale, start using the code short phrases for that lanuage.
33  * Normally, you won't have to call this method. It gets called at startup
34  * and sets the locale to the default one.
35  *
36  * @param l the new locale
37  */

38 public static void setLanguage(Locale JavaDoc l) {
39     if (!l.equals(locale)) {
40     locale = l;
41     bundles = new HashMap JavaDoc();
42     }
43 }
44
45 /**
46  * Returns the string corresponding to the specified string. Returns
47  * <var>key</var> if <var>key</var> is either <code>null</code> or the
48  * empty string. Reports an error if <var>key</var> does not exist.
49  *
50  * @param key the lookup key
51  * @return the string corresponding to the specified lookup key
52  * or <code>null</code> if there isn't one; if <var>key</var> is the
53  * empty string, return it
54  */

55 public static String JavaDoc get(String JavaDoc key) {
56     return get(RESOURCE_FILE_PREFIX, key);
57 }
58
59 /**
60  * Returns the string corresponding to the specified string in the bundle
61  * file corresponding to the name <var>prefix</var>. Returns <var>key</var>
62  * if <var>key</var> is either <code>null</code> or the empty string.
63  * Reports an error if <var>key</var> does not exist.
64  *
65  * @param prefix the bundle file name prefix
66  * @param key the lookup key
67  * @return the string corresponding to the specified lookup key
68  * or <code>null</code> if there isn't one; if <var>key</var> is the
69  * empty string, return it
70  */

71 public static String JavaDoc get(String JavaDoc prefix, String JavaDoc key) {
72     if (key == null || prefix == null || prefix.length() == 0) return null;
73     if (key.length() == 0) return "";
74
75     String JavaDoc val = "";
76     try {
77     ResourceBundle JavaDoc strings = getBundle(prefix);
78     val = strings.getString(key);
79     if (val == null) val = key;
80     else val = val.trim();
81     }
82     catch (MissingResourceException JavaDoc ex) {
83     val = key;
84     }
85     return val;
86 }
87
88 /**
89  * Returns the string corresponding to the specified string. Returns
90  * <var>key</var> if <var>key</var> is either <code>null</code> or the
91  * empty string. Reports <code>null</code> if <var>key</var> does not exist.
92  *
93  * @param key the lookup key
94  * @return the string corresponding to the specified lookup key
95  * or <code>null</code> if there isn't one; if <var>key</var> is the
96  * empty string, return it
97  */

98 public static String JavaDoc getNullIfMissing(String JavaDoc key) {
99     return getNullIfMissing(RESOURCE_FILE_PREFIX, key);
100 }
101
102 /**
103  * Returns the string corresponding to the specified string in the bundle
104  * file corresponding to the name <var>prefix</var>. Returns <var>key</var>
105  * if <var>key</var> is either <code>null</code> or the empty string.
106  * Reports <code>null</code> if <var>key</var> does not exist.
107  *
108  * @param prefix the bundle file name prefix
109  * @param key the lookup key
110  * @return the string corresponding to the specified lookup key
111  * or <code>null</code> if there isn't one; if <var>key</var> is the
112  * empty string, return it
113  */

114 public static String JavaDoc getNullIfMissing(String JavaDoc prefix, String JavaDoc key) {
115     if (key == null || prefix == null || prefix.length() == 0) return null;
116     if (key.length() == 0) return "";
117
118     String JavaDoc val = null;
119     try {
120     ResourceBundle JavaDoc strings = getBundle(prefix);
121     val = strings.getString(key);
122     if (val == null) val = "";
123     else val = val.trim();
124     }
125     catch (MissingResourceException JavaDoc ex) {
126     val = null;
127     }
128     return val;
129 }
130
131 protected static ResourceBundle JavaDoc getBundle(String JavaDoc prefix) {
132     ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc)bundles.get(prefix);
133     if (bundle == null) {
134     bundle = ResourceBundle.getBundle(prefix, locale);
135     bundles.put(prefix, bundle);
136     }
137     return bundle;
138 }
139
140 }
141
Popular Tags