svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / src / main / java / org / gvsig / i18n / Messages.java @ 41314
History | View | Annotate | Download (27.8 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | |
25 | package org.gvsig.i18n; |
||
26 | |||
27 | import java.io.File; |
||
28 | import java.io.IOException; |
||
29 | import java.io.InputStream; |
||
30 | import java.net.MalformedURLException; |
||
31 | import java.net.URL; |
||
32 | import java.text.MessageFormat; |
||
33 | import java.util.ArrayList; |
||
34 | import java.util.Enumeration; |
||
35 | import java.util.HashSet; |
||
36 | import java.util.IllegalFormatException; |
||
37 | import java.util.Iterator; |
||
38 | 41314 | jjdelcerro | import java.util.List; |
39 | 40435 | jjdelcerro | import java.util.Locale; |
40 | import java.util.Properties; |
||
41 | import java.util.Set; |
||
42 | |||
43 | import org.slf4j.Logger; |
||
44 | import org.slf4j.LoggerFactory; |
||
45 | |||
46 | /**
|
||
47 | * <p>This class offers some methods to provide internationalization services
|
||
48 | * to other projects. All the methods are static.</p>
|
||
49 | *
|
||
50 | * <p>The most useful method is {@link #getText(String) getText(key)} (and family),
|
||
51 | * which returns the translation associated
|
||
52 | * with the provided key. The class must be initialized before getText can be
|
||
53 | * used.</p>
|
||
54 | *
|
||
55 | * <p>The typical usage sequence would be:</p>
|
||
56 | * <ul>
|
||
57 | * <li>Add some locale to the prefered locales list: <code>Messages.addLocale(new Locale("es"))</code></li>
|
||
58 | * <li>Add some resource file containing translations: <code>Messages.addResourceFamily("text", new File("."))</code></li>
|
||
59 | * <li>And finaly getText can be used: <code>Messages.getText("aceptar")</code></li>
|
||
60 | * </ul>
|
||
61 | *
|
||
62 | * <p>The resource files are Java properties files, which contain <code>key=translation</code>
|
||
63 | * pairs, one
|
||
64 | * pair per line. These files must be encoded using iso-8859-1 encoding, and unicode escaped
|
||
65 | * sequences must be used to include characters outside the former encoding.
|
||
66 | * There are several ways to specify the property file to load, see the different
|
||
67 | * addResourceFamily methods for details.</p>
|
||
68 | *
|
||
69 | * @author Cesar Martinez Izquierdo (cesar.martinez@iver.es)
|
||
70 | *
|
||
71 | */
|
||
72 | public class Messages { |
||
73 | 41314 | jjdelcerro | |
74 | private static class FamilyDescriptor { |
||
75 | String family = null; |
||
76 | ClassLoader loader = null; |
||
77 | String callerName = null; |
||
78 | |||
79 | public FamilyDescriptor(String family, ClassLoader loader, String callerName ) { |
||
80 | this.family = family;
|
||
81 | this.loader = loader;
|
||
82 | this.callerName = callerName;
|
||
83 | } |
||
84 | } |
||
85 | |||
86 | 40435 | jjdelcerro | private static Logger logger = LoggerFactory.getLogger("Messages"); |
87 | private static String _CLASSNAME = "org.gvsig.i18n.Messages"; |
||
88 | 41314 | jjdelcerro | private static Locale currentLocale; |
89 | 40435 | jjdelcerro | |
90 | /* Each entry will contain a hashmap with translations. Each hasmap
|
||
91 | * contains the translations for one language, indexed by the
|
||
92 | * translation key. The translations for language (i) in the preferred locales
|
||
93 | * list are contained in the position (i) of the localeResources list */
|
||
94 | private static ArrayList localeResources = new ArrayList(); |
||
95 | private static ArrayList preferredLocales = new ArrayList(); // contains the ordered list of prefered languages/locales (class Locale) |
||
96 | |||
97 | |||
98 | /* Set of resource families and classloaders used to load i18n resources. */
|
||
99 | private static Set resourceFamilies = new HashSet(); |
||
100 | private static Set classLoaders = new HashSet(); |
||
101 | |||
102 | 41314 | jjdelcerro | private static List familyDescriptors = new ArrayList(); |
103 | |||
104 | 40435 | jjdelcerro | /*
|
105 | * The language considered the origin of translations, which will
|
||
106 | * (possibly) be stored in a property file without language suffix
|
||
107 | * (ie: text.properties instead of text_es.properties).
|
||
108 | */
|
||
109 | private static String baseLanguage = "es"; |
||
110 | private static Locale baseLocale = new Locale(baseLanguage); |
||
111 | |||
112 | /**
|
||
113 | * <p>Gets the localized message associated with the provided key.
|
||
114 | * If the key is not in the dictionary, return the key and register
|
||
115 | * the failure in the log.</p>
|
||
116 | *
|
||
117 | * <p>The <code>callerName</code> parameter is only
|
||
118 | * used as a label when logging, so any String can be used. However, a
|
||
119 | * meaningful String should be used, such as the name of the class requiring
|
||
120 | * the translation services, in order to identify the source of the failure
|
||
121 | * in the log.</p>
|
||
122 | *
|
||
123 | * @param key An String which identifies the translation that we want to get.
|
||
124 | * @param callerName A symbolic name given to the caller of this method, to
|
||
125 | * show it in the log if the key was not found
|
||
126 | * @return an String with the message associated with the provided key.
|
||
127 | * If the key is not in the dictionary, return the key. If the key
|
||
128 | * is null, return null.
|
||
129 | */
|
||
130 | public static String getText(String key, String callerName) { |
||
131 | if (key==null) { |
||
132 | return null; |
||
133 | } |
||
134 | for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
||
135 | // try to get the translation for any of the languagues in the preferred languages list
|
||
136 | String translation = ((Properties)localeResources.get(numLocale)).getProperty(key); |
||
137 | if (translation!=null && !translation.equals("")) { |
||
138 | return translation;
|
||
139 | } |
||
140 | } |
||
141 | 41030 | jjdelcerro | logger.info("["+callerName+ "] Cannot find translation for key '"+key+"'."); |
142 | 40435 | jjdelcerro | return key;
|
143 | } |
||
144 | |||
145 | public static String getText(String key, String[] arguments, String callerName) { |
||
146 | String translation = getText(key, callerName);
|
||
147 | if (translation!=null && arguments!=null ) { |
||
148 | try {
|
||
149 | translation = MessageFormat.format(translation, arguments);
|
||
150 | } |
||
151 | catch (IllegalFormatException ex) { |
||
152 | logger.error(callerName+" -- Error formating key: "+key+" -- "+translation); |
||
153 | } |
||
154 | } |
||
155 | return translation;
|
||
156 | } |
||
157 | |||
158 | public static String translate(String message, String[] args) { |
||
159 | String msg = message;
|
||
160 | if (msg == null) { |
||
161 | return ""; |
||
162 | } |
||
163 | msg = getText(msg, args); |
||
164 | if (msg == null) { |
||
165 | msg = "_" + message.replace("_", " "); |
||
166 | } |
||
167 | return msg;
|
||
168 | } |
||
169 | |||
170 | public static String translate(String message) { |
||
171 | String msg = message;
|
||
172 | if (msg == null) { |
||
173 | return ""; |
||
174 | } |
||
175 | msg = getText(msg, (String[]) null); |
||
176 | 40633 | jjdelcerro | if (msg == null || msg.startsWith("_")) { |
177 | 40435 | jjdelcerro | msg = "_" + message.replace("_", " "); |
178 | } |
||
179 | return msg;
|
||
180 | } |
||
181 | |||
182 | /**
|
||
183 | * <p>Gets the localized message associated with the provided key.
|
||
184 | * If the key is not in the dictionary or the translation is empty,
|
||
185 | * return the key and register the failure in the log.</p>
|
||
186 | *
|
||
187 | * @param key An String which identifies the translation that we want to get.
|
||
188 | * @return an String with the message associated with the provided key.
|
||
189 | * If the key is not in the dictionary or the translation is empty,
|
||
190 | * return the key. If the key is null, return null.
|
||
191 | */
|
||
192 | public static String getText(String key) { |
||
193 | return getText(key, _CLASSNAME);
|
||
194 | } |
||
195 | |||
196 | public static String getText(String key, String[] arguments) { |
||
197 | return getText(key, arguments, _CLASSNAME);
|
||
198 | } |
||
199 | |||
200 | |||
201 | /**
|
||
202 | * <p>Gets the localized message associated with the provided key.
|
||
203 | * If the key is not in the dictionary or the translation is empty,
|
||
204 | * it returns null and the failure is only registered in the log if
|
||
205 | * the param log is true.</p>
|
||
206 | *
|
||
207 | * @param key An String which identifies the translation that we want
|
||
208 | * to get.
|
||
209 | * @param log Determines whether log a key failure or not
|
||
210 | * @return an String with the message associated with the provided key,
|
||
211 | * or null if the key is not in the dictionary or the
|
||
212 | * translation is empty.
|
||
213 | */
|
||
214 | public static String getText(String key, boolean log) { |
||
215 | return getText(key, _CLASSNAME, log);
|
||
216 | } |
||
217 | |||
218 | public static String getText(String key, String[] arguments, boolean log) { |
||
219 | String translation = getText(key, _CLASSNAME, log);
|
||
220 | if (translation!=null && arguments!=null ) { |
||
221 | try {
|
||
222 | translation = MessageFormat.format(translation, arguments);
|
||
223 | } catch (IllegalFormatException ex) { |
||
224 | if (log) {
|
||
225 | logger.error(_CLASSNAME+" -- Error formating key: "+key+" -- "+translation); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | return translation;
|
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | * <p>Gets the localized message associated with the provided key.
|
||
234 | * If the key is not in the dictionary, it returns null and the failure
|
||
235 | * is only registered in the log if the param log is true.</p>
|
||
236 | *
|
||
237 | * @param key An String which identifies the translation that we want to get.
|
||
238 | * @param callerName A symbolic name given to the caller of this method, to
|
||
239 | * show it in the log if the key was not found
|
||
240 | * @param log Determines whether log a key failure or not
|
||
241 | * @return an String with the message associated with the provided key,
|
||
242 | * or null if the key is not in the dictionary.
|
||
243 | */
|
||
244 | public static String getText(String key, String callerName, boolean log) { |
||
245 | if (key==null) { |
||
246 | return null; |
||
247 | } |
||
248 | for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
||
249 | // try to get the translation for any of the languagues in the preferred languages list
|
||
250 | String translation = ((Properties)localeResources.get(numLocale)).getProperty(key); |
||
251 | if (translation!=null && !translation.equals("")) { |
||
252 | return translation;
|
||
253 | } |
||
254 | } |
||
255 | if (log) {
|
||
256 | 41030 | jjdelcerro | logger.info("["+callerName+"] Cannot find translation for key '"+key+"'."); |
257 | 40435 | jjdelcerro | } |
258 | return null; |
||
259 | } |
||
260 | |||
261 | public static String getText(String key, String[] arguments, String callerName, boolean log) { |
||
262 | String translation = getText(key, callerName, log);
|
||
263 | if (translation!=null) { |
||
264 | try {
|
||
265 | translation = MessageFormat.format(translation, arguments);
|
||
266 | } |
||
267 | catch (IllegalFormatException ex) { |
||
268 | if (log) {
|
||
269 | logger.error(callerName+" -- Error formating key: "+key+" -- "+translation); |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | return translation;
|
||
274 | } |
||
275 | |||
276 | /**
|
||
277 | * <p>Adds an additional family of resource files containing some translations.
|
||
278 | * A family is a group of files with a common baseName.
|
||
279 | * The file must be an iso-8859-1 encoded file, which can contain any unicode
|
||
280 | * character using unicode escaped sequences, and following the syntax:
|
||
281 | * <code>key1=value1
|
||
282 | * key2=value2</code>
|
||
283 | * where 'key1' is the key used to identify the string and must not
|
||
284 | * contain the '=' symbol, and 'value1' is the associated translation.</p>
|
||
285 | * <p<For example:</p>
|
||
286 | * <code>cancel=Cancelar
|
||
287 | * accept=Aceptar</code>
|
||
288 | * <p>Only one pair key-value is allowed per line.</p>
|
||
289 | *
|
||
290 | * <p>The actual name of the resource file to load is determined using the rules
|
||
291 | * explained in the class java.util.ResourceBundle. Summarizing, for each language
|
||
292 | * in the specified preferred locales list it will try to load a file with
|
||
293 | * the following structure: <code>family_locale.properties</code></p>
|
||
294 | *
|
||
295 | * <p>For example, if the preferred locales list contains {"fr", "es", "en"}, and
|
||
296 | * the family name is "text", it will try to load the files "text_fr.properties",
|
||
297 | * "text_es.properties" and finally "text_en.properties".</p>
|
||
298 | *
|
||
299 | * <p>Locales might be more specific, such us "es_AR" (meaning Spanish from Argentina)
|
||
300 | * or "es_AR_linux" (meaning Linux system preferring Spanish from Argentina). In the
|
||
301 | * later case, it will try to load "text_es_AR_linux.properties", then
|
||
302 | * "text_es_AR.properties" if the former fails, and finally "text_es.properties".</p>
|
||
303 | *
|
||
304 | * <p>The directory used to locate the resource file is determining by using the
|
||
305 | * getResource method from the provided ClassLoader.</p>
|
||
306 | *
|
||
307 | * @param family The family name (or base name) which is used to search
|
||
308 | * actual properties files.
|
||
309 | * @param loader A ClassLoader which is able to find a property file matching
|
||
310 | * the specified family name and the preferred locales
|
||
311 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
312 | */
|
||
313 | public static void addResourceFamily(String family, ClassLoader loader) { |
||
314 | addResourceFamily(family, loader, "");
|
||
315 | } |
||
316 | |||
317 | /**
|
||
318 | * <p>Adds an additional family of resource files containing some translations.
|
||
319 | * The search path to locate the files is provided by the dirList parameter.</p>
|
||
320 | *
|
||
321 | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
||
322 | * format of the property files and the way to determine the candidat files
|
||
323 | * to load. Note that those methods are different in the way to locate the
|
||
324 | * candidat files. This method searches in the provided paths (<code>dirList</code>
|
||
325 | * parameter), while the referred method searches using the getResource method
|
||
326 | * of the provided ClassLoader.</p>
|
||
327 | *
|
||
328 | * @param family The family name (or base name) which is used to search
|
||
329 | * actual properties files.
|
||
330 | * @param dirList A list of search paths to locate the property files
|
||
331 | * @throws MalformedURLException
|
||
332 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
333 | */
|
||
334 | public static void addResourceFamily(String family, File[] dirList) throws MalformedURLException{ |
||
335 | // use our own classloader
|
||
336 | URL[] urls = new URL[dirList.length]; |
||
337 | |||
338 | int i;
|
||
339 | for (i=0; i<urls.length; i++) { |
||
340 | urls[i] = dirList[i].toURL(); |
||
341 | } |
||
342 | |||
343 | ClassLoader loader = new MessagesClassLoader(urls); |
||
344 | addResourceFamily(family, loader, "");
|
||
345 | } |
||
346 | |||
347 | /**
|
||
348 | * <p>Adds an additional family of resource files containing some translations.
|
||
349 | * The search path to locate the files is provided by the dir parameter.</p>
|
||
350 | *
|
||
351 | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
||
352 | * format of the property files and the way to determine the candidat files
|
||
353 | * to load. Note that those methods are different in the way to locate the
|
||
354 | * candidat files. This method searches in the provided path (<code>dir</code>
|
||
355 | * parameter), while the referred method searches using the getResource method
|
||
356 | * of the provided ClassLoader.</p>
|
||
357 | *
|
||
358 | * @param family The family name (or base name) which is used to search
|
||
359 | * actual properties files.
|
||
360 | * @param dir The search path to locate the property files
|
||
361 | * @throws MalformedURLException
|
||
362 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
363 | */
|
||
364 | public static void addResourceFamily(String family, File dir) throws MalformedURLException{ |
||
365 | // use our own classloader
|
||
366 | URL[] urls = new URL[1]; |
||
367 | urls[0] = dir.toURL();
|
||
368 | ClassLoader loader = new MessagesClassLoader(urls); |
||
369 | addResourceFamily(family, loader, "");
|
||
370 | } |
||
371 | |||
372 | |||
373 | /**
|
||
374 | * <p>Adds an additional family of resource files containing some translations.
|
||
375 | * The search path is determined by the getResource method from the
|
||
376 | * provided ClassLoader.</p>
|
||
377 | *
|
||
378 | * <p>This method is identical to {@link addResourceFamily(String, ClassLoader)},
|
||
379 | * except that it adds a <pode>callerName</code> parameter to show in the log.</p>
|
||
380 | *
|
||
381 | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
||
382 | * format of the property files andthe way to determine the candidat files
|
||
383 | * to load.</p>
|
||
384 | *
|
||
385 | * @param family The family name (or base name) which is used to search
|
||
386 | * actual properties files.
|
||
387 | * @param loader A ClassLoader which is able to find a property file matching
|
||
388 | * the specified family name and the preferred locales
|
||
389 | * @param callerName A symbolic name given to the caller of this method, to
|
||
390 | * show it in the log if there is an error
|
||
391 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
392 | */
|
||
393 | public static void addResourceFamily(String family, ClassLoader loader, String callerName) { |
||
394 | 41030 | jjdelcerro | // String currentKey;
|
395 | // Enumeration keys;
|
||
396 | 40435 | jjdelcerro | Locale lang;
|
397 | 41030 | jjdelcerro | // Properties properties;
|
398 | Properties translations;
|
||
399 | 40435 | jjdelcerro | int totalLocales = preferredLocales.size();
|
400 | |||
401 | if (totalLocales == 0) { |
||
402 | // if it's empty, warn about that
|
||
403 | logger.warn("There is not preferred languages list. Maybe the Messages class was not initialized");
|
||
404 | } |
||
405 | |||
406 | 41314 | jjdelcerro | familyDescriptors.add( new FamilyDescriptor(family,loader,callerName));
|
407 | |||
408 | 40435 | jjdelcerro | resourceFamilies.add(family); |
409 | classLoaders.add(loader); |
||
410 | |||
411 | for (int numLocale=0; numLocale<totalLocales; numLocale++) { // for each language |
||
412 | 41030 | jjdelcerro | // properties = new Properties();
|
413 | 40435 | jjdelcerro | |
414 | lang = (Locale) preferredLocales.get(numLocale);
|
||
415 | translations = (Properties) localeResources.get(numLocale);
|
||
416 | |||
417 | addResourceFamily(lang, translations, family, loader, callerName); |
||
418 | } |
||
419 | } |
||
420 | 41314 | jjdelcerro | |
421 | 40435 | jjdelcerro | private static void addResourceFamily(Locale lang, Properties translations, |
422 | String family, ClassLoader loader, String callerName) { |
||
423 | 41314 | jjdelcerro | //logger.debug("addResourceFamily "+lang.toString()+", "+family+", "+loader.toString());
|
424 | |||
425 | 40435 | jjdelcerro | Properties properties = new Properties(); |
426 | 41044 | jjdelcerro | String langCode = lang.toString();
|
427 | String resource = family.replace('.', '/') + "_" + langCode + ".properties"; |
||
428 | 41314 | jjdelcerro | URL resourceURL = loader.getResource(resource);
|
429 | 40435 | jjdelcerro | InputStream is = loader.getResourceAsStream(resource);
|
430 | 41044 | jjdelcerro | if( is==null && langCode.contains("_") ) { |
431 | try {
|
||
432 | langCode = langCode.split("_")[0]; |
||
433 | resource = family.replace('.', '/') + "_" + langCode + ".properties"; |
||
434 | 41314 | jjdelcerro | resourceURL = loader.getResource(resource); |
435 | 41044 | jjdelcerro | is = loader.getResourceAsStream(resource); |
436 | if( is==null ) { |
||
437 | resource = family.replace('.', '/') + ".properties"; |
||
438 | 41314 | jjdelcerro | resourceURL = loader.getResource(resource); |
439 | 41044 | jjdelcerro | is = loader.getResourceAsStream(resource); |
440 | } |
||
441 | } catch(Exception ex) { |
||
442 | // Do nothing, is are null and are handled later
|
||
443 | } |
||
444 | } |
||
445 | 40435 | jjdelcerro | if (is != null) { |
446 | try {
|
||
447 | properties.load(is); |
||
448 | } catch (IOException e) { |
||
449 | } |
||
450 | } else if (lang.equals(baseLocale)) { |
||
451 | // try also "text.properties" for the base language
|
||
452 | is = loader.getResourceAsStream(family.replace('.', '/') |
||
453 | + ".properties");
|
||
454 | |||
455 | |||
456 | if (is != null) { |
||
457 | try {
|
||
458 | properties.load(is); |
||
459 | } catch (IOException e) { |
||
460 | } |
||
461 | } |
||
462 | |||
463 | } |
||
464 | 41314 | jjdelcerro | if( resourceURL!=null && logger.isDebugEnabled() ) { |
465 | logger.debug("Load resources from '"+resourceURL.toString()+"' with classloader {"+loader.toString()+"}."); |
||
466 | } |
||
467 | 40435 | jjdelcerro | Enumeration keys = properties.keys();
|
468 | while (keys.hasMoreElements()) {
|
||
469 | String currentKey = (String) keys.nextElement(); |
||
470 | if (!translations.containsKey(currentKey)) {
|
||
471 | 41314 | jjdelcerro | translations.put(currentKey, properties.getProperty(currentKey)); |
472 | 40435 | jjdelcerro | } |
473 | } |
||
474 | |||
475 | } |
||
476 | |||
477 | /**
|
||
478 | * <p>Adds an additional family of resource files containing some translations.</p>
|
||
479 | *
|
||
480 | * <p>This method is identical to {@link addResourceFamily(String, ClassLoader, String)},
|
||
481 | * except that it uses the caller's class loader.</p>
|
||
482 | *
|
||
483 | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
||
484 | * format of the property files and the way to determine the candidat files
|
||
485 | * to load.</p>
|
||
486 | *
|
||
487 | * @param family The family name (or base name) which is used to search
|
||
488 | * actual properties files.
|
||
489 | * @param callerName A symbolic name given to the caller of this method, to
|
||
490 | * show it in the log if there is an error. This is only used
|
||
491 | * to show
|
||
492 | * something meaningful in the log, so you can use any string
|
||
493 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
494 | */
|
||
495 | public static void addResourceFamily(String family, String callerName) { |
||
496 | addResourceFamily(family, Messages.class.getClassLoader(), callerName); |
||
497 | } |
||
498 | |||
499 | |||
500 | /**
|
||
501 | * Returns an ArrayList containing the ordered list of prefered Locales
|
||
502 | * Each element of the ArrayList is a Locale object.
|
||
503 | *
|
||
504 | * @return an ArrayList containing the ordered list of prefered Locales
|
||
505 | * Each element of the ArrayList is a Locale object.
|
||
506 | */
|
||
507 | public static ArrayList getPreferredLocales() { |
||
508 | return preferredLocales;
|
||
509 | } |
||
510 | |||
511 | /**
|
||
512 | * <p>Sets the ordered list of preferred locales.
|
||
513 | * Each element of the ArrayList is a Locale object.</p>
|
||
514 | *
|
||
515 | * <p>Note that calling this method does not load any translation, it just
|
||
516 | * adds the language to the preferred locales list, so this method must
|
||
517 | * be always called before the translations are loaded using
|
||
518 | * the addResourceFamily() methods.</p>
|
||
519 | *
|
||
520 | * <p>It there was any language in the preferred locale list, the language
|
||
521 | * and its associated translations are deleted.</p>
|
||
522 | *
|
||
523 | *
|
||
524 | * @param preferredLocales an ArrayList containing Locale objects.
|
||
525 | * The ArrayList represents an ordered list of preferred locales
|
||
526 | */
|
||
527 | public static void setPreferredLocales(ArrayList preferredLocalesList) { |
||
528 | 41314 | jjdelcerro | logger.info("setPreferredLocales "+preferredLocalesList.toString());
|
529 | 40435 | jjdelcerro | // delete all existing locales
|
530 | Iterator oldLocales = preferredLocales.iterator();
|
||
531 | while (oldLocales.hasNext()) {
|
||
532 | removeLocale((Locale) oldLocales.next());
|
||
533 | } |
||
534 | |||
535 | // add the new locales now
|
||
536 | for (int numLocale=0; numLocale < preferredLocalesList.size(); numLocale++) { |
||
537 | addLocale((Locale) preferredLocalesList.get(numLocale));
|
||
538 | } |
||
539 | } |
||
540 | |||
541 | 41314 | jjdelcerro | public static Locale getCurrentLocale() { |
542 | return currentLocale;
|
||
543 | } |
||
544 | |||
545 | public static void setCurrentLocale(Locale locale) { |
||
546 | logger.info("setCurrentLocale "+locale.toString());
|
||
547 | |||
548 | resourceFamilies = new HashSet(); |
||
549 | classLoaders = new HashSet(); |
||
550 | localeResources = new ArrayList(); |
||
551 | preferredLocales = new ArrayList(); |
||
552 | |||
553 | addLocale(locale); |
||
554 | String localeStr = locale.getLanguage();
|
||
555 | if ( localeStr.equals("es") || localeStr.equals("ca") |
||
556 | || localeStr.equals("gl") || localeStr.equals("eu") |
||
557 | || localeStr.equals("va") ) {
|
||
558 | // prefer Spanish for languages spoken in Spain
|
||
559 | addLocale(new Locale("es")); |
||
560 | addLocale(new Locale("en")); |
||
561 | } else {
|
||
562 | // prefer English for the rest
|
||
563 | addLocale(new Locale("en")); |
||
564 | addLocale(new Locale("es")); |
||
565 | } |
||
566 | |||
567 | for( int curlocale=0; curlocale<preferredLocales.size(); curlocale++) { |
||
568 | for( int curfamily=0; curfamily<familyDescriptors.size(); curfamily++) { |
||
569 | FamilyDescriptor family = (FamilyDescriptor) familyDescriptors.get(curfamily); |
||
570 | addResourceFamily( |
||
571 | (Locale) preferredLocales.get(curlocale),
|
||
572 | (Properties) localeResources.get(curlocale),
|
||
573 | family.family, |
||
574 | family.loader, |
||
575 | family.callerName); |
||
576 | } |
||
577 | } |
||
578 | currentLocale = locale; |
||
579 | Locale.setDefault(locale);
|
||
580 | } |
||
581 | |||
582 | 40435 | jjdelcerro | /**
|
583 | * Adds a Locale at the end of the ordered list of preferred locales.
|
||
584 | * Note that calling this method does not load any translation, it just
|
||
585 | * adds the language to the preferred locales list, so this method must
|
||
586 | * be always called before the translations are loaded using
|
||
587 | * the addResourceFamily() methods.
|
||
588 | *
|
||
589 | * @param lang A Locale object specifying the locale to add
|
||
590 | */
|
||
591 | public static void addLocale(Locale lang) { |
||
592 | if (!preferredLocales.contains(lang)) { // avoid duplicates |
||
593 | 41314 | jjdelcerro | logger.info("addLocale "+lang.toString());
|
594 | preferredLocales.add(lang); // add the lang to the ordered list of preferred locales
|
||
595 | Properties dict = new Properties(); |
||
596 | localeResources.add(dict); // add a hashmap which will contain the translation for this language
|
||
597 | 40435 | jjdelcerro | } |
598 | } |
||
599 | |||
600 | /**
|
||
601 | * Removes the specified Locale from the list of preferred locales and the
|
||
602 | * translations associated with this locale.
|
||
603 | *
|
||
604 | * @param lang A Locale object specifying the locale to remove
|
||
605 | * @return True if the locale was in the preferred locales list, false otherwise
|
||
606 | */
|
||
607 | public static boolean removeLocale(Locale lang) { |
||
608 | int numLocale = preferredLocales.indexOf(lang);
|
||
609 | if (numLocale!=-1) { // we found the locale in the list |
||
610 | try {
|
||
611 | preferredLocales.remove(numLocale); |
||
612 | localeResources.remove(numLocale); |
||
613 | } |
||
614 | catch (IndexOutOfBoundsException ex) { |
||
615 | 41030 | jjdelcerro | logger.warn(_CLASSNAME + "." + "removeLocale: " + ex.getLocalizedMessage(), ex); |
616 | 40435 | jjdelcerro | } |
617 | return true; |
||
618 | } |
||
619 | return false; |
||
620 | } |
||
621 | |||
622 | /**
|
||
623 | * Cleans the translation tables (removes all the translations from memory).
|
||
624 | */
|
||
625 | public static void removeResources() { |
||
626 | for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
||
627 | ((Properties)localeResources.get(numLocale)).clear();
|
||
628 | } |
||
629 | } |
||
630 | |||
631 | /**
|
||
632 | * The number of translation keys which have been loaded till now
|
||
633 | * (In other words: the number of available translation strings).
|
||
634 | *
|
||
635 | * @param lang The language for which we want to know the number of translation keys
|
||
636 | * return The number of translation keys for the provided language.
|
||
637 | */
|
||
638 | protected static int size(Locale lang) { |
||
639 | int numLocale = preferredLocales.indexOf(lang);
|
||
640 | if (numLocale!=-1) { |
||
641 | return ((Properties)localeResources.get(numLocale)).size(); |
||
642 | }; |
||
643 | return 0; |
||
644 | } |
||
645 | |||
646 | protected static Set keySet(Locale lang) { |
||
647 | int numLocale = preferredLocales.indexOf(lang);
|
||
648 | if (numLocale!=-1) { |
||
649 | return ((Properties)localeResources.get(numLocale)).keySet(); |
||
650 | } else {
|
||
651 | return null; |
||
652 | } |
||
653 | } |
||
654 | |||
655 | /**
|
||
656 | * Checks if some locale has been added to the preferred locales
|
||
657 | * list, which is necessary before loading any translation because
|
||
658 | * only the translations for the preferred locales are loaded.
|
||
659 | *
|
||
660 | * @return
|
||
661 | */
|
||
662 | public static boolean hasLocales() { |
||
663 | return preferredLocales.size()>0; |
||
664 | } |
||
665 | |||
666 | /**
|
||
667 | * Gets the base language, the language considered the origin of
|
||
668 | * translations, which will be (possibly) stored in a property
|
||
669 | * file without language suffix
|
||
670 | * (ie: text.properties instead of text_es.properties).
|
||
671 | */
|
||
672 | public static String getBaseLanguage() { |
||
673 | return baseLanguage;
|
||
674 | } |
||
675 | |||
676 | /**
|
||
677 | * Sets the base language, the language considered the origin of
|
||
678 | * translations, which will be (possibly)
|
||
679 | * stored in a property file without language suffix
|
||
680 | * (ie: text.properties instead of text_es.properties).
|
||
681 | *
|
||
682 | * @param lang The base language to be set
|
||
683 | */
|
||
684 | public static void setBaseLanguage(String lang) { |
||
685 | baseLanguage = lang; |
||
686 | baseLocale = new Locale(baseLanguage); |
||
687 | } |
||
688 | |||
689 | /*
|
||
690 | * Searches the subdirectories of the provided directory, finding
|
||
691 | * all the translation files, and constructing a list of available translations.
|
||
692 | * It reports different country codes or variants, if available.
|
||
693 | * For example, if there is an en_US translation and an en_GB translation, both
|
||
694 | * locales will be present in the Vector.
|
||
695 | *
|
||
696 | * @return
|
||
697 | */
|
||
698 | |||
699 | /**
|
||
700 | *
|
||
701 | * @return A Vector containing the available locales. Each element is a Locale object
|
||
702 | */
|
||
703 | /*public static Vector getAvailableLocales() {
|
||
704 | return _availableLocales;
|
||
705 | }*/
|
||
706 | |||
707 | /**
|
||
708 | *
|
||
709 | * @return A Vector containing the available languages. Each element is an String object
|
||
710 | */
|
||
711 | /*public static Vector getAvailableLanguages() {
|
||
712 | Vector availableLanguages = new Vector();
|
||
713 | Locale lang;
|
||
714 | Enumeration locales = _availableLocales.elements();
|
||
715 | while (locales.hasMoreElements()) {
|
||
716 | lang = (Locale) locales.nextElement();
|
||
717 | availableLanguages.add(lang.getLanguage());
|
||
718 | }
|
||
719 | return availableLanguages;
|
||
720 | }*/
|
||
721 | |||
722 | public static Properties getAllTexts(Locale lang) { |
||
723 | Properties texts = new Properties(); |
||
724 | getAllTexts(lang, null, texts);
|
||
725 | for (Iterator iterator = classLoaders.iterator(); iterator.hasNext();) { |
||
726 | getAllTexts(lang, (ClassLoader) iterator.next(), texts);
|
||
727 | } |
||
728 | return texts;
|
||
729 | } |
||
730 | |||
731 | private static void getAllTexts(Locale lang, ClassLoader classLoader, |
||
732 | Properties texts) {
|
||
733 | ClassLoader loader = classLoader == null ? Messages.class |
||
734 | .getClassLoader() : classLoader; |
||
735 | |||
736 | for (Iterator iterator = resourceFamilies.iterator(); iterator |
||
737 | .hasNext();) { |
||
738 | String family = (String) iterator.next(); |
||
739 | addResourceFamily(lang, texts, family, loader, |
||
740 | "Messages.getAllTexts");
|
||
741 | } |
||
742 | } |
||
743 | |||
744 | 40596 | jjdelcerro | } |