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