1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.localization;
22
23 import java.io.BufferedInputStream;
24 import java.io.InputStream;
25 import java.text.MessageFormat;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.ResourceBundle;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import net.fckeditor.handlers.PropertiesLoader;
37 import net.fckeditor.tool.Utils;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class LocalizedMessages {
59
60 private static final Map<Locale, LocalizedMessages> prototypes = Collections
61 .synchronizedMap(new HashMap<Locale, LocalizedMessages>());
62 private static final String DEFAULT_FILENAME = "default_messages.properties";
63 private static final String LOCAL_PROPERTIES = "fckeditor_messages";
64 private static final Properties defaultProperties = new Properties();
65
66 private Properties properties;
67 private static LocaleResolver localeResolver;
68 private static final Locale NEUTRAL = new Locale(Utils.EMPTY_STRING);
69 private static final Logger logger = LoggerFactory
70 .getLogger(LocalizedMessages.class);
71
72 static {
73
74 InputStream in = LocalizedMessages.class
75 .getResourceAsStream(DEFAULT_FILENAME);
76
77 if (in == null) {
78 logger.error("{} not found", DEFAULT_FILENAME);
79 throw new RuntimeException(DEFAULT_FILENAME + " not found");
80 } else {
81 if (!(in instanceof BufferedInputStream))
82 in = new BufferedInputStream(in);
83
84 try {
85 defaultProperties.load(in);
86 in.close();
87 logger.debug("{} loaded", DEFAULT_FILENAME);
88 } catch (Exception e) {
89 logger.error("Error while loading {}", DEFAULT_FILENAME);
90 throw new RuntimeException(
91 "Error while loading " + DEFAULT_FILENAME, e);
92 }
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106 public static LocalizedMessages getInstance(HttpServletRequest request) {
107
108 if (request == null)
109 throw new NullPointerException("the request cannot be null");
110
111 Locale locale = getLocaleResolverInstance().resolveLocale(request);
112
113 if (locale == null)
114 locale = NEUTRAL;
115
116 synchronized (LocalizedMessages.class) {
117 if (!prototypes.containsKey(locale))
118 prototypes.put(locale, new LocalizedMessages(locale));
119 }
120
121
122
123 return prototypes.get(locale);
124
125 }
126
127
128
129
130
131
132
133 private synchronized static LocaleResolver getLocaleResolverInstance() {
134
135 if (localeResolver == null) {
136 String className = PropertiesLoader.getLocaleResolverImpl();
137
138 if (Utils.isEmpty(className))
139 logger.error("Empty LocaleResolver implementation class name provided");
140 else
141 try {
142 Class<?> clazz = Class.forName(className);
143 localeResolver = (LocaleResolver) clazz.newInstance();
144 logger.info("LocaleResolver initialized to {}", className);
145 } catch (Throwable e) {
146 logger.error("LocaleResolver implementation {} could not be instantiated", className);
147 throw new RuntimeException("LocaleResolver implementation " + className + " could not be instantiated", e);
148 }
149 }
150
151 return localeResolver;
152 }
153
154
155
156
157
158
159
160
161
162
163
164 private LocalizedMessages(Locale locale) {
165
166 properties = new Properties(defaultProperties);
167
168 ResourceBundle localized = null;
169 try {
170 localized = ResourceBundle.getBundle(LOCAL_PROPERTIES, locale,
171 Thread.currentThread().getContextClassLoader());
172 } catch (Exception e) {
173 ;
174 }
175
176 if (localized != null
177 && localized.getLocale().getLanguage().equals(
178 locale.getLanguage())) {
179 Enumeration<String> keys = localized.getKeys();
180
181 while (keys.hasMoreElements()) {
182 String key = keys.nextElement();
183 properties.setProperty(key, localized.getString(key));
184 }
185
186 logger.debug("Resource bundle for locale '{}' loaded", locale);
187 } else {
188 logger.debug("No resource bundle for locale '{}' found, loading default bundle", locale);
189
190 ResourceBundle base = null;
191 try {
192 base = ResourceBundle.getBundle(LOCAL_PROPERTIES, NEUTRAL,
193 Thread.currentThread().getContextClassLoader());
194 } catch (Exception e) {
195 ;
196 }
197
198 if (base != null && base.getLocale().equals(NEUTRAL)) {
199 Enumeration<String> keys = base.getKeys();
200
201 while (keys.hasMoreElements()) {
202 String key = keys.nextElement();
203 properties.setProperty(key, base.getString(key));
204 }
205 }
206
207 }
208
209 }
210
211
212
213
214
215
216 private String getMessage(String key) {
217 return properties.getProperty(key);
218 }
219
220
221 public String getCompatibleBrowserYes() {
222 return getMessage("editor.compatibleBrowser.yes");
223 }
224
225
226 public String getCompatibleBrowserNo() {
227 return getMessage("editor.compatibleBrowser.no");
228 }
229
230
231 public String getFileUploadEnabled() {
232 return getMessage("connector.fileUpload.enabled");
233 }
234
235
236 public String getFileUploadDisabled() {
237 return getMessage("connector.fileUpload.disabled");
238 }
239
240
241
242
243
244
245
246
247 public String getFileRenamedWarning(String newFileName) {
248 return MessageFormat.format(getMessage("connector.fileUpload.file_renamed_warning"), newFileName);
249 }
250
251
252 public String getInvalidFileTypeSpecified() {
253 return getMessage("connector.fileUpload.invalid_file_type_specified");
254 }
255
256
257 public String getFileUploadWriteError() {
258 return getMessage("connector.fileUpload.write_error");
259 }
260
261
262 public String getGetResourcesEnabled() {
263 return getMessage("connector.getResources.enabled");
264 }
265
266
267 public String getGetResourcesDisabled() {
268 return getMessage("connector.getResources.disabled");
269 }
270
271
272 public String getGetResourcesReadError() {
273 return getMessage("connector.getResources.read_error");
274 }
275
276
277 public String getCreateFolderEnabled() {
278 return getMessage("connector.createFolder.enabled");
279 }
280
281
282 public String getCreateFolderDisabled() {
283 return getMessage("connector.createFolder.disabled");
284 }
285
286
287 public String getInvalidCommandSpecified() {
288 return getMessage("connector.invalid_command_specified");
289 }
290
291
292 public String getFolderAlreadyExistsError() {
293 return getMessage("connector.createFolder.folder_already_exists_error");
294 }
295
296
297 public String getInvalidNewFolderNameSpecified() {
298 return getMessage("connector.createFolder.invalid_new_folder_name_specified");
299 }
300
301
302 public String getCreateFolderWriteError() {
303 return getMessage("connector.createFolder.write_error");
304 }
305
306
307 public String getInvalidResouceTypeSpecified() {
308 return getMessage("connector.invalid_resource_type_specified");
309 }
310
311
312 public String getInvalidCurrentFolderSpecified() {
313 return getMessage("connector.invalid_current_folder_specified");
314 }
315
316 }