25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.i8n; |
package skrueger.i8n; |
31 |
|
|
33 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
34 |
import java.beans.PropertyChangeEvent; |
import java.beans.PropertyChangeEvent; |
35 |
import java.beans.PropertyChangeListener; |
import java.beans.PropertyChangeListener; |
36 |
|
import java.io.Serializable; |
37 |
import java.util.ArrayList; |
import java.util.ArrayList; |
38 |
import java.util.HashMap; |
import java.util.HashMap; |
39 |
import java.util.Iterator; |
import java.util.Iterator; |
40 |
import java.util.List; |
import java.util.List; |
41 |
import java.util.Locale; |
import java.util.Locale; |
42 |
import java.util.Random; |
import java.util.Random; |
43 |
|
import java.util.Set; |
44 |
import java.util.WeakHashMap; |
import java.util.WeakHashMap; |
45 |
|
|
46 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
49 |
import org.geotools.util.WeakHashSet; |
import org.geotools.util.WeakHashSet; |
50 |
import org.opengis.util.InternationalString; |
import org.opengis.util.InternationalString; |
51 |
|
|
52 |
|
import schmitzm.lang.ResourceProvider; |
53 |
|
import schmitzm.lang.SortableVector; |
54 |
import skrueger.geotools.Copyable; |
import skrueger.geotools.Copyable; |
55 |
|
|
56 |
/** |
/** |
57 |
* Represents a {@link HashMap} of translations. toString() returns the |
* Represents a {@link HashMap} of translations. toString() returns the |
58 |
* appropriate translation |
* appropriate translation. This class is mutable. |
59 |
* |
* |
60 |
* @author @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
|
* Krüger</a> |
|
61 |
*/ |
*/ |
62 |
|
|
63 |
public class Translation extends HashMap<String, String> implements |
public class Translation extends HashMap<String, String> implements |
64 |
Copyable<Translation> { |
Copyable<Translation>, Serializable, Cloneable { |
65 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
|
66 |
|
private static final long serialVersionUID = -347702744122305245L; |
67 |
|
|
68 |
|
public static final String LOCALE_CHANGE_PROPERTY = "localechange"; |
69 |
|
public static final String ACTIVELANG_CHANGE_PROPERTY = "activelangchange"; |
70 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
71 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
72 |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
73 |
|
|
74 |
|
/** A static field defining which language should be served **/ |
75 |
static String activeLang = Locale.getDefault().getLanguage(); |
static String activeLang = Locale.getDefault().getLanguage(); |
76 |
|
|
77 |
static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>( |
/** |
78 |
|
* A {@link WeakHashSet} of {@link PropertyChangeListener} that will be |
79 |
|
* informed if the acitveLang has been changed with a setActiveLang(..,true |
80 |
|
* or false). |
81 |
|
*/ |
82 |
|
static protected WeakHashSet<PropertyChangeListener> listenersActiveLangChange = new WeakHashSet<PropertyChangeListener>( |
83 |
PropertyChangeListener.class); |
PropertyChangeListener.class); |
84 |
|
|
85 |
static { |
/** |
86 |
|
* A {@link WeakHashSet} of {@link PropertyChangeListener} that will be |
87 |
|
* informed if the Locale has been changed due to a setActiveLang(..,true) |
88 |
|
*/ |
89 |
|
static protected WeakHashSet<PropertyChangeListener> listenersLocaleChange = new WeakHashSet<PropertyChangeListener>( |
90 |
|
PropertyChangeListener.class); |
91 |
|
|
92 |
// TODO default aus Locale auslesen und mit möglichen vergleichen... |
/** |
93 |
// mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht |
* A {@link WeakHashSet} of {@link PropertyChangeListener} that will be |
94 |
|
* informed if any of the translations changed. TODO: Should be registerable |
95 |
|
* for specific languages |
96 |
|
*/ |
97 |
|
private WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>( |
98 |
|
ActionListener.class); |
99 |
|
|
100 |
|
static { |
101 |
// Get default locale |
// Get default locale |
102 |
Locale locale = Locale.getDefault(); |
Locale locale = Locale.getDefault(); |
103 |
setActiveLang(locale.getLanguage()); |
setActiveLang(locale.getLanguage()); |
104 |
} |
} |
105 |
|
|
|
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
|
|
ActionListener.class); |
|
|
|
|
106 |
@Override |
@Override |
107 |
/* |
/** |
108 |
* @comment To make a copy of a translation see methods toOneLine() and |
* implemented using #toOneLine and #fromOneLine |
|
* fromOneLine() |
|
109 |
*/ |
*/ |
110 |
public Translation clone() { |
public Translation clone() { |
111 |
throw new RuntimeException("use copy()"); |
Translation clone = new Translation(); |
112 |
// return (Translation) super.clone(); |
clone.fromOneLine(toOneLine()); |
113 |
|
return clone; |
114 |
} |
} |
115 |
|
|
116 |
/** |
/** |
123 |
/** |
/** |
124 |
* Set up the {@link Translation}-system to use language. If a change is |
* Set up the {@link Translation}-system to use language. If a change is |
125 |
* performed, events are fired to listeners. Nothing is done if the new |
* performed, events are fired to listeners. Nothing is done if the new |
126 |
* language equals the old language. The system's default locale is changed. |
* language equals the old language. The system's default {@link Locale} is |
127 |
|
* changed. |
128 |
* |
* |
129 |
* @param newLang |
* @param newLang |
130 |
* The ISO Code of the new active language |
* The ISO Code of the new active language |
154 |
+ "' is not a valid ISO language code."); |
+ "' is not a valid ISO language code."); |
155 |
} |
} |
156 |
|
|
157 |
Locale newLocale = new Locale(newLang); |
Translation.activeLang = newLang; |
158 |
if (setDefaultLocale) |
fireActiveLangChangeEvents(); |
|
Locale.setDefault(newLocale); |
|
159 |
|
|
160 |
/** |
Locale newLocale = new Locale(newLang); |
161 |
* Setting default locale for Swing JComponents to work around bug |
if (setDefaultLocale) { |
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
|
|
*/ |
|
|
JComponent.setDefaultLocale(newLocale); |
|
162 |
|
|
163 |
Translation.activeLang = newLang; |
setDefaultLocale(newLocale); |
164 |
|
|
165 |
fireLocaleChangeEvents(); |
LOGGER.info(Translation.class.getName() |
166 |
|
+ " switched ActiveLang and Locale to " + newLang); |
167 |
|
} else { |
168 |
|
LOGGER.info(Translation.class.getName() |
169 |
|
+ " switched ActiveLang to " + newLang); |
170 |
|
} |
171 |
|
|
|
LOGGER.info("skrueger.i8n.Translation switched ActiveLang to " |
|
|
+ newLang); |
|
172 |
} |
} |
173 |
|
|
174 |
/** |
/** |
190 |
|
|
191 |
/** |
/** |
192 |
* Initializes a new {@link Translation}, an uses the given String to |
* Initializes a new {@link Translation}, an uses the given String to |
193 |
* initialize the {@link Translation} for all languages codes passed. |
* initialize the {@link Translation} for all languages codes passed. The |
194 |
|
* translations can be changed later. This class is not immutable. |
195 |
|
* |
196 |
|
* @param languages |
197 |
|
* if empty or null, the given default {@link Translation} till |
198 |
|
* be stored under a special key {@link #DEFAULT_KEY} |
199 |
|
* |
200 |
* |
* |
|
* The translations can be changed later |
|
201 |
*/ |
*/ |
202 |
public Translation(List<String> languages, String defaultTranslation) { |
public Translation(List<String> languages, String defaultTranslation) { |
203 |
// put(DEFAULT_KEY, defaultTranslation); |
if (languages == null || languages.isEmpty()) { |
|
if (languages == null) { |
|
204 |
put(DEFAULT_KEY, defaultTranslation); |
put(DEFAULT_KEY, defaultTranslation); |
205 |
} else |
} else |
206 |
for (String code : languages) { |
for (String code : languages) { |
207 |
if (code.equals(getActiveLang())) { |
put(code, defaultTranslation); |
|
put(code, defaultTranslation); |
|
|
} |
|
208 |
} |
} |
209 |
} |
} |
210 |
|
|
223 |
* <li>If format can't be recognized, the {@link String} is interpreted as |
* <li>If format can't be recognized, the {@link String} is interpreted as |
224 |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
225 |
* |
* |
226 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
227 |
*/ |
*/ |
228 |
public void fromOneLine(final String oneLineCoded) { |
public void fromOneLine(final String oneLineCoded) { |
229 |
|
|
264 |
/** |
/** |
265 |
* Exports the Translations to a String of the Format: "de{Baum}en{tree}" |
* Exports the Translations to a String of the Format: "de{Baum}en{tree}" |
266 |
* |
* |
267 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
268 |
*/ |
*/ |
269 |
public String toOneLine() { |
public String toOneLine() { |
270 |
return I8NUtil.toOneLine(this); |
return I8NUtil.toOneLine(this); |
271 |
} |
} |
272 |
|
|
273 |
/** |
/** |
274 |
* Returns the right translation by using the {@link #activeLang} field. If |
* Returns the correct translation by using the {@link #activeLang} field. |
275 |
* no translation is set, an ugly String {@link #NO_TRANSLATION} will re |
* If no translation is set, an ugly String {@link #NO_TRANSLATION} will re |
276 |
* returned. This might be changed for the final release. If the correct |
* returned. This might be changed for the final release. If the correct |
277 |
* language was not found, any entry in the {@link Translation} |
* language was not found, any entry in the {@link Translation} |
278 |
* {@link HashMap} will be returned, that contains more than an empty |
* {@link HashMap} will be returned, that contains more than an empty |
280 |
*/ |
*/ |
281 |
@Override |
@Override |
282 |
public String toString() { |
public String toString() { |
283 |
if (get(activeLang) != null) { |
final String string = get(activeLang); |
284 |
|
if (string != null) { |
285 |
return get(activeLang); |
return get(activeLang); |
286 |
} |
} |
287 |
// **************************************************************************** |
// **************************************************************************** |
290 |
// **************************************************************************** |
// **************************************************************************** |
291 |
// else return ""; |
// else return ""; |
292 |
// //**************************************************************************** |
// //**************************************************************************** |
293 |
// // The following is commented out.. the concept of the default lang |
// // ST: The following is commented out.. the concept of the default |
294 |
|
// lang |
295 |
// seems to be bad.... |
// seems to be bad.... |
296 |
// //**************************************************************************** |
// //**************************************************************************** |
297 |
// MS: |
// MS: |
323 |
*/ |
*/ |
324 |
public static void addLocaleChangeListener( |
public static void addLocaleChangeListener( |
325 |
PropertyChangeListener propertyChangeListener) { |
PropertyChangeListener propertyChangeListener) { |
326 |
listeners.add(propertyChangeListener); |
listenersLocaleChange.add(propertyChangeListener); |
327 |
} |
} |
328 |
|
|
329 |
/** |
/** |
338 |
*/ |
*/ |
339 |
public static boolean removeLocaleChangeListener( |
public static boolean removeLocaleChangeListener( |
340 |
PropertyChangeListener propertyChangeListener) { |
PropertyChangeListener propertyChangeListener) { |
341 |
return listeners.remove(propertyChangeListener); |
return listenersLocaleChange.remove(propertyChangeListener); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
|
* {@link PropertyChangeListener} can be registered to be informed when the |
346 |
|
* {@link Locale} changed.<br> |
347 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
348 |
|
* reference to the listener or it will be removed! |
349 |
|
* |
350 |
|
* @param propertyChangeListener |
351 |
|
* A {@link PropertyChangeListener} that will be called when |
352 |
|
* {@link #setActiveLang(String)} changes the language. |
353 |
|
*/ |
354 |
|
public static void addActiveLangChangeListener( |
355 |
|
PropertyChangeListener propertyChangeListener) { |
356 |
|
listenersActiveLangChange.add(propertyChangeListener); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
|
* {@link PropertyChangeListener} can be registered to be informed when the |
361 |
|
* {@link Locale} changed.<br> |
362 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
363 |
|
* reference to the listener or it will be removed! |
364 |
|
* |
365 |
|
* @param propertyChangeListener |
366 |
|
* A {@link PropertyChangeListener} that will be called when |
367 |
|
* {@link #setActiveLang(String)} changes the language. |
368 |
|
*/ |
369 |
|
public static boolean removeActiveLangListener( |
370 |
|
PropertyChangeListener propertyChangeListener) { |
371 |
|
return listenersActiveLangChange.remove(propertyChangeListener); |
372 |
} |
} |
373 |
|
|
374 |
/** |
/** |
375 |
* Informs all registered {@link PropertyChangeListener}s about a change of |
* Informs all registered {@link PropertyChangeListener}s about a change of |
376 |
* the the {@link Locale}. |
* type LOCALE_CHANGE_PROPERTY the the {@link Locale}. |
377 |
*/ |
*/ |
378 |
public static void fireLocaleChangeEvents() { |
public static void fireLocaleChangeEvents() { |
379 |
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
380 |
new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY, |
new ArrayList<String>(), "fakeSource"), LOCALE_CHANGE_PROPERTY, |
381 |
null, getActiveLang()); |
null, getActiveLang()); |
382 |
for (PropertyChangeListener pcl : listeners) { |
for (PropertyChangeListener pcl : listenersLocaleChange) { |
383 |
|
if (pcl != null) |
384 |
|
pcl.propertyChange(pce); |
385 |
|
} |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
|
* Informs all registered {@link PropertyChangeListener}s about a change of |
390 |
|
* type ACTIVELANG_CHANGE_PROPERTY the the {@link Locale}. |
391 |
|
*/ |
392 |
|
public static void fireActiveLangChangeEvents() { |
393 |
|
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
394 |
|
new ArrayList<String>(), "fakeSource"), |
395 |
|
ACTIVELANG_CHANGE_PROPERTY, null, getActiveLang()); |
396 |
|
for (PropertyChangeListener pcl : listenersLocaleChange) { |
397 |
if (pcl != null) |
if (pcl != null) |
398 |
pcl.propertyChange(pce); |
pcl.propertyChange(pce); |
399 |
} |
} |
404 |
* reference as long as you need the listener. |
* reference as long as you need the listener. |
405 |
*/ |
*/ |
406 |
public void addTranslationChangeListener(ActionListener actionListener) { |
public void addTranslationChangeListener(ActionListener actionListener) { |
407 |
if (actionListeners.add(actionListener)) { |
if (changeListeners.add(actionListener)) { |
408 |
LOGGER |
// LOGGER |
409 |
.debug("registering a new translationChangeActionListener in the WeakHashSet"); |
// .debug("registering a new translationChangeActionListener in the WeakHashSet"); |
410 |
} |
} |
411 |
} |
} |
412 |
|
|
416 |
* listener. |
* listener. |
417 |
*/ |
*/ |
418 |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
419 |
return actionListeners.remove(actionListener); |
return changeListeners.remove(actionListener); |
420 |
} |
} |
421 |
|
|
422 |
public void fireTranslationChangedEvents(String lang) { |
public void fireTranslationChangedEvents(String lang) { |
423 |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
424 |
|
|
425 |
final Iterator<ActionListener> iterator = actionListeners.iterator(); |
final Iterator<ActionListener> iterator = changeListeners.iterator(); |
426 |
while (iterator.hasNext()) { |
while (iterator.hasNext()) { |
427 |
ActionListener al = iterator.next(); |
ActionListener al = iterator.next(); |
428 |
al.actionPerformed(ae); |
al.actionPerformed(ae); |
445 |
|
|
446 |
/** |
/** |
447 |
* Copy this {@link Translation} to another {@link Translation} e.g. for |
* Copy this {@link Translation} to another {@link Translation} e.g. for |
448 |
* editing |
* editing and return the target. |
449 |
* |
* |
450 |
* @return the destination {@link Translation} |
* @return the destination {@link Translation} |
451 |
*/ |
*/ |
452 |
@Override |
@Override |
453 |
public Translation copyTo(Translation translation2) { |
public Translation copyTo(Translation translation2) { |
|
|
|
454 |
if (translation2 == null) |
if (translation2 == null) |
|
// throw new IllegalArgumentException( |
|
|
// "Target translation may not be null."); |
|
455 |
return copy(); |
return copy(); |
|
for (String s : keySet()) { |
|
|
translation2.put(s, get(s)); |
|
|
} |
|
456 |
|
|
457 |
|
translation2.fromOneLine(toOneLine()); |
458 |
return translation2; |
return translation2; |
459 |
} |
} |
460 |
|
|
481 |
return true; |
return true; |
482 |
} |
} |
483 |
|
|
484 |
|
/** |
485 |
|
* Goes through the available languages of the FIRST registered |
486 |
|
* {@link ResourceProvider} and set the active locale to the fist match. |
487 |
|
* |
488 |
|
* @param fireChangeEvent |
489 |
|
* if <code>true</code>, a Translation.fireLocaleChangeEvents() |
490 |
|
* is issued. |
491 |
|
* |
492 |
|
* @return |
493 |
|
*/ |
494 |
|
public static boolean setFirstmatchingLanguage(List<String> languages, |
495 |
|
boolean fireChangeEvent) { |
496 |
|
|
497 |
|
SortableVector<ResourceProvider> registeredResourceProvider = ResourceProvider |
498 |
|
.getRegisteredResourceProvider(); |
499 |
|
Set<Locale> available = ResourceProvider.getAvailableLocales( |
500 |
|
registeredResourceProvider.get(0), true); |
501 |
|
|
502 |
|
for (String l : languages) { |
503 |
|
for (Locale loc : available) { |
504 |
|
if (loc.getLanguage().equals(l)) { |
505 |
|
Translation.setActiveLang(l); |
506 |
|
if (fireChangeEvent) |
507 |
|
Translation.fireLocaleChangeEvents(); |
508 |
|
return true; |
509 |
|
} |
510 |
|
} |
511 |
|
} |
512 |
|
|
513 |
|
return false; |
514 |
|
|
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
|
* Returns the translation in a requested language |
519 |
|
*/ |
520 |
|
public String toString(String lang) { |
521 |
|
return get(lang); |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
|
* Will set the default Locale (if not already equal) and fire Locale change |
526 |
|
* events. |
527 |
|
* |
528 |
|
* @param if <code>null</code> will do nothing. |
529 |
|
*/ |
530 |
|
public static void setDefaultLocale(Locale locale) { |
531 |
|
|
532 |
|
if (locale == null) |
533 |
|
return; |
534 |
|
|
535 |
|
if (Locale.getDefault().equals(locale)) |
536 |
|
return; |
537 |
|
Locale.setDefault(locale); |
538 |
|
/** |
539 |
|
* Setting default locale for Swing JComponents to work around bug |
540 |
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
541 |
|
*/ |
542 |
|
JComponent.setDefaultLocale(locale); |
543 |
|
|
544 |
|
fireLocaleChangeEvents(); |
545 |
|
} |
546 |
|
|
547 |
} |
} |