KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > I18NHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.spi.persistence.utility;
25
26 import java.util.*;
27 import java.text.MessageFormat JavaDoc;
28
29 public class I18NHelper {
30     private static final String JavaDoc bundleSuffix = ".Bundle"; // NOI18N
31
private static Hashtable bundles = new Hashtable();
32     private static Locale locale = Locale.getDefault();
33
34     /**
35      * Constructor
36      */

37     public I18NHelper() {
38     }
39
40     /**
41      * Load ResourceBundle by bundle name
42      */

43     public static ResourceBundle loadBundle(String JavaDoc bundleName) {
44         return loadBundle(bundleName, I18NHelper.class.getClassLoader());
45     }
46
47     /**
48      * Load ResourceBundle by bundle name and class loader
49      */

50     public static ResourceBundle loadBundle(String JavaDoc bundleName, ClassLoader JavaDoc loader) {
51             ResourceBundle messages = (ResourceBundle)bundles.get(bundleName);
52
53             if (messages == null) //not found as loaded - add
54
{
55                 messages = ResourceBundle.getBundle(bundleName, locale, loader);
56             bundles.put(bundleName, messages);
57             }
58         return messages;
59     }
60
61     /**
62      * Load ResourceBundle by class object - figure out the bundle name
63      * for the class object's package and use the class' class loader.
64      */

65     public static ResourceBundle loadBundle(Class JavaDoc classObject) {
66         return loadBundle(
67             JavaTypeHelper.getPackageName(classObject.getName()) + bundleSuffix,
68             classObject.getClassLoader());
69     }
70
71   
72     /**
73      * Returns message as String
74      */

75     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey)
76     {
77             return messages.getString(messageKey);
78     }
79
80     /**
81      * Formats message by adding Array of arguments
82      */

83     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, Object JavaDoc msgArgs[])
84     {
85             for (int i=0; i<msgArgs.length; i++) {
86                 if (msgArgs[i] == null) msgArgs[i] = ""; // NOI18N
87
}
88             MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(messages.getString(messageKey));
89             return formatter.format(msgArgs);
90     }
91     /**
92      * Formats message by adding a String argument
93      */

94     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, String JavaDoc arg)
95     {
96             Object JavaDoc []args = {arg};
97             return getMessage(messages, messageKey, args);
98     }
99     /**
100      * Formats message by adding two String arguments
101      */

102     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, String JavaDoc arg1,
103                        String JavaDoc arg2)
104     {
105             Object JavaDoc []args = {arg1, arg2};
106             return getMessage(messages, messageKey, args);
107     }
108     /**
109      * Formats message by adding three String arguments
110      */

111     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, String JavaDoc arg1,
112                        String JavaDoc arg2, String JavaDoc arg3)
113     {
114             Object JavaDoc []args = {arg1, arg2, arg3};
115             return getMessage(messages, messageKey, args);
116     }
117     /**
118      *
119      * Formats message by adding an Object as an argument
120      */

121     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, Object JavaDoc arg)
122     {
123             Object JavaDoc []args = {arg};
124             return getMessage(messages, messageKey, args);
125     }
126     /**
127      * Formats message by adding an int as an argument
128      */

129     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, int arg)
130     {
131             Object JavaDoc []args = {new Integer JavaDoc(arg)};
132             return getMessage(messages, messageKey, args);
133     }
134     /**
135      * Formats message by adding a boolean as an argument
136      */

137     final public static String JavaDoc getMessage(ResourceBundle messages, String JavaDoc messageKey, boolean arg)
138     {
139             Object JavaDoc []args = {String.valueOf(arg)};
140             return getMessage(messages, messageKey, args);
141     }
142
143 }
144
Popular Tags