KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > Messages


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.util;
17
18 import java.text.MessageFormat JavaDoc;
19 import java.util.MissingResourceException JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21
22 /**
23  * Internationalization for DWR
24  * @author Joe Walker [joe at getahead dot ltd dot uk]
25  */

26 public class Messages
27 {
28     /**
29      * Prevent instansiation
30      */

31     private Messages()
32     {
33     }
34
35     /**
36      * Internationalize a simple string
37      * @param key The lookup key
38      * @return The internationalized version of the key
39      */

40     public static String JavaDoc getString(String JavaDoc key)
41     {
42         try
43         {
44             return RESOURCE_BUNDLE.getString(key);
45         }
46         catch (MissingResourceException JavaDoc ex)
47         {
48             log.error("Missing I18N string: " + key, ex);
49             return '!' + key + '!';
50         }
51     }
52
53     /**
54      * Internationalize a parameterized string
55      * @param key The lookup key
56      * @param param Lookup parameter number 1
57      * @return The internationalized version of the key
58      */

59     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc param)
60     {
61         return getString(key, new Object JavaDoc[] { param, });
62     }
63
64     /**
65      * Internationalize a parameterized string
66      * @param key The lookup key
67      * @param param1 Lookup parameter number 1
68      * @param param2 Lookup parameter number 2
69      * @return The internationalized version of the key
70      */

71     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc param1, Object JavaDoc param2)
72     {
73         return getString(key, new Object JavaDoc[] { param1, param2, });
74     }
75
76     /**
77      * Internationalize a parameterized string
78      * @param key The lookup key
79      * @param param1 Lookup parameter number 1
80      * @param param2 Lookup parameter number 2
81      * @param param3 Lookup parameter number 3
82      * @return The internationalized version of the key
83      */

84     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3)
85     {
86         return getString(key, new Object JavaDoc[] { param1, param2, param3, });
87     }
88
89     /**
90      * Internationalize a parameterized string
91      * @param key The lookup key
92      * @param param1 Lookup parameter number 1
93      * @param param2 Lookup parameter number 2
94      * @param param3 Lookup parameter number 3
95      * @param param4 Lookup parameter number 4
96      * @return The internationalized version of the key
97      */

98     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4)
99     {
100         return getString(key, new Object JavaDoc[] { param1, param2, param3, param4, });
101     }
102
103     /**
104      * Internationalize a parameterized string
105      * @param key The lookup key
106      * @param params
107      * @return The internationalized version of the key
108      */

109     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc[] params)
110     {
111         try
112         {
113             String JavaDoc format = RESOURCE_BUNDLE.getString(key);
114             return MessageFormat.format(format, params);
115         }
116         catch (MissingResourceException JavaDoc ex)
117         {
118             // Create a list of the parameters
119
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120             buffer.append('[');
121             for (int i = 0; i < params.length; i++)
122             {
123                 if (i != 0)
124                 {
125                     buffer.append(',');
126                 }
127                 buffer.append(params[i].toString());
128             }
129             buffer.append(']');
130
131             log.error("Missing I18N string: " + key + ". Params: " + buffer.toString());
132             return '!' + key + '!' + buffer.toString() + '!';
133         }
134     }
135
136     /**
137      * The log stream
138      */

139     private static final Logger log = Logger.getLogger(Messages.class);
140
141     /**
142      * The lookup bundle
143      */

144     private static final ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle.getBundle("org.directwebremoting.messages");
145 }
146
Popular Tags