1 |
package skrueger.i8n; |
package skrueger.i8n; |
2 |
|
import java.beans.PropertyChangeEvent; |
3 |
|
import java.beans.PropertyChangeListener; |
4 |
|
import java.util.ArrayList; |
5 |
import java.util.HashMap; |
import java.util.HashMap; |
6 |
import java.util.List; |
import java.util.List; |
7 |
import java.util.Locale; |
import java.util.Locale; |
8 |
|
|
9 |
|
import javax.swing.JComponent; |
10 |
|
|
11 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
12 |
|
|
13 |
/** |
/** |
17 |
* @author @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
* @author @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
18 |
*/ |
*/ |
19 |
|
|
20 |
public class Translation extends HashMap<String, String> { |
public class Translation extends HashMap<String, String>{ |
21 |
|
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
22 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
23 |
public static final String DEFAULT_KEY = "default"; |
public static final String DEFAULT_KEY = "default"; |
24 |
static final Logger log = Logger.getLogger( Translation.class ); |
static final Logger log = Logger.getLogger( Translation.class ); |
25 |
static String activeLang = "fr"; |
static String activeLang = "fr"; |
26 |
|
|
27 |
|
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
28 |
|
|
29 |
static { |
static { |
30 |
|
|
31 |
//TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht |
//TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht |
36 |
} |
} |
37 |
|
|
38 |
@Override |
@Override |
39 |
|
/** |
40 |
|
* @comment To make a copy of a translation see methods toOneLine() and fromOneLine() |
41 |
|
*/ |
42 |
public Translation clone() { |
public Translation clone() { |
43 |
return (Translation) super.clone(); |
return (Translation) super.clone(); |
44 |
} |
} |
51 |
} |
} |
52 |
|
|
53 |
/** |
/** |
54 |
* Set up the {@link Translation}-system to use language. |
* Set up the {@link Translation}-system to use language. If a change is performed, events are fired to listeners. Nothing is done if the new language equals the old language. |
55 |
* @param activeLang |
* |
56 |
|
* @param newLang The ISO Code of the new active language |
57 |
*/ |
*/ |
58 |
public static void setActiveLang(String activeLang) { |
public static void setActiveLang(String newLang) { |
59 |
if (!I8NUtil.isValidISOLangCode(activeLang)) { |
if (getActiveLang().equals(newLang)) { |
60 |
throw new IllegalArgumentException("'"+activeLang+"' is not a valid ISO language code."); |
return; |
61 |
|
} |
62 |
|
|
63 |
|
if (!I8NUtil.isValidISOLangCode(newLang)) { |
64 |
|
throw new IllegalArgumentException("'"+newLang+"' is not a valid ISO language code."); |
65 |
} |
} |
66 |
|
|
67 |
Locale.setDefault(new Locale(activeLang)); |
Locale newLocale = new Locale(newLang); |
68 |
Translation.activeLang = activeLang; |
Locale.setDefault(newLocale); |
69 |
log.info("Translation-system switched to "+activeLang); |
|
70 |
|
/** |
71 |
|
* Setting default locale for Swing JComponents to work around bug |
72 |
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
73 |
|
*/ |
74 |
|
JComponent.setDefaultLocale(newLocale); |
75 |
|
|
76 |
|
Translation.activeLang = newLang; |
77 |
|
|
78 |
|
fireChangeEvents(); |
79 |
|
|
80 |
|
log.info("skrueger.i8n.Translation switched ActiveLang to "+newLang); |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* Initilises a new {@link Translation} with a default translation. |
* Initializes a new {@link Translation} with a default translation. |
85 |
* Other translations may be added later. |
* Other translations may be added later. |
86 |
* |
* |
87 |
* @param defaultTranslation |
* @param defaultTranslation |
97 |
} |
} |
98 |
|
|
99 |
/** |
/** |
100 |
* Initilises a new {@link Translation}, an uses the given String to |
* Initializes a new {@link Translation}, an uses the given String to |
101 |
* initialise the {@link Translation} for all languages codes passed. |
* initialize the {@link Translation} for all languages codes passed. |
102 |
* |
* |
103 |
* The translations can be cahnged later |
* The translations can be changed later |
104 |
*/ |
*/ |
105 |
public Translation(List<String> languages, String defaultTranslation) { |
public Translation(List<String> languages, String defaultTranslation) { |
106 |
// put(DEFAULT_KEY, defaultTranslation); |
// put(DEFAULT_KEY, defaultTranslation); |
217 |
} |
} |
218 |
return backup; |
return backup; |
219 |
} |
} |
220 |
|
|
221 |
|
|
222 |
|
/** |
223 |
|
* {@link PropertyChangeListener} can be registered to be informed when the |
224 |
|
* {@link Locale} changed. |
225 |
|
* |
226 |
|
* @param propertyChangeListener |
227 |
|
*/ |
228 |
|
public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) { |
229 |
|
listeners.add(propertyChangeListener); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
|
* Informs all registered {@link PropertyChangeListener}s about a change of the |
234 |
|
* the {@link Locale}. |
235 |
|
*/ |
236 |
|
public static void fireChangeEvents() { |
237 |
|
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY, |
238 |
|
null, getActiveLang()); |
239 |
|
for (PropertyChangeListener pcl : listeners) { |
240 |
|
if (pcl != null) |
241 |
|
pcl.propertyChange(pce); |
242 |
|
} |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
} |
} |