1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
alfonx |
862 |
* Stefan A. Tzeggai - additional utility classes |
29 |
alfonx |
244 |
******************************************************************************/ |
30 |
|
|
package skrueger.i8n; |
31 |
|
|
|
32 |
alfonx |
292 |
import java.awt.event.ActionEvent; |
33 |
|
|
import java.awt.event.ActionListener; |
34 |
alfonx |
244 |
import java.beans.PropertyChangeEvent; |
35 |
|
|
import java.beans.PropertyChangeListener; |
36 |
alfonx |
772 |
import java.io.Serializable; |
37 |
alfonx |
244 |
import java.util.ArrayList; |
38 |
|
|
import java.util.HashMap; |
39 |
alfonx |
482 |
import java.util.Iterator; |
40 |
alfonx |
244 |
import java.util.List; |
41 |
|
|
import java.util.Locale; |
42 |
alfonx |
292 |
import java.util.Random; |
43 |
alfonx |
906 |
import java.util.Set; |
44 |
alfonx |
607 |
import java.util.WeakHashMap; |
45 |
alfonx |
244 |
|
46 |
|
|
import javax.swing.JComponent; |
47 |
|
|
|
48 |
|
|
import org.apache.log4j.Logger; |
49 |
alfonx |
482 |
import org.geotools.util.WeakHashSet; |
50 |
alfonx |
409 |
import org.opengis.util.InternationalString; |
51 |
alfonx |
244 |
|
52 |
alfonx |
906 |
import schmitzm.lang.ResourceProvider; |
53 |
|
|
import schmitzm.lang.SortableVector; |
54 |
alfonx |
420 |
import skrueger.geotools.Copyable; |
55 |
|
|
|
56 |
alfonx |
244 |
/** |
57 |
|
|
* Represents a {@link HashMap} of translations. toString() returns the |
58 |
|
|
* appropriate translation |
59 |
|
|
* |
60 |
|
|
* @author @author <a href="mailto:[email protected]">Stefan Alfons |
61 |
alfonx |
888 |
* Tzeggai</a> |
62 |
alfonx |
244 |
*/ |
63 |
|
|
|
64 |
alfonx |
482 |
public class Translation extends HashMap<String, String> implements |
65 |
alfonx |
772 |
Copyable<Translation>, Serializable { |
66 |
|
|
|
67 |
|
|
private static final long serialVersionUID = -347702744122305245L; |
68 |
|
|
|
69 |
alfonx |
244 |
public static final String LOCALECHANGE_PROPERTY = "localechange"; |
70 |
|
|
public static final String NO_TRANSLATION = "NO TRANSLATION"; |
71 |
|
|
public static final String DEFAULT_KEY = "default"; |
72 |
alfonx |
486 |
static final Logger LOGGER = Logger.getLogger(Translation.class); |
73 |
alfonx |
268 |
static String activeLang = Locale.getDefault().getLanguage(); |
74 |
alfonx |
244 |
|
75 |
alfonx |
607 |
static protected WeakHashSet<PropertyChangeListener> listeners = new WeakHashSet<PropertyChangeListener>( |
76 |
|
|
PropertyChangeListener.class); |
77 |
alfonx |
244 |
|
78 |
|
|
static { |
79 |
|
|
|
80 |
|
|
// TODO default aus Locale auslesen und mit möglichen vergleichen... |
81 |
|
|
// mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht |
82 |
|
|
|
83 |
|
|
// Get default locale |
84 |
|
|
Locale locale = Locale.getDefault(); |
85 |
|
|
setActiveLang(locale.getLanguage()); |
86 |
|
|
} |
87 |
alfonx |
772 |
|
88 |
|
|
|
89 |
alfonx |
244 |
|
90 |
alfonx |
482 |
private WeakHashSet<ActionListener> actionListeners = new WeakHashSet<ActionListener>( |
91 |
|
|
ActionListener.class); |
92 |
alfonx |
292 |
|
93 |
alfonx |
244 |
@Override |
94 |
|
|
/* |
95 |
|
|
* @comment To make a copy of a translation see methods toOneLine() and |
96 |
|
|
* fromOneLine() |
97 |
|
|
*/ |
98 |
|
|
public Translation clone() { |
99 |
alfonx |
420 |
throw new RuntimeException("use copy()"); |
100 |
alfonx |
482 |
// return (Translation) super.clone(); |
101 |
alfonx |
244 |
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Get the two-letter language sting that is active |
105 |
|
|
*/ |
106 |
|
|
public static String getActiveLang() { |
107 |
|
|
return activeLang; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* Set up the {@link Translation}-system to use language. If a change is |
112 |
|
|
* performed, events are fired to listeners. Nothing is done if the new |
113 |
|
|
* language equals the old language. The system's default locale is changed. |
114 |
|
|
* |
115 |
|
|
* @param newLang |
116 |
|
|
* The ISO Code of the new active language |
117 |
|
|
*/ |
118 |
|
|
public static void setActiveLang(String newLang) { |
119 |
|
|
setActiveLang(newLang, true); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Set up the {@link Translation}-system to use language. If a change is |
124 |
|
|
* performed, events are fired to listeners. Nothing is done if the new |
125 |
|
|
* language equals the old language. |
126 |
|
|
* |
127 |
|
|
* @param newLang |
128 |
|
|
* The ISO Code of the new active language |
129 |
|
|
* |
130 |
|
|
* @param setDefaultLocale |
131 |
|
|
* Shall the system's default locale be changed? |
132 |
|
|
*/ |
133 |
|
|
public static void setActiveLang(String newLang, boolean setDefaultLocale) { |
134 |
|
|
if (getActiveLang().equals(newLang)) { |
135 |
|
|
return; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
if (!I8NUtil.isValidISOLangCode(newLang)) { |
139 |
|
|
throw new IllegalArgumentException("'" + newLang |
140 |
|
|
+ "' is not a valid ISO language code."); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
Locale newLocale = new Locale(newLang); |
144 |
|
|
if (setDefaultLocale) |
145 |
|
|
Locale.setDefault(newLocale); |
146 |
|
|
|
147 |
|
|
/** |
148 |
|
|
* Setting default locale for Swing JComponents to work around bug |
149 |
|
|
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480 |
150 |
|
|
*/ |
151 |
|
|
JComponent.setDefaultLocale(newLocale); |
152 |
|
|
|
153 |
|
|
Translation.activeLang = newLang; |
154 |
|
|
|
155 |
|
|
fireLocaleChangeEvents(); |
156 |
|
|
|
157 |
alfonx |
607 |
LOGGER.info("skrueger.i8n.Translation switched ActiveLang to " |
158 |
|
|
+ newLang); |
159 |
alfonx |
244 |
} |
160 |
|
|
|
161 |
|
|
/** |
162 |
|
|
* Initializes a new {@link Translation} with a default translation if a |
163 |
|
|
* simple text is passed. If a "oneLine" text is parsed, it is interpreted. |
164 |
|
|
* Other translations may be added later - this is a HashMap<br/> |
165 |
|
|
* |
166 |
|
|
* @param defaultTranslation |
167 |
|
|
* |
168 |
|
|
* @see public Translation(List<String> languages, String |
169 |
|
|
* defaultTranslation) { |
170 |
|
|
* |
171 |
|
|
*/ |
172 |
|
|
public Translation(String defaultTranslation) { |
173 |
|
|
|
174 |
|
|
fromOneLine(defaultTranslation); |
175 |
|
|
|
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* Initializes a new {@link Translation}, an uses the given String to |
180 |
|
|
* initialize the {@link Translation} for all languages codes passed. |
181 |
|
|
* |
182 |
|
|
* The translations can be changed later |
183 |
|
|
*/ |
184 |
|
|
public Translation(List<String> languages, String defaultTranslation) { |
185 |
|
|
// put(DEFAULT_KEY, defaultTranslation); |
186 |
|
|
if (languages == null) { |
187 |
|
|
put(DEFAULT_KEY, defaultTranslation); |
188 |
|
|
} else |
189 |
|
|
for (String code : languages) { |
190 |
alfonx |
615 |
// if (code.equals(getActiveLang())) { |
191 |
alfonx |
244 |
put(code, defaultTranslation); |
192 |
alfonx |
615 |
// } |
193 |
alfonx |
244 |
} |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/** |
197 |
|
|
* Sometimes Translations are optional, like for keywords. |
198 |
|
|
*/ |
199 |
|
|
public Translation() { |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
/** |
203 |
|
|
* Fills the {@link Translation} with the values coded into the String |
204 |
|
|
* Format of {@link String} is: "de{Baum}en{tree}" |
205 |
|
|
* <p> |
206 |
|
|
* <ul> |
207 |
|
|
* <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set. |
208 |
|
|
* <li>If format can't be recognized, the {@link String} is interpreted as |
209 |
|
|
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
210 |
|
|
* |
211 |
alfonx |
862 |
* @author Stefan Alfons Tzeggai |
212 |
alfonx |
244 |
*/ |
213 |
|
|
public void fromOneLine(final String oneLineCoded) { |
214 |
alfonx |
420 |
|
215 |
alfonx |
244 |
clear(); |
216 |
alfonx |
482 |
|
217 |
alfonx |
420 |
try { |
218 |
alfonx |
244 |
|
219 |
alfonx |
482 |
if ((oneLineCoded == null) || (oneLineCoded.equals(""))) { |
220 |
|
|
put(DEFAULT_KEY, ""); |
221 |
|
|
return; |
222 |
|
|
} |
223 |
alfonx |
244 |
|
224 |
alfonx |
482 |
if (oneLineCoded.indexOf("}") == -1) { |
225 |
|
|
// log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);"); |
226 |
|
|
put(DEFAULT_KEY, oneLineCoded); |
227 |
|
|
} |
228 |
alfonx |
244 |
|
229 |
alfonx |
482 |
String eatUp = oneLineCoded; |
230 |
|
|
while (eatUp.indexOf("}") != -1) { |
231 |
|
|
String substring = eatUp.substring(0, eatUp.indexOf("}")); |
232 |
|
|
|
233 |
|
|
// log.debug("substring = "+substring); |
234 |
|
|
String key = substring.substring(0, substring.indexOf("{")); |
235 |
|
|
String value = substring.substring(substring.indexOf("{") + 1, |
236 |
|
|
substring.length()); |
237 |
|
|
// log.debug("key="+key); |
238 |
|
|
// log.debug("value="+value); |
239 |
|
|
put(key, value); |
240 |
|
|
eatUp = eatUp.substring(eatUp.indexOf("}") + 1); |
241 |
|
|
} |
242 |
alfonx |
420 |
} catch (Exception e) { |
243 |
alfonx |
486 |
LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded |
244 |
alfonx |
482 |
+ "'", e); |
245 |
alfonx |
486 |
LOGGER.warn("Translation will be empty!"); |
246 |
alfonx |
420 |
} |
247 |
alfonx |
244 |
} |
248 |
|
|
|
249 |
|
|
/** |
250 |
|
|
* Exports the Translations to a String of the Format: "de{Baum}en{tree}" |
251 |
|
|
* |
252 |
alfonx |
862 |
* @author Stefan Alfons Tzeggai |
253 |
alfonx |
244 |
*/ |
254 |
|
|
public String toOneLine() { |
255 |
|
|
return I8NUtil.toOneLine(this); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
/** |
259 |
|
|
* Returns the right translation by using the {@link #activeLang} field. If |
260 |
|
|
* no translation is set, an ugly String {@link #NO_TRANSLATION} will re |
261 |
|
|
* returned. This might be changed for the final release. If the correct |
262 |
|
|
* language was not found, any entry in the {@link Translation} |
263 |
|
|
* {@link HashMap} will be returned, that contains more than an empty |
264 |
|
|
* string. |
265 |
|
|
*/ |
266 |
|
|
@Override |
267 |
|
|
public String toString() { |
268 |
|
|
if (get(activeLang) != null) { |
269 |
|
|
return get(activeLang); |
270 |
|
|
} |
271 |
|
|
// **************************************************************************** |
272 |
|
|
// MS: The ISDSS needs the concept of the default lang!! So I took the |
273 |
|
|
// following in again!! |
274 |
|
|
// **************************************************************************** |
275 |
|
|
// else return ""; |
276 |
|
|
// //**************************************************************************** |
277 |
|
|
// // The following is commented out.. the concept of the default lang |
278 |
|
|
// seems to be bad.... |
279 |
|
|
// //**************************************************************************** |
280 |
|
|
// MS: |
281 |
|
|
else { |
282 |
|
|
if (get(DEFAULT_KEY) != null) { |
283 |
|
|
return get(DEFAULT_KEY); |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
// log.debug("return first best <> '' "); |
287 |
|
|
if (size() > 0) |
288 |
|
|
for (String s : values()) { |
289 |
|
|
if ((s != null) && (s.trim().length() > 0)) |
290 |
|
|
return s; |
291 |
|
|
} |
292 |
|
|
} |
293 |
alfonx |
482 |
// log.warn("No translation found!"); |
294 |
alfonx |
244 |
return NO_TRANSLATION; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
/** |
298 |
|
|
* {@link PropertyChangeListener} can be registered to be informed when the |
299 |
alfonx |
607 |
* {@link Locale} changed.<br> |
300 |
|
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
301 |
|
|
* reference to the listener or it will be removed! |
302 |
alfonx |
244 |
* |
303 |
|
|
* @param propertyChangeListener |
304 |
alfonx |
607 |
* A {@link PropertyChangeListener} that will be called when |
305 |
|
|
* {@link #setActiveLang(String)} changes the language. |
306 |
alfonx |
244 |
*/ |
307 |
|
|
public static void addLocaleChangeListener( |
308 |
|
|
PropertyChangeListener propertyChangeListener) { |
309 |
|
|
listeners.add(propertyChangeListener); |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
/** |
313 |
alfonx |
607 |
* {@link PropertyChangeListener} can be registered to be informed when the |
314 |
|
|
* {@link Locale} changed.<br> |
315 |
|
|
* The listeners are kept in a {@link WeakHashMap}, so you have to keep a |
316 |
|
|
* reference to the listener or it will be removed! |
317 |
|
|
* |
318 |
|
|
* @param propertyChangeListener |
319 |
|
|
* A {@link PropertyChangeListener} that will be called when |
320 |
|
|
* {@link #setActiveLang(String)} changes the language. |
321 |
|
|
*/ |
322 |
|
|
public static boolean removeLocaleChangeListener( |
323 |
|
|
PropertyChangeListener propertyChangeListener) { |
324 |
|
|
return listeners.remove(propertyChangeListener); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
/** |
328 |
alfonx |
244 |
* Informs all registered {@link PropertyChangeListener}s about a change of |
329 |
|
|
* the the {@link Locale}. |
330 |
|
|
*/ |
331 |
|
|
public static void fireLocaleChangeEvents() { |
332 |
|
|
PropertyChangeEvent pce = new PropertyChangeEvent(new Translation( |
333 |
|
|
new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY, |
334 |
|
|
null, getActiveLang()); |
335 |
|
|
for (PropertyChangeListener pcl : listeners) { |
336 |
|
|
if (pcl != null) |
337 |
|
|
pcl.propertyChange(pce); |
338 |
|
|
} |
339 |
|
|
} |
340 |
|
|
|
341 |
alfonx |
482 |
/** |
342 |
|
|
* The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a |
343 |
|
|
* reference as long as you need the listener. |
344 |
|
|
*/ |
345 |
alfonx |
292 |
public void addTranslationChangeListener(ActionListener actionListener) { |
346 |
alfonx |
486 |
if (actionListeners.add(actionListener)) { |
347 |
alfonx |
723 |
// LOGGER |
348 |
|
|
// .debug("registering a new translationChangeActionListener in the WeakHashSet"); |
349 |
alfonx |
486 |
} |
350 |
alfonx |
292 |
} |
351 |
alfonx |
482 |
|
352 |
|
|
/** |
353 |
|
|
* The listeneras are stored in a {@link WeakHashSet}! You don't have to |
354 |
|
|
* remove the listener, as long as you throw away the reference to the |
355 |
|
|
* listener. |
356 |
|
|
*/ |
357 |
alfonx |
292 |
public boolean removeTranslationChangeListener(ActionListener actionListener) { |
358 |
|
|
return actionListeners.remove(actionListener); |
359 |
|
|
} |
360 |
|
|
|
361 |
alfonx |
300 |
public void fireTranslationChangedEvents(String lang) { |
362 |
|
|
ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang); |
363 |
alfonx |
482 |
|
364 |
|
|
final Iterator<ActionListener> iterator = actionListeners.iterator(); |
365 |
|
|
while (iterator.hasNext()) { |
366 |
|
|
ActionListener al = iterator.next(); |
367 |
|
|
al.actionPerformed(ae); |
368 |
alfonx |
292 |
} |
369 |
|
|
} |
370 |
alfonx |
482 |
|
371 |
alfonx |
292 |
@Override |
372 |
alfonx |
300 |
public String put(String lang, String value) { |
373 |
|
|
String result = super.put(lang, value); |
374 |
|
|
fireTranslationChangedEvents(lang); |
375 |
alfonx |
292 |
return result; |
376 |
|
|
} |
377 |
alfonx |
409 |
|
378 |
|
|
public void fromOneLine(InternationalString iString) { |
379 |
|
|
if (iString != null) |
380 |
|
|
fromOneLine(iString.toString()); |
381 |
alfonx |
482 |
else |
382 |
|
|
fromOneLine((String) null); |
383 |
alfonx |
409 |
} |
384 |
alfonx |
482 |
|
385 |
alfonx |
420 |
/** |
386 |
|
|
* Copy this {@link Translation} to another {@link Translation} e.g. for |
387 |
|
|
* editing |
388 |
|
|
* |
389 |
|
|
* @return the destination {@link Translation} |
390 |
|
|
*/ |
391 |
|
|
@Override |
392 |
|
|
public Translation copyTo(Translation translation2) { |
393 |
alfonx |
482 |
|
394 |
alfonx |
420 |
if (translation2 == null) |
395 |
alfonx |
482 |
// throw new IllegalArgumentException( |
396 |
|
|
// "Target translation may not be null."); |
397 |
|
|
return copy(); |
398 |
alfonx |
420 |
for (String s : keySet()) { |
399 |
|
|
translation2.put(s, get(s)); |
400 |
|
|
} |
401 |
alfonx |
482 |
|
402 |
alfonx |
420 |
return translation2; |
403 |
|
|
} |
404 |
|
|
|
405 |
|
|
@Override |
406 |
|
|
public Translation copy() { |
407 |
|
|
return copyTo(new Translation()); |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
/** |
411 |
alfonx |
482 |
* Checks if the {@link String}s stored in the {@link Translation} are all |
412 |
|
|
* valid. |
413 |
|
|
* |
414 |
alfonx |
420 |
* @return <code>true</code> if all good |
415 |
|
|
*/ |
416 |
|
|
public static boolean checkValid(Translation translationToCheck) { |
417 |
alfonx |
482 |
|
418 |
alfonx |
420 |
for (String l : translationToCheck.values()) { |
419 |
alfonx |
482 |
|
420 |
alfonx |
420 |
if (l.contains("{") || l.contains("}")) { |
421 |
alfonx |
482 |
|
422 |
alfonx |
420 |
return false; |
423 |
|
|
} |
424 |
|
|
} |
425 |
|
|
return true; |
426 |
|
|
} |
427 |
alfonx |
482 |
|
428 |
alfonx |
906 |
/** |
429 |
|
|
* Goes through the available languages of the FIRST registered {@link ResourceProvider} and set the active locale to the fist match. |
430 |
|
|
* |
431 |
|
|
* @param fireChangeEvent if <code>true</code>, a Translation.fireLocaleChangeEvents() is issued. |
432 |
|
|
|
433 |
|
|
* @return |
434 |
|
|
*/ |
435 |
|
|
public static boolean setFirstmatchingLanguage(List<String> languages, |
436 |
|
|
boolean fireChangeEvent) { |
437 |
|
|
|
438 |
|
|
SortableVector<ResourceProvider> registeredResourceProvider = ResourceProvider |
439 |
|
|
.getRegisteredResourceProvider(); |
440 |
|
|
Set<Locale> available = ResourceProvider.getAvailableLocales( |
441 |
|
|
registeredResourceProvider.get(0), true); |
442 |
|
|
|
443 |
|
|
for (String l : languages) { |
444 |
|
|
for (Locale loc : available) { |
445 |
|
|
if (loc.getLanguage().equals(l)) { |
446 |
|
|
Translation.setActiveLang(l); |
447 |
|
|
if (fireChangeEvent) |
448 |
|
|
Translation.fireLocaleChangeEvents(); |
449 |
|
|
return true; |
450 |
|
|
} |
451 |
|
|
} |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
return false; |
455 |
|
|
|
456 |
|
|
} |
457 |
alfonx |
244 |
} |