1 |
mojays |
2 |
package skrueger.i8n; |
2 |
alfonx |
34 |
import java.beans.PropertyChangeEvent; |
3 |
|
|
import java.beans.PropertyChangeListener; |
4 |
|
|
import java.util.ArrayList; |
5 |
mojays |
2 |
import java.util.HashMap; |
6 |
|
|
import java.util.List; |
7 |
|
|
import java.util.Locale; |
8 |
|
|
|
9 |
alfonx |
20 |
import javax.swing.JComponent; |
10 |
|
|
|
11 |
mojays |
2 |
import org.apache.log4j.Logger; |
12 |
|
|
|
13 |
|
|
/** |
14 |
|
|
* Represents a {@link HashMap} of translations. |
15 |
|
|
* toString() returns the appropriate translation |
16 |
|
|
* |
17 |
|
|
* @author @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
18 |
|
|
*/ |
19 |
|
|
|
20 |
alfonx |
34 |
public class Translation extends HashMap<String, String>{ |
21 |
|
|
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
22 |
mojays |
2 |
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
23 |
|
|
public static final String DEFAULT_KEY = "default"; |
24 |
|
|
static final Logger log = Logger.getLogger( Translation.class ); |
25 |
|
|
static String activeLang = "fr"; |
26 |
|
|
|
27 |
alfonx |
34 |
static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>(); |
28 |
|
|
|
29 |
mojays |
2 |
static { |
30 |
|
|
|
31 |
|
|
//TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht |
32 |
|
|
|
33 |
|
|
// Get default locale |
34 |
|
|
Locale locale = Locale.getDefault(); |
35 |
|
|
setActiveLang(locale.getLanguage()); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
@Override |
39 |
alfonx |
34 |
/** |
40 |
|
|
* @comment To make a copy of a translation see methods toOneLine() and fromOneLine() |
41 |
|
|
*/ |
42 |
mojays |
2 |
public Translation clone() { |
43 |
|
|
return (Translation) super.clone(); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
/** |
47 |
|
|
* Get the two-letter language sting that is active |
48 |
|
|
*/ |
49 |
|
|
public static String getActiveLang() { |
50 |
|
|
return activeLang; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
|
|
* Set up the {@link Translation}-system to use language. |
55 |
|
|
* @param activeLang |
56 |
|
|
*/ |
57 |
|
|
public static void setActiveLang(String activeLang) { |
58 |
|
|
if (!I8NUtil.isValidISOLangCode(activeLang)) { |
59 |
|
|
throw new IllegalArgumentException("'"+activeLang+"' is not a valid ISO language code."); |
60 |
|
|
} |
61 |
|
|
|
62 |
alfonx |
20 |
Locale newLocale = new Locale(activeLang); |
63 |
|
|
Locale.setDefault(newLocale); |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* Setting default locale for Swing JComponents to work around bug |
67 |
|
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
68 |
|
|
*/ |
69 |
|
|
JComponent.setDefaultLocale(newLocale); |
70 |
|
|
|
71 |
mojays |
2 |
Translation.activeLang = activeLang; |
72 |
alfonx |
34 |
|
73 |
|
|
fireChangeEvents(); |
74 |
|
|
|
75 |
|
|
log.info("skrueger.i8n.Translation switched ActiveLang to "+activeLang); |
76 |
mojays |
2 |
} |
77 |
|
|
|
78 |
|
|
/** |
79 |
alfonx |
34 |
* Initializes a new {@link Translation} with a default translation. |
80 |
mojays |
2 |
* Other translations may be added later. |
81 |
|
|
* |
82 |
|
|
* @param defaultTranslation |
83 |
|
|
* |
84 |
|
|
* @deprecated SK: The concept of the default translation doesn't seem so nice to me anymore.. |
85 |
|
|
* I would prefer the default translation to be set for all valid languages. |
86 |
|
|
* |
87 |
|
|
* @see public Translation(List<String> languages, String defaultTranslation) { |
88 |
|
|
* |
89 |
|
|
*/ |
90 |
|
|
public Translation(String defaultTranslation) { |
91 |
|
|
put(DEFAULT_KEY, defaultTranslation); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
alfonx |
34 |
* Initializes a new {@link Translation}, an uses the given String to |
96 |
|
|
* initialize the {@link Translation} for all languages codes passed. |
97 |
mojays |
2 |
* |
98 |
alfonx |
34 |
* The translations can be changed later |
99 |
mojays |
2 |
*/ |
100 |
|
|
public Translation(List<String> languages, String defaultTranslation) { |
101 |
|
|
// put(DEFAULT_KEY, defaultTranslation); |
102 |
|
|
if (languages == null) { |
103 |
|
|
put(DEFAULT_KEY, defaultTranslation); |
104 |
|
|
} |
105 |
|
|
else for (String code : languages){ |
106 |
|
|
put(code, defaultTranslation); |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* Sometimes Translations are optional, like for keywords. |
112 |
|
|
*/ |
113 |
|
|
public Translation() { |
114 |
|
|
super(); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/** |
118 |
|
|
* Fills the {@link Translation} with the values coded into the String |
119 |
|
|
* Format of {@link String} is: "de{Baum}en{tree}" |
120 |
|
|
* <p> |
121 |
|
|
* <ul> |
122 |
|
|
* <li> If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set. |
123 |
|
|
* <li> If format can't be recognized, the {@link String} is interpreted as the translation in the <code>{@value #DEFAULT_KEY}</code> language |
124 |
|
|
* |
125 |
|
|
* @author Stefan Alfons Krüger |
126 |
|
|
*/ |
127 |
|
|
public void fromOneLine( final String oneLineCoded) { |
128 |
|
|
clear(); |
129 |
|
|
if ( (oneLineCoded == null) || (oneLineCoded.equals("")) ) { |
130 |
|
|
put(DEFAULT_KEY,NO_TRANSLATION); |
131 |
|
|
return; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
if (oneLineCoded.indexOf("}") == -1) { |
135 |
|
|
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
136 |
|
|
put(DEFAULT_KEY,oneLineCoded); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
String eatUp = oneLineCoded; |
140 |
|
|
while ( eatUp.indexOf("}") != -1) { |
141 |
|
|
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
142 |
|
|
|
143 |
|
|
// log.debug("substring = "+substring); |
144 |
|
|
String key = substring.substring(0, substring.indexOf("{") ); |
145 |
|
|
String value = substring.substring(substring.indexOf("{")+1, substring.length() ); |
146 |
|
|
// log.debug("key="+key); |
147 |
|
|
// log.debug("value="+value); |
148 |
|
|
put(key,value); |
149 |
|
|
eatUp = eatUp.substring(eatUp.indexOf("}")+1); |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
/** |
154 |
|
|
* Exports the Translations to a String of the Format: "de{Baum}en{tree}" |
155 |
|
|
* @author Stefan Alfons Krüger |
156 |
|
|
*/ |
157 |
|
|
public String toOneLine(){ |
158 |
|
|
StringBuffer oneLine = new StringBuffer(); |
159 |
|
|
for (String key: keySet()) { |
160 |
|
|
oneLine.append(key+"{"+ get(key) + "}"); |
161 |
|
|
} |
162 |
|
|
return oneLine.toString(); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* Returns the right translation by using the {@link #activeLang} field. |
167 |
|
|
* If no translation is set, an ugly String {@link #NO_TRANSLATION} will re returned. This might be changed for the final release. |
168 |
|
|
* If the correct language was not found, any entry in the {@link Translation} {@link HashMap} will be returned, that contains |
169 |
|
|
* more than an empty string. |
170 |
|
|
*/ |
171 |
|
|
@Override |
172 |
|
|
public String toString(){ |
173 |
|
|
if ( get(activeLang) != null ) { |
174 |
|
|
return get(activeLang); |
175 |
|
|
} |
176 |
|
|
//**************************************************************************** |
177 |
|
|
// MS: The ISDSS needs the concept of the default lang!! So I took the |
178 |
|
|
// following in again!! |
179 |
|
|
//**************************************************************************** |
180 |
|
|
// else return ""; |
181 |
|
|
// //**************************************************************************** |
182 |
|
|
// // The following is commented out.. the concept of the default lang seems to be bad.... |
183 |
|
|
// //**************************************************************************** |
184 |
|
|
// MS: |
185 |
|
|
else { |
186 |
|
|
if ( get(DEFAULT_KEY) != null ) { |
187 |
|
|
// log.debug("default lng returned, cuz the translation to "+activeLang+" was not found. Schmeiss raus martin, wenn du das mit der default trans geklärt hast."); |
188 |
|
|
return get(DEFAULT_KEY); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
// log.debug("return first best <> '' "); |
192 |
|
|
if (size() > 0) |
193 |
|
|
for ( String s : values() ) { |
194 |
|
|
if ( (s != null) && (s.trim().length()>0) ) |
195 |
|
|
return s; |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
log.warn("No translation found!"); |
199 |
|
|
return NO_TRANSLATION; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
/** |
203 |
|
|
* Copy this {@link Translation} to another {@link Translation} |
204 |
|
|
* e.g. for editing |
205 |
|
|
* |
206 |
|
|
* @return the destination {@link Translation} |
207 |
|
|
*/ |
208 |
|
|
public Translation copy(Translation backup) { |
209 |
|
|
if (backup == null) throw new IllegalArgumentException("Target translation may not be null."); |
210 |
|
|
for (String s : keySet() ) { |
211 |
|
|
backup.put(s, get(s) ); |
212 |
|
|
} |
213 |
|
|
return backup; |
214 |
|
|
} |
215 |
alfonx |
34 |
|
216 |
|
|
|
217 |
|
|
/** |
218 |
|
|
* {@link PropertyChangeListener} can be registered to be informed when the |
219 |
|
|
* {@link Locale} changed. |
220 |
|
|
* |
221 |
|
|
* @param propertyChangeListener |
222 |
|
|
*/ |
223 |
|
|
public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) { |
224 |
|
|
listeners.add(propertyChangeListener); |
225 |
|
|
} |
226 |
mojays |
2 |
|
227 |
alfonx |
34 |
/** |
228 |
|
|
* Informs all registered {@link PropertyChangeListener}s about a change of the |
229 |
|
|
* the {@link Locale}. |
230 |
|
|
*/ |
231 |
|
|
public static void fireChangeEvents() { |
232 |
|
|
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY, |
233 |
|
|
null, getActiveLang()); |
234 |
|
|
for (PropertyChangeListener pcl : listeners) { |
235 |
|
|
if (pcl != null) |
236 |
|
|
pcl.propertyChange(pce); |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
|
241 |
mojays |
2 |
} |