KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > i18n > I18NMessageManager


1 /*
2  * $Id: I18NMessageManager.java,v 1.7 2005/01/10 23:25:36 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.i18n;
27
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33
34 import org.apache.commons.lang.StringUtils;
35
36 import com.j2biz.blogunity.BlogunityManager;
37 import com.j2biz.blogunity.IConstants;
38
39 public class I18NMessageManager {
40
41     private static final String JavaDoc ERRORS_BUNDLE_NAME = "i18n_errors";
42
43     private static final String JavaDoc MESSAGES_BUNDLE_NAME = "i18n_messages";
44
45     private static final String JavaDoc NAVIGATION_BUNDLE_NAME = "i18n_navigation";
46
47     private static I18NMessageManager INSTANCE = null;
48
49     private I18NMessageManager() {
50
51     }
52
53     public static I18NMessageManager getInstance() {
54         if (INSTANCE == null) INSTANCE = new I18NMessageManager();
55         return INSTANCE;
56     }
57
58     /**
59      * @return
60      */

61     private Locale JavaDoc getSystemLocale() {
62         return BlogunityManager.getSystemConfiguration().getSystemLocale();
63     }
64
65     /**
66      * @param key
67      * @return
68      */

69     public String JavaDoc getMessage(String JavaDoc key) {
70         return getMessage(key, new String JavaDoc[]{}, getSystemLocale());
71     }
72
73     /**
74      * @param key
75      * @param params
76      * @return
77      */

78     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc[] params) {
79         return getMessage(key, params, getSystemLocale());
80     }
81
82     /**
83      * @param key
84      * @param locale
85      * @return
86      */

87     public String JavaDoc getMessage(String JavaDoc key, Locale JavaDoc locale) {
88         return getMessage(key, new String JavaDoc[]{}, locale);
89     }
90
91     public String JavaDoc getMessage(String JavaDoc key, HttpServletRequest JavaDoc request) {
92         return getMessage(key, new String JavaDoc[]{}, request);
93     }
94
95     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc[] params, HttpServletRequest JavaDoc request) {
96
97         Locale JavaDoc locale = (Locale JavaDoc) request.getAttribute(IConstants.Request.LOCALE);
98         return getMessage(key, params, locale);
99     }
100
101     /**
102      * @param key
103      * @param params
104      * @param locale
105      * @return
106      */

107     public String JavaDoc getMessage(String JavaDoc key, String JavaDoc[] params, Locale JavaDoc locale) {
108         return getTextFromBundle(key, params, locale, MESSAGES_BUNDLE_NAME);
109     }
110
111     /**
112      * @param key
113      * @return
114      */

115     public String JavaDoc getError(String JavaDoc key) {
116         return getError(key, new String JavaDoc[]{}, getSystemLocale());
117     }
118
119     /**
120      * @param key
121      * @param params
122      * @return
123      */

124     public String JavaDoc getError(String JavaDoc key, String JavaDoc[] params) {
125         return getError(key, params, getSystemLocale());
126     }
127
128     /**
129      * @param key
130      * @param locale
131      * @return
132      */

133     public String JavaDoc getError(String JavaDoc key, Locale JavaDoc locale) {
134         return getError(key, new String JavaDoc[]{}, locale);
135     }
136
137     /**
138      * @param key
139      * @param params
140      * @param locale
141      * @return
142      */

143     public String JavaDoc getError(String JavaDoc key, String JavaDoc[] params, Locale JavaDoc locale) {
144         return getTextFromBundle(key, params, locale, ERRORS_BUNDLE_NAME);
145     }
146
147     /**
148      * @param key
149      * @return
150      */

151     public String JavaDoc getNavigationText(String JavaDoc key) {
152         return getNavigationText(key, new String JavaDoc[]{}, getSystemLocale());
153     }
154
155     /**
156      * @param key
157      * @param params
158      * @return
159      */

160     public String JavaDoc getNavigationText(String JavaDoc key, String JavaDoc[] params) {
161         return getNavigationText(key, params, getSystemLocale());
162     }
163
164     /**
165      * @param key
166      * @param locale
167      * @return
168      */

169     public String JavaDoc getNavigationText(String JavaDoc key, Locale JavaDoc locale) {
170         return getNavigationText(key, new String JavaDoc[]{}, locale);
171     }
172
173     /**
174      * @param key
175      * @param params
176      * @param locale
177      * @return
178      */

179     public String JavaDoc getNavigationText(String JavaDoc key, String JavaDoc[] params, Locale JavaDoc locale) {
180         return getTextFromBundle(key, params, locale, NAVIGATION_BUNDLE_NAME);
181     }
182
183     private String JavaDoc getTextFromBundle(String JavaDoc key, String JavaDoc[] params, Locale JavaDoc locale, String JavaDoc bundleName) {
184         ResourceBundle JavaDoc messagesBundle = ResourceBundle.getBundle(bundleName, locale);
185
186         if (StringUtils.isNotEmpty(key)) {
187
188             String JavaDoc value = null;
189             try {
190                 value = new String JavaDoc(messagesBundle.getString(key).getBytes("ISO-8859-1"), "UTF-8");
191             } catch (Throwable JavaDoc t) {
192                 return key;
193             }
194             if (params != null && params.length > 0) {
195                 value = MessageFormat.format(value, params);
196             }
197
198             if (StringUtils.isEmpty(value)) return key;
199             else
200                 return value;
201         }
202         return I18N.MESSAGES.UNKNOWN;
203     }
204
205 }
Popular Tags