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.WeakHashMap; |
44 |
|
|
45 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
46 |
|
|
59 |
*/ |
*/ |
60 |
|
|
61 |
public class Translation extends HashMap<String, String> implements |
public class Translation extends HashMap<String, String> implements |
62 |
Copyable<Translation> { |
Copyable<Translation>, Serializable { |
63 |
|
|
64 |
|
private static final long serialVersionUID = -347702744122305245L; |
65 |
|
|
66 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
67 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
68 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
69 |
static final Logger log = Logger.getLogger(Translation.class); |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
70 |
static String activeLang = Locale.getDefault().getLanguage(); |
static String activeLang = Locale.getDefault().getLanguage(); |
71 |
|
|
72 |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>( |
73 |
|
PropertyChangeListener.class); |
74 |
|
|
75 |
static { |
static { |
76 |
|
|
81 |
Locale locale = Locale.getDefault(); |
Locale locale = Locale.getDefault(); |
82 |
setActiveLang(locale.getLanguage()); |
setActiveLang(locale.getLanguage()); |
83 |
} |
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
88 |
ActionListener.class); |
ActionListener.class); |
151 |
|
|
152 |
fireLocaleChangeEvents(); |
fireLocaleChangeEvents(); |
153 |
|
|
154 |
log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang); |
LOGGER.info("skrueger.i8n.Translation switched ActiveLang to " |
155 |
|
+ newLang); |
156 |
} |
} |
157 |
|
|
158 |
/** |
/** |
184 |
put(DEFAULT_KEY, defaultTranslation); |
put(DEFAULT_KEY, defaultTranslation); |
185 |
} else |
} else |
186 |
for (String code : languages) { |
for (String code : languages) { |
187 |
if (code.equals(getActiveLang())) { |
// if (code.equals(getActiveLang())) { |
188 |
put(code, defaultTranslation); |
put(code, defaultTranslation); |
189 |
} |
// } |
190 |
} |
} |
191 |
} |
} |
192 |
|
|
237 |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
238 |
} |
} |
239 |
} catch (Exception e) { |
} catch (Exception e) { |
240 |
log.warn("Error while reading the oneLineCode '" + oneLineCoded |
LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded |
241 |
+ "'", e); |
+ "'", e); |
242 |
log.warn("Translation will be empty!"); |
LOGGER.warn("Translation will be empty!"); |
243 |
} |
} |
244 |
} |
} |
245 |
|
|
293 |
|
|
294 |
/** |
/** |
295 |
* {@link PropertyChangeListener} can be registered to be informed when the |
* {@link PropertyChangeListener} can be registered to be informed when the |
296 |
* {@link Locale} changed. |
* {@link Locale} changed.<br> |
297 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
298 |
|
* reference to the listener or it will be removed! |
299 |
* |
* |
300 |
* @param propertyChangeListener |
* @param propertyChangeListener |
301 |
|
* A {@link PropertyChangeListener} that will be called when |
302 |
|
* {@link #setActiveLang(String)} changes the language. |
303 |
*/ |
*/ |
304 |
public static void addLocaleChangeListener( |
public static void addLocaleChangeListener( |
305 |
PropertyChangeListener propertyChangeListener) { |
PropertyChangeListener propertyChangeListener) { |
307 |
} |
} |
308 |
|
|
309 |
/** |
/** |
310 |
|
* {@link PropertyChangeListener} can be registered to be informed when the |
311 |
|
* {@link Locale} changed.<br> |
312 |
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
313 |
|
* reference to the listener or it will be removed! |
314 |
|
* |
315 |
|
* @param propertyChangeListener |
316 |
|
* A {@link PropertyChangeListener} that will be called when |
317 |
|
* {@link #setActiveLang(String)} changes the language. |
318 |
|
*/ |
319 |
|
public static boolean removeLocaleChangeListener( |
320 |
|
PropertyChangeListener propertyChangeListener) { |
321 |
|
return listeners.remove(propertyChangeListener); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
* Informs all registered {@link PropertyChangeListener}s about a change of |
* Informs all registered {@link PropertyChangeListener}s about a change of |
326 |
* the the {@link Locale}. |
* the the {@link Locale}. |
327 |
*/ |
*/ |
340 |
* reference as long as you need the listener. |
* reference as long as you need the listener. |
341 |
*/ |
*/ |
342 |
public void addTranslationChangeListener(ActionListener actionListener) { |
public void addTranslationChangeListener(ActionListener actionListener) { |
343 |
actionListeners.add(actionListener); |
if (actionListeners.add(actionListener)) { |
344 |
|
// LOGGER |
345 |
|
// .debug("registering a new translationChangeActionListener in the WeakHashSet"); |
346 |
|
} |
347 |
} |
} |
348 |
|
|
349 |
/** |
/** |