57 |
* Represents a {@link HashMap} of translations. toString() returns the |
* Represents a {@link HashMap} of translations. toString() returns the |
58 |
* appropriate translation. This class is mutable. |
* appropriate translation. This class is mutable. |
59 |
* |
* |
60 |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
61 |
*/ |
*/ |
62 |
|
|
63 |
public class Translation extends HashMap<String, String> implements |
public class Translation extends HashMap<String, String> implements |
65 |
|
|
66 |
private static final long serialVersionUID = -347702744122305245L; |
private static final long serialVersionUID = -347702744122305245L; |
67 |
|
|
68 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
public static final String LOCALE_CHANGE_PROPERTY = "localechange"; |
69 |
public static final String ACTIVELANG_CHANGE_PROPERTY = "activelangchange"; |
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"; |
94 |
* informed if any of the translations changed. TODO: Should be registerable |
* informed if any of the translations changed. TODO: Should be registerable |
95 |
* for specific languages |
* for specific languages |
96 |
*/ |
*/ |
97 |
private WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>( |
private final WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>( |
98 |
ActionListener.class); |
ActionListener.class); |
99 |
|
|
100 |
static { |
static { |
150 |
} |
} |
151 |
|
|
152 |
if (!I8NUtil.isValidISOLangCode(newLang)) { |
if (!I8NUtil.isValidISOLangCode(newLang)) { |
153 |
throw new IllegalArgumentException("'" + newLang |
|
154 |
+ "' is not a valid ISO language code."); |
if (!I8NUtil.isPropertiesLanguage(newLang)) { |
155 |
|
throw new IllegalArgumentException( |
156 |
|
"'" |
157 |
|
+ newLang |
158 |
|
+ "' is not a valid ISO language code, nor is it a valid userdefined Properties code."); |
159 |
|
} |
160 |
|
|
161 |
} |
} |
162 |
|
|
163 |
Translation.activeLang = newLang; |
Translation.activeLang = newLang; |
166 |
Locale newLocale = new Locale(newLang); |
Locale newLocale = new Locale(newLang); |
167 |
if (setDefaultLocale) { |
if (setDefaultLocale) { |
168 |
|
|
169 |
Locale.setDefault(newLocale); |
setDefaultLocale(newLocale); |
|
/** |
|
|
* Setting default locale for Swing JComponents to work around bug |
|
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
|
|
*/ |
|
|
JComponent.setDefaultLocale(newLocale); |
|
|
|
|
|
fireLocaleChangeEvents(); |
|
170 |
|
|
171 |
LOGGER.info(Translation.class.getName() |
LOGGER.info(Translation.class.getName() |
172 |
+ " switched ActiveLang and Locale to " + newLang); |
+ " switched ActiveLang and Locale to " + newLang); |
379 |
|
|
380 |
/** |
/** |
381 |
* Informs all registered {@link PropertyChangeListener}s about a change of |
* Informs all registered {@link PropertyChangeListener}s about a change of |
382 |
* the the {@link Locale}. |
* type LOCALE_CHANGE_PROPERTY the the {@link Locale}. |
383 |
*/ |
*/ |
384 |
public static void fireLocaleChangeEvents() { |
public static void fireLocaleChangeEvents() { |
385 |
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
386 |
new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY, |
new ArrayList<String>(), "fakeSource"), LOCALE_CHANGE_PROPERTY, |
387 |
null, getActiveLang()); |
null, getActiveLang()); |
388 |
for (PropertyChangeListener pcl : listenersLocaleChange) { |
for (PropertyChangeListener pcl : listenersLocaleChange) { |
389 |
if (pcl != null) |
if (pcl != null) |
393 |
|
|
394 |
/** |
/** |
395 |
* Informs all registered {@link PropertyChangeListener}s about a change of |
* Informs all registered {@link PropertyChangeListener}s about a change of |
396 |
* the the {@link Locale}. |
* type ACTIVELANG_CHANGE_PROPERTY the the {@link Locale}. |
397 |
*/ |
*/ |
398 |
public static void fireActiveLangChangeEvents() { |
public static void fireActiveLangChangeEvents() { |
399 |
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
526 |
public String toString(String lang) { |
public String toString(String lang) { |
527 |
return get(lang); |
return get(lang); |
528 |
} |
} |
529 |
|
|
530 |
|
/** |
531 |
|
* Will set the default Locale (if not already equal) and fire Locale change |
532 |
|
* events. |
533 |
|
* |
534 |
|
* @param if <code>null</code> will do nothing. |
535 |
|
*/ |
536 |
|
public static void setDefaultLocale(Locale locale) { |
537 |
|
|
538 |
|
if (locale == null) |
539 |
|
return; |
540 |
|
|
541 |
|
if (I8NUtil.isPropertiesLanguage(locale.getLanguage())) { |
542 |
|
locale = I8NUtil.propLocales.get(locale.getLanguage()) |
543 |
|
.getParentLocale(); |
544 |
|
} |
545 |
|
|
546 |
|
if (Locale.getDefault().equals(locale)) |
547 |
|
return; |
548 |
|
Locale.setDefault(locale); |
549 |
|
/** |
550 |
|
* Setting default locale for Swing JComponents to work around bug |
551 |
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
552 |
|
*/ |
553 |
|
JComponent.setDefaultLocale(locale); |
554 |
|
|
555 |
|
fireLocaleChangeEvents(); |
556 |
|
} |
557 |
|
|
558 |
} |
} |