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