KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > core > I18N


1 package org.dspace.core;
2
3 import java.util.Locale JavaDoc;
4 import java.util.ResourceBundle JavaDoc;
5
6 /**
7  * Utility class for internationalisation of message generating code. The
8  * message(String, Class) and getMessage(String, Class) methods should be
9  * favoured.
10  *
11  * @author Jim Downing
12  */

13 public class I18N {
14
15     private static final I18N DEFAULT = new I18N(Locale.getDefault());
16
17     /**
18      * Utility method for finding the class of the calling object to this class.
19      * @param stack
20      * @return classname
21      */

22 // private static String callerClassName(StackTraceElement[] stack) {
23
// for (int i = 0; i < stack.length; i++) {
24
// String currentClass = stack[i].getClassName();
25
// if (currentClass.indexOf("java.") == 0)
26
// continue;
27
// if (I18N.class.getName().equals(currentClass))
28
// continue;
29
// return currentClass;
30
// }
31
// return "";
32
// }
33

34     /**
35      * Super convenience method that works out the calling class by examining
36      * the Thread's stack at the point of invocation. This is probably naughty
37      * and certainly excessively expensive, so prefer to use the message(String,
38      * Class) method if you're making repeated calls.
39      *
40      * @param msg
41      * the key of the message required, omitting the fully qualified
42      * class name
43      * @return message for the JVM default Locale
44      */

45 // public static String message(String msg) {
46
// return DEFAULT.getMessage(msg);
47
// }
48

49     /**
50      * Convenience method to get localized messages in code
51      *
52      * @param msg
53      * message key, omitting fully qualified class name
54      * @param clazz
55      * The class object the messages are filed under
56      * @return Localized message for the JVM default Locale
57      */

58     public static String JavaDoc message(String JavaDoc msg, Class JavaDoc clazz) {
59         return DEFAULT.getMessage(msg, clazz);
60     }
61
62     /**
63      * <p>
64      * Convenience method to get localized messages in code. Usage will be of
65      * the form: -
66      * </p>
67      *
68      * <pre>
69      * I18N.message(&quot;my-key&quot;, this);
70      * </pre>
71      *
72      * <p>
73      * N.B. That this method uses the runtime type of the object, and hence
74      * should only be used in final classes.
75      * </p>
76      *
77      * @param msg
78      * Key for the message, omitting the fully qualified class name
79      * @param obj
80      * the calling object
81      * @return Localized message for the JVM default Locale
82      */

83     public static String JavaDoc message(String JavaDoc msg, Object JavaDoc obj) {
84         return DEFAULT.getMessage(msg, obj);
85     }
86
87     /**
88      * Slightly convenient method that appends together a msg key and a class
89      * name together and retrieves the corresponding message from
90      * Messages.properties
91      *
92      * @param msg
93      * final part of message key, omitting fully qualified class name
94      * @param classname
95      * fully qualified class name.
96      * @return Localized message for the JVM default Locale
97      */

98     public static String JavaDoc message(String JavaDoc msg, String JavaDoc classname) {
99         return DEFAULT.getMessage(msg, classname);
100     }
101
102     private ResourceBundle JavaDoc messages = null;
103
104     private I18N() {
105         ;
106     }
107
108     public I18N(Locale JavaDoc locale) {
109         messages = ResourceBundle.getBundle("Messages", locale);
110     }
111
112     /**
113      * Super convenience method that works out the calling class by examining
114      * the Thread's stack at the point of invocation. This is probably naughty
115      * and certainly excessively expensive, so prefer to use the message(String,
116      * Class) method if you're making repeated calls.
117      *
118      * @param msg
119      * the key of the message required, omitting the fully qualified
120      * class name
121      * @return message
122      */

123 // public String getMessage(String msg) {
124
// return getMessage(msg, callerClassName(Thread.currentThread()
125
// .getStackTrace()));
126
// }
127

128     /**
129      * Convenience method to get localized messages in code
130      *
131      * @param msg
132      * message key, omitting fully qualified class name
133      * @param clazz
134      * The class object the messages are filed under
135      * @return Localized message
136      */

137     public String JavaDoc getMessage(String JavaDoc msg, Class JavaDoc clazz) {
138         String JavaDoc className = clazz.getName();
139         return messages.getString(new StringBuffer JavaDoc(50).append(className)
140                 .append(".").append(msg).toString());
141     }
142
143     /**
144      * <p>
145      * Convenience method to get localized messages in code. Usage will be of
146      * the form: -
147      * </p>
148      *
149      * <pre>
150      * I18N.message(&quot;my-key&quot;, this);
151      * </pre>
152      *
153      * <p>
154      * N.B. That this method uses the runtime type of the object, and hence
155      * should only be used in final classes.
156      * </p>
157      *
158      * @param msg
159      * Key for the message, omitting the fully qualified class name
160      * @param obj
161      * the calling object
162      * @return Localized message
163      */

164     public String JavaDoc getMessage(String JavaDoc msg, Object JavaDoc obj) {
165         return getMessage(msg, obj.getClass());
166     }
167
168     /**
169      * Slightly convenient method that appends together a msg key and a class
170      * name together and retrieves the corresponding message from
171      * Messages.properties
172      *
173      * @param msg
174      * final part of message key, omitting fully qualified class name
175      * @param classname
176      * fully qualified class name.
177      * @return Localized message
178      */

179 // public String getMessage(String msg, String classname) {
180
// return messages.getString(new StringBuilder(50).append(classname)
181
// .append(".").append(msg).toString());
182
// }
183

184 }
185
Popular Tags