Revision 41314 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.i18n/src/main/java/org/gvsig/i18n/Messages.java
Messages.java | ||
---|---|---|
35 | 35 |
import java.util.HashSet; |
36 | 36 |
import java.util.IllegalFormatException; |
37 | 37 |
import java.util.Iterator; |
38 |
import java.util.List; |
|
38 | 39 |
import java.util.Locale; |
39 | 40 |
import java.util.Properties; |
40 | 41 |
import java.util.Set; |
... | ... | |
69 | 70 |
* |
70 | 71 |
*/ |
71 | 72 |
public class Messages { |
73 |
|
|
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 |
|
|
72 | 86 |
private static Logger logger = LoggerFactory.getLogger("Messages"); |
73 | 87 |
private static String _CLASSNAME = "org.gvsig.i18n.Messages"; |
88 |
private static Locale currentLocale; |
|
74 | 89 |
|
75 | 90 |
/* Each entry will contain a hashmap with translations. Each hasmap |
76 | 91 |
* contains the translations for one language, indexed by the |
... | ... | |
84 | 99 |
private static Set resourceFamilies = new HashSet(); |
85 | 100 |
private static Set classLoaders = new HashSet(); |
86 | 101 |
|
102 |
private static List familyDescriptors = new ArrayList(); |
|
103 |
|
|
87 | 104 |
/* |
88 | 105 |
* The language considered the origin of translations, which will |
89 | 106 |
* (possibly) be stored in a property file without language suffix |
... | ... | |
386 | 403 |
logger.warn("There is not preferred languages list. Maybe the Messages class was not initialized"); |
387 | 404 |
} |
388 | 405 |
|
406 |
familyDescriptors.add( new FamilyDescriptor(family,loader,callerName)); |
|
407 |
|
|
389 | 408 |
resourceFamilies.add(family); |
390 | 409 |
classLoaders.add(loader); |
391 | 410 |
|
... | ... | |
398 | 417 |
addResourceFamily(lang, translations, family, loader, callerName); |
399 | 418 |
} |
400 | 419 |
} |
401 |
|
|
420 |
|
|
402 | 421 |
private static void addResourceFamily(Locale lang, Properties translations, |
403 | 422 |
String family, ClassLoader loader, String callerName) { |
423 |
//logger.debug("addResourceFamily "+lang.toString()+", "+family+", "+loader.toString()); |
|
424 |
|
|
404 | 425 |
Properties properties = new Properties(); |
405 | 426 |
String langCode = lang.toString(); |
406 | 427 |
String resource = family.replace('.', '/') + "_" + langCode + ".properties"; |
428 |
URL resourceURL = loader.getResource(resource); |
|
407 | 429 |
InputStream is = loader.getResourceAsStream(resource); |
408 | 430 |
if( is==null && langCode.contains("_") ) { |
409 | 431 |
try { |
410 | 432 |
langCode = langCode.split("_")[0]; |
411 | 433 |
resource = family.replace('.', '/') + "_" + langCode + ".properties"; |
434 |
resourceURL = loader.getResource(resource); |
|
412 | 435 |
is = loader.getResourceAsStream(resource); |
413 | 436 |
if( is==null ) { |
414 | 437 |
resource = family.replace('.', '/') + ".properties"; |
438 |
resourceURL = loader.getResource(resource); |
|
415 | 439 |
is = loader.getResourceAsStream(resource); |
416 | 440 |
} |
417 | 441 |
} catch(Exception ex) { |
... | ... | |
437 | 461 |
} |
438 | 462 |
|
439 | 463 |
} |
464 |
if( resourceURL!=null && logger.isDebugEnabled() ) { |
|
465 |
logger.debug("Load resources from '"+resourceURL.toString()+"' with classloader {"+loader.toString()+"}."); |
|
466 |
} |
|
440 | 467 |
Enumeration keys = properties.keys(); |
441 | 468 |
while (keys.hasMoreElements()) { |
442 | 469 |
String currentKey = (String) keys.nextElement(); |
443 | 470 |
if (!translations.containsKey(currentKey)) { |
444 |
translations |
|
445 |
.put(currentKey, properties.getProperty(currentKey)); |
|
471 |
translations.put(currentKey, properties.getProperty(currentKey)); |
|
446 | 472 |
} |
447 | 473 |
} |
448 | 474 |
|
... | ... | |
499 | 525 |
* The ArrayList represents an ordered list of preferred locales |
500 | 526 |
*/ |
501 | 527 |
public static void setPreferredLocales(ArrayList preferredLocalesList) { |
528 |
logger.info("setPreferredLocales "+preferredLocalesList.toString()); |
|
502 | 529 |
// delete all existing locales |
503 | 530 |
Iterator oldLocales = preferredLocales.iterator(); |
504 | 531 |
while (oldLocales.hasNext()) { |
... | ... | |
511 | 538 |
} |
512 | 539 |
} |
513 | 540 |
|
541 |
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 |
|
|
514 | 582 |
/** |
515 | 583 |
* Adds a Locale at the end of the ordered list of preferred locales. |
516 | 584 |
* Note that calling this method does not load any translation, it just |
... | ... | |
522 | 590 |
*/ |
523 | 591 |
public static void addLocale(Locale lang) { |
524 | 592 |
if (!preferredLocales.contains(lang)) { // avoid duplicates |
525 |
preferredLocales.add(lang); // add the lang to the ordered list of preferred locales |
|
526 |
Properties dict = new Properties(); |
|
527 |
localeResources.add(dict); // add a hashmap which will contain the translation for this language |
|
593 |
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 |
|
528 | 597 |
} |
529 | 598 |
} |
530 | 599 |
|
Also available in: Unified diff