Revision 28469
branches/v2_0_0_prep/libraries/libInternationalization/src/org/gvsig/i18n/Messages.java | ||
---|---|---|
49 | 49 |
import java.text.MessageFormat; |
50 | 50 |
import java.util.ArrayList; |
51 | 51 |
import java.util.Enumeration; |
52 |
import java.util.HashSet; |
|
52 | 53 |
import java.util.IllegalFormatException; |
53 | 54 |
import java.util.Iterator; |
54 | 55 |
import java.util.Locale; |
... | ... | |
95 | 96 |
private static ArrayList localeResources = new ArrayList(); |
96 | 97 |
private static ArrayList preferredLocales = new ArrayList(); // contains the ordered list of prefered languages/locales (class Locale) |
97 | 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 |
|
|
98 | 104 |
/* |
99 | 105 |
* The language considered the origin of translations, which will |
100 | 106 |
* (possibly) be stored in a property file without language suffix |
101 | 107 |
* (ie: text.properties instead of text_es.properties). |
102 | 108 |
*/ |
103 | 109 |
private static String baseLanguage = "es"; |
110 |
private static Locale baseLocale = new Locale(baseLanguage); |
|
104 | 111 |
|
105 | 112 |
/** |
106 | 113 |
* <p>Gets the localized message associated with the provided key. |
... | ... | |
121 | 128 |
* is null, return null. |
122 | 129 |
*/ |
123 | 130 |
public static String getText(String key, String callerName) { |
124 |
if (key==null) return null; |
|
131 |
if (key==null) { |
|
132 |
return null; |
|
133 |
} |
|
125 | 134 |
for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
126 | 135 |
// try to get the translation for any of the languagues in the preferred languages list |
127 | 136 |
String translation = ((Properties)localeResources.get(numLocale)).getProperty(key); |
128 |
if (translation!=null && !translation.equals("")) |
|
137 |
if (translation!=null && !translation.equals("")) {
|
|
129 | 138 |
return translation; |
139 |
} |
|
130 | 140 |
} |
131 | 141 |
logger.warn(callerName+ " -- Cannot find translation for "+key); |
132 | 142 |
return key; |
... | ... | |
208 | 218 |
* or null if the key is not in the dictionary. |
209 | 219 |
*/ |
210 | 220 |
public static String getText(String key, String callerName, boolean log) { |
211 |
if (key==null) return null; |
|
221 |
if (key==null) { |
|
222 |
return null; |
|
223 |
} |
|
212 | 224 |
for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
213 | 225 |
// try to get the translation for any of the languagues in the preferred languages list |
214 | 226 |
String translation = ((Properties)localeResources.get(numLocale)).getProperty(key); |
215 |
if (translation!=null && !translation.equals("")) |
|
227 |
if (translation!=null && !translation.equals("")) {
|
|
216 | 228 |
return translation; |
229 |
} |
|
217 | 230 |
} |
218 | 231 |
if (log) { |
219 | 232 |
logger.warn(callerName+" -- Cannot find translation for "+key); |
... | ... | |
356 | 369 |
public static void addResourceFamily(String family, ClassLoader loader, String callerName) { |
357 | 370 |
String currentKey; |
358 | 371 |
Enumeration keys; |
359 |
String lang;
|
|
372 |
Locale lang;
|
|
360 | 373 |
Properties properties, translations; |
361 | 374 |
int totalLocales = preferredLocales.size(); |
362 | 375 |
|
... | ... | |
365 | 378 |
logger.warn("There is not preferred languages list. Maybe the Messages class was not initialized"); |
366 | 379 |
} |
367 | 380 |
|
381 |
resourceFamilies.add(family); |
|
382 |
classLoaders.add(loader); |
|
368 | 383 |
|
369 | 384 |
for (int numLocale=0; numLocale<totalLocales; numLocale++) { // for each language |
370 | 385 |
properties = new Properties(); |
371 |
lang = ((Locale) preferredLocales.get(numLocale)).getLanguage(); |
|
372 |
InputStream is = loader.getResourceAsStream(family.replace('.','/')+"_"+lang+".properties"); |
|
386 |
|
|
387 |
lang = (Locale) preferredLocales.get(numLocale); |
|
388 |
translations = (Properties) localeResources.get(numLocale); |
|
389 |
|
|
390 |
addResourceFamily(lang, translations, family, loader, callerName); |
|
391 |
} |
|
392 |
} |
|
393 |
|
|
394 |
private static void addResourceFamily(Locale lang, Properties translations, |
|
395 |
String family, ClassLoader loader, String callerName) { |
|
396 |
Properties properties = new Properties(); |
|
397 |
String resource = family.replace('.', '/') + "_" + lang.toString() |
|
398 |
+ ".properties"; |
|
399 |
InputStream is = loader.getResourceAsStream(resource); |
|
400 |
if (is != null) { |
|
401 |
try { |
|
402 |
properties.load(is); |
|
403 |
} catch (IOException e) { |
|
404 |
} |
|
405 |
} else if (lang.equals(baseLocale)) { |
|
406 |
// try also "text.properties" for the base language |
|
407 |
is = loader.getResourceAsStream(family.replace('.', '/') |
|
408 |
+ ".properties"); |
|
409 |
|
|
410 |
|
|
373 | 411 |
if (is != null) { |
374 | 412 |
try { |
375 | 413 |
properties.load(is); |
376 | 414 |
} catch (IOException e) { |
377 | 415 |
} |
378 | 416 |
} |
379 |
else if (lang.equals(baseLanguage)) { |
|
380 |
// try also "text.properties" for the base language |
|
381 |
is = loader.getResourceAsStream(family.replace('.','/')+".properties"); |
|
382 |
if (is!=null) { |
|
383 |
try { |
|
384 |
properties.load(is); |
|
385 |
} catch (IOException e) { |
|
386 |
} |
|
387 |
} |
|
388 |
} |
|
389 | 417 |
|
390 |
translations = (Properties)localeResources.get(numLocale);
|
|
391 |
keys = properties.keys();
|
|
392 |
while (keys.hasMoreElements()) {
|
|
393 |
currentKey = (String) keys.nextElement();
|
|
394 |
if (! translations.containsKey(currentKey)) {
|
|
395 |
translations.put(currentKey, properties.getProperty(currentKey));
|
|
396 |
}
|
|
418 |
}
|
|
419 |
Enumeration keys = properties.keys();
|
|
420 |
while (keys.hasMoreElements()) { |
|
421 |
String currentKey = (String) keys.nextElement();
|
|
422 |
if (!translations.containsKey(currentKey)) {
|
|
423 |
translations
|
|
424 |
.put(currentKey, properties.getProperty(currentKey));
|
|
397 | 425 |
} |
398 | 426 |
} |
427 |
|
|
399 | 428 |
} |
400 | 429 |
|
401 | 430 |
/** |
... | ... | |
528 | 557 |
int numLocale = preferredLocales.indexOf(lang); |
529 | 558 |
if (numLocale!=-1) { |
530 | 559 |
return ((Properties)localeResources.get(numLocale)).keySet(); |
560 |
} else { |
|
561 |
return null; |
|
531 | 562 |
} |
532 |
else |
|
533 |
return null; |
|
534 | 563 |
} |
535 | 564 |
|
536 | 565 |
/** |
... | ... | |
564 | 593 |
*/ |
565 | 594 |
public static void setBaseLanguage(String lang) { |
566 | 595 |
baseLanguage = lang; |
596 |
baseLocale = new Locale(baseLanguage); |
|
567 | 597 |
} |
568 | 598 |
|
569 | 599 |
/* |
... | ... | |
598 | 628 |
} |
599 | 629 |
return availableLanguages; |
600 | 630 |
}*/ |
631 |
|
|
632 |
public static Properties getAllTexts(Locale lang) { |
|
633 |
Properties texts = new Properties(); |
|
634 |
getAllTexts(lang, null, texts); |
|
635 |
for (Iterator iterator = classLoaders.iterator(); iterator.hasNext();) { |
|
636 |
getAllTexts(lang, (ClassLoader) iterator.next(), texts); |
|
637 |
} |
|
638 |
return texts; |
|
639 |
} |
|
640 |
|
|
641 |
private static void getAllTexts(Locale lang, ClassLoader classLoader, |
|
642 |
Properties texts) { |
|
643 |
ClassLoader loader = classLoader == null ? Messages.class |
|
644 |
.getClassLoader() : classLoader; |
|
645 |
|
|
646 |
for (Iterator iterator = resourceFamilies.iterator(); iterator |
|
647 |
.hasNext();) { |
|
648 |
String family = (String) iterator.next(); |
|
649 |
addResourceFamily(lang, texts, family, loader, |
|
650 |
"Messages.getAllTexts"); |
|
651 |
} |
|
652 |
} |
|
653 |
|
|
601 | 654 |
} |
Also available in: Unified diff