/[schmitzm]/trunk/src/skrueger/i8n/Translation.java
ViewVC logotype

Diff of /trunk/src/skrueger/i8n/Translation.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.39  
changed lines
  Added in v.1373

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26