Revision 32337 branches/v2_0_0_prep/extensions/extI18n/src/main/java/org/gvsig/i18n/impl/I18nManagerImpl.java
I18nManagerImpl.java | ||
---|---|---|
61 | 61 |
import org.gvsig.utils.StringUtilities; |
62 | 62 |
import org.gvsig.utils.XMLEntity; |
63 | 63 |
|
64 |
|
|
65 | 64 |
/** |
66 | 65 |
* Implementation of the I18nManager interface. |
67 | 66 |
* |
... | ... | |
82 | 81 |
|
83 | 82 |
private static final String LANGUAGE = "language"; |
84 | 83 |
|
85 |
private static final String REGISTERED_LOCALES_PERSISTENCE = "RegisteredLocales"; |
|
84 |
private static final String REGISTERED_LOCALES_PERSISTENCE = |
|
85 |
"RegisteredLocales"; |
|
86 | 86 |
|
87 | 87 |
private static final I18nManager DEFAULT = new I18nManagerImpl(); |
88 | 88 |
|
... | ... | |
95 | 95 |
private Locale[] referenceLocales = new Locale[] { ENGLISH, SPANISH }; |
96 | 96 |
|
97 | 97 |
private Locale[] defaultLocales = new Locale[] { |
98 |
// Default supported locales
|
|
98 |
// Default supported locales |
|
99 | 99 |
SPANISH, // Spanish |
100 | 100 |
ENGLISH, // English |
101 |
new Locale("en","US"), // English US |
|
101 |
new Locale("en", "US"), // English US
|
|
102 | 102 |
new Locale("ca"), // Catalan |
103 | 103 |
new Locale("gl"), // Galician |
104 | 104 |
new Locale("eu"), // Basque |
... | ... | |
118 | 118 |
new Locale("tr"), // Turkish |
119 | 119 |
new Locale("sr"), // Serbio |
120 | 120 |
new Locale("sw") // Swahili |
121 |
}; |
|
121 |
};
|
|
122 | 122 |
|
123 |
|
|
124 | 123 |
/** |
125 | 124 |
* Returns the unique instance of the I18nManager. |
126 | 125 |
* |
... | ... | |
132 | 131 |
|
133 | 132 |
public static String capitalize(String text) { |
134 | 133 |
// Convert the first letter to uppercase |
135 |
String capitalLetter = new String(new char[] { Character
|
|
136 |
.toUpperCase(text.charAt(0)) }); |
|
134 |
String capitalLetter = |
|
135 |
new String(new char[] { Character.toUpperCase(text.charAt(0)) });
|
|
137 | 136 |
return capitalLetter.concat(text.substring(1)); |
138 | 137 |
} |
139 | 138 |
|
140 |
/** |
|
141 |
* Empty constructor. |
|
142 |
*/ |
|
143 |
I18nManagerImpl() { |
|
144 |
} |
|
145 |
|
|
146 | 139 |
public Locale[] getInstalledLocales() { |
147 | 140 |
if (registeredLocales == null) { |
148 | 141 |
|
... | ... | |
160 | 153 |
storeInstalledLocales(); |
161 | 154 |
} else { |
162 | 155 |
XMLEntity localesEntity = getRegisteredLocalesPersistence(); |
163 |
registeredLocales = new HashSet(localesEntity
|
|
164 |
.getChildrenCount()); |
|
156 |
registeredLocales = |
|
157 |
new HashSet(localesEntity.getChildrenCount());
|
|
165 | 158 |
for (int i = 0; i < localesEntity.getChildrenCount(); i++) { |
166 | 159 |
XMLEntity localeEntity = localesEntity.getChild(i); |
167 | 160 |
String language = localeEntity.getStringProperty(LANGUAGE); |
... | ... | |
173 | 166 |
} |
174 | 167 |
} |
175 | 168 |
|
176 |
return (Locale[]) registeredLocales |
|
177 |
.toArray(new Locale[registeredLocales.size()]); |
|
169 |
return (Locale[]) registeredLocales.toArray(new Locale[registeredLocales.size()]); |
|
178 | 170 |
} |
179 | 171 |
|
180 | 172 |
public void uninstallLocale(Locale locale) throws I18nException { |
... | ... | |
187 | 179 |
storeInstalledLocales(); |
188 | 180 |
|
189 | 181 |
// Remove the resource bundle file |
190 |
File bundleFile = new File(getResourcesFolder(),
|
|
191 |
getResourceFileName(locale)); |
|
182 |
File bundleFile = |
|
183 |
new File(getResourcesFolder(), getResourceFileName(locale));
|
|
192 | 184 |
|
193 | 185 |
if (bundleFile.exists()) { |
194 | 186 |
bundleFile.delete(); |
... | ... | |
201 | 193 |
} |
202 | 194 |
|
203 | 195 |
public String getDisplayName(Locale locale, Locale displayLocale) { |
204 |
StringBuffer name = new StringBuffer(getLanguageDisplayName(locale,
|
|
205 |
displayLocale)); |
|
196 |
StringBuffer name = |
|
197 |
new StringBuffer(getLanguageDisplayName(locale, displayLocale));
|
|
206 | 198 |
|
207 | 199 |
if (!isEmpty(locale.getCountry())) { |
208 | 200 |
name.append(" - "); |
... | ... | |
297 | 289 |
return null; |
298 | 290 |
} |
299 | 291 |
|
300 |
for (Iterator iterator = locales.entrySet().iterator(); iterator |
|
301 |
.hasNext();) { |
|
292 |
for (Iterator iterator = locales.entrySet().iterator(); iterator.hasNext();) { |
|
302 | 293 |
Entry entry = (Entry) iterator.next(); |
303 | 294 |
|
304 | 295 |
String fileName = (String) entry.getKey(); |
... | ... | |
315 | 306 |
// Replace the old bundle with the new one |
316 | 307 |
ZipEntry zipEntry = zipFile.getEntry(fileName); |
317 | 308 |
InputStream is = zipFile.getInputStream(zipEntry); |
318 |
BufferedReader reader = new BufferedReader(
|
|
319 |
new InputStreamReader(is)); |
|
309 |
BufferedReader reader = |
|
310 |
new BufferedReader(new InputStreamReader(is));
|
|
320 | 311 |
|
321 | 312 |
File bundleFile = getResourceFile(locale); |
322 | 313 |
FileWriter fileWriter = new FileWriter(bundleFile); |
... | ... | |
338 | 329 |
throw new InstallLocalesException(importFile, ex); |
339 | 330 |
} |
340 | 331 |
|
341 |
return (Locale[]) importLocales |
|
342 |
.toArray(new Locale[importLocales.size()]); |
|
332 |
return (Locale[]) importLocales.toArray(new Locale[importLocales.size()]); |
|
343 | 333 |
} |
344 | 334 |
|
345 | 335 |
public void exportLocaleForUpdate(Locale locale, Locale referenceLocale, |
... | ... | |
385 | 375 |
private void exportLocale(Locale[] locales, Locale[] referenceLocales, |
386 | 376 |
File exportFile, boolean update) throws I18nException { |
387 | 377 |
|
388 |
Locale[] refArray = getReferenceLocalesToExport(locales,
|
|
389 |
referenceLocales); |
|
378 |
Locale[] refArray = |
|
379 |
getReferenceLocalesToExport(locales, referenceLocales);
|
|
390 | 380 |
|
381 |
Set allReferenceKeys = new HashSet(); |
|
382 |
Map referenceTexts = new HashMap(refArray.length); |
|
383 |
for (int i = 0; i < refArray.length; i++) { |
|
384 |
Map texts = getAllTexts(refArray[i]); |
|
385 |
referenceTexts.put(refArray[i], texts); |
|
386 |
allReferenceKeys.addAll(texts.keySet()); |
|
387 |
} |
|
388 |
|
|
391 | 389 |
try { |
392 | 390 |
FileOutputStream fos = new FileOutputStream(exportFile); |
393 | 391 |
ZipOutputStream zipos = new ZipOutputStream(fos); |
... | ... | |
403 | 401 |
if (locales != null) { |
404 | 402 |
for (int i = 0; i < locales.length; i++) { |
405 | 403 |
texts = getAllTexts(locales[i]); |
404 |
addPendingKeys(texts, allReferenceKeys); |
|
406 | 405 |
putResourceInZip(zipos, ps, texts, |
407 | 406 |
getResourceFileName(locales[i])); |
408 | 407 |
} |
... | ... | |
410 | 409 |
} else { // translate |
411 | 410 |
// First, export the locales to translate, taking the keys from |
412 | 411 |
// the reference locales, but without values |
413 |
// We will use the keys of the reference locales |
|
414 |
texts = getAllTexts(referenceLocales[0]); |
|
415 | 412 |
if (locales != null) { |
416 | 413 |
for (int i = 0; i < locales.length; i++) { |
417 |
putResourceInZip(zipos, ps, texts,
|
|
418 |
getResourceFileName(locales[i]), false);
|
|
414 |
putResourceInZip(zipos, ps, allReferenceKeys,
|
|
415 |
getResourceFileName(locales[i])); |
|
419 | 416 |
} |
420 | 417 |
} |
421 | 418 |
} |
... | ... | |
439 | 436 |
} |
440 | 437 |
|
441 | 438 |
/** |
439 |
* Adds the keys of the set to the map that aren't still contained in it |
|
440 |
* with a related empty String. |
|
441 |
* |
|
442 |
* @param texts |
|
443 |
* the map to complete with the keys |
|
444 |
* @param allReferenceKeys |
|
445 |
* the complete key set |
|
446 |
*/ |
|
447 |
private void addPendingKeys(Map texts, Set allReferenceKeys) { |
|
448 |
for (Iterator iterator = allReferenceKeys.iterator(); iterator.hasNext();) { |
|
449 |
Object key = (Object) iterator.next(); |
|
450 |
if (!texts.containsKey(key)) { |
|
451 |
texts.put(key, ""); |
|
452 |
} |
|
453 |
} |
|
454 |
} |
|
455 |
|
|
456 |
/** |
|
442 | 457 |
* Returns the list of reference locales to export, as the union of the |
443 | 458 |
* default reference locales list and the one selected as reference. The |
444 | 459 |
* locales to translate or update are extracted from the list. |
... | ... | |
461 | 476 |
exportRefLocales.remove(locales[i]); |
462 | 477 |
} |
463 | 478 |
} |
464 |
Locale[] refArray = (Locale[]) exportRefLocales
|
|
465 |
.toArray(new Locale[exportRefLocales.size()]); |
|
479 |
Locale[] refArray = |
|
480 |
(Locale[]) exportRefLocales.toArray(new Locale[exportRefLocales.size()]);
|
|
466 | 481 |
return refArray; |
467 | 482 |
} |
468 | 483 |
|
... | ... | |
484 | 499 |
Map locales; |
485 | 500 |
try { |
486 | 501 |
InputStream is = zipFile.getInputStream(zipEntry); |
487 |
BufferedReader reader = new BufferedReader(
|
|
488 |
new InputStreamReader(is)); |
|
502 |
BufferedReader reader = |
|
503 |
new BufferedReader(new InputStreamReader(is));
|
|
489 | 504 |
|
490 | 505 |
locales = new HashMap(2); |
491 | 506 |
String line; |
492 | 507 |
while ((line = reader.readLine()) != null) { |
493 | 508 |
// The excepted format is: |
494 | 509 |
// FILENAME,LANGUAGE,COUNTRY,VARIANT,IS_REFERENCE |
495 |
StringTokenizer st = new StringTokenizer(line, CSV_SEPARATOR,
|
|
496 |
true); |
|
510 |
StringTokenizer st = |
|
511 |
new StringTokenizer(line, CSV_SEPARATOR, true);
|
|
497 | 512 |
// First: locale file name (required) |
498 | 513 |
String fileName = st.nextToken(); |
499 | 514 |
if (CSV_SEPARATOR.equals(fileName)) { |
... | ... | |
626 | 641 |
*/ |
627 | 642 |
private void putResourceInZip(ZipOutputStream zipos, PrintStream ps, |
628 | 643 |
Map texts, String resourceFileName) throws IOException { |
644 |
// Add ZIP entry for the resource bundle file |
|
645 |
zipos.putNextEntry(new ZipEntry(resourceFileName)); |
|
629 | 646 |
|
630 |
putResourceInZip(zipos, ps, texts, resourceFileName, true); |
|
647 |
for (Iterator iterator = texts.entrySet().iterator(); iterator.hasNext();) { |
|
648 |
Entry entry = (Entry) iterator.next(); |
|
649 |
String keyEncoded = escape((String) entry.getKey(), true); |
|
650 |
ps.print(keyEncoded); |
|
651 |
ps.print("="); |
|
652 |
String valueEncoded = escape((String) entry.getValue(), false); |
|
653 |
ps.println(valueEncoded); |
|
654 |
} |
|
655 |
|
|
656 |
ps.flush(); |
|
657 |
|
|
658 |
// Close the ZIP entry, the file is complete |
|
659 |
zipos.closeEntry(); |
|
631 | 660 |
} |
632 | 661 |
|
633 | 662 |
/** |
634 | 663 |
* Puts a new resource file into a Jar file. |
635 | 664 |
*/ |
636 | 665 |
private void putResourceInZip(ZipOutputStream zipos, PrintStream ps, |
637 |
Map texts, String resourceFileName, boolean withValue) |
|
638 |
throws IOException { |
|
666 |
Set keys, String resourceFileName) throws IOException { |
|
639 | 667 |
// Add ZIP entry for the resource bundle file |
640 | 668 |
zipos.putNextEntry(new ZipEntry(resourceFileName)); |
641 | 669 |
|
642 |
for (Iterator iterator = texts.entrySet().iterator(); iterator |
|
643 |
.hasNext();) { |
|
644 |
Entry entry = (Entry) iterator.next(); |
|
645 |
String keyEncoded = escape((String) entry.getKey(), true); |
|
670 |
for (Iterator iterator = keys.iterator(); iterator.hasNext();) { |
|
671 |
String value = (String) iterator.next(); |
|
672 |
String keyEncoded = escape(value, true); |
|
646 | 673 |
ps.print(keyEncoded); |
647 | 674 |
ps.print("="); |
648 |
if (withValue) { |
|
649 |
String valueEncoded = escape((String) entry.getValue(), false); |
|
650 |
ps.println(valueEncoded); |
|
651 |
} else { |
|
652 |
ps.println(); |
|
653 |
} |
|
675 |
ps.println(); |
|
654 | 676 |
} |
655 | 677 |
|
656 | 678 |
ps.flush(); |
... | ... | |
720 | 742 |
* Returns the I18n Plugin persistence. |
721 | 743 |
*/ |
722 | 744 |
private XMLEntity getI18nPersistence() { |
723 |
XMLEntity entity = PluginServices.getPluginServices(I18N_EXTENSION) |
|
724 |
.getPersistentXML(); |
|
745 |
XMLEntity entity = |
|
746 |
PluginServices.getPluginServices(I18N_EXTENSION) |
|
747 |
.getPersistentXML(); |
|
725 | 748 |
return entity; |
726 | 749 |
} |
727 | 750 |
|
... | ... | |
755 | 778 |
|
756 | 779 |
localesEntity.setName(REGISTERED_LOCALES_PERSISTENCE); |
757 | 780 |
|
758 |
for (Iterator iterator = registeredLocales.iterator(); iterator |
|
759 |
.hasNext();) { |
|
781 |
for (Iterator iterator = registeredLocales.iterator(); iterator.hasNext();) { |
|
760 | 782 |
Locale locale = (Locale) iterator.next(); |
761 | 783 |
XMLEntity localeEntity = new XMLEntity(); |
762 | 784 |
localeEntity.setName(locale.getDisplayName()); |
... | ... | |
807 | 829 |
} |
808 | 830 |
return sb.toString(); |
809 | 831 |
} |
810 |
|
|
832 |
|
|
811 | 833 |
@SuppressWarnings("unchecked") |
812 | 834 |
private String getSystemProperty(final String property, String defaultValue) { |
813 |
String value = (String) AccessController
|
|
814 |
.doPrivileged(new PrivilegedAction() { |
|
815 |
public Object run() { |
|
816 |
return System.getProperty(property); |
|
817 |
} |
|
818 |
});
|
|
835 |
String value = |
|
836 |
(String) AccessController.doPrivileged(new PrivilegedAction() {
|
|
837 |
public Object run() {
|
|
838 |
return System.getProperty(property);
|
|
839 |
}
|
|
840 |
});
|
|
819 | 841 |
return value == null ? defaultValue : value; |
820 | 842 |
} |
821 | 843 |
} |
Also available in: Unified diff