/[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 44 by alfonx, Tue Apr 14 20:23:41 2009 UTC revision 1097 by alfonx, Sun Oct 10 21:14:05 2010 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 LOCALECHANGE_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 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          }           */
132            public static void setActiveLang(String newLang) {
133          /**                  setActiveLang(newLang, true);
134           * Fills the {@link Translation} with the values coded into the String          }
135           * Format of {@link String} is: "de{Baum}en{tree}"  
136           * <p>          /**
137           * <ul>           * Set up the {@link Translation}-system to use language. If a change is
138           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.           * performed, events are fired to listeners. Nothing is done if the new
139           * <li>If format can't be recognized, the {@link String} is interpreted as           * language equals the old language.
140           * the translation in the <code>{@value #DEFAULT_KEY}</code> language           *
141           *           * @param newLang
142           * @author Stefan Alfons Krüger           *            The ISO Code of the new active language
143           */           *
144          public void fromOneLine(final String oneLineCoded) {           * @param setDefaultLocale
145                  clear();           *            Shall the system's default locale be changed?
146                  if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {           */
147                          put(DEFAULT_KEY, "");          public static void setActiveLang(String newLang, boolean setDefaultLocale) {
148                          return;                  if (getActiveLang().equals(newLang)) {
149                  }                          return;
150                    }
151                  if (oneLineCoded.indexOf("}") == -1) {  
152                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");                  if (!I8NUtil.isValidISOLangCode(newLang)) {
153                          put(DEFAULT_KEY, oneLineCoded);                          throw new IllegalArgumentException("'" + newLang
154                  }                                          + "' is not a valid ISO language code.");
155                    }
156                  String eatUp = oneLineCoded;  
157                  while (eatUp.indexOf("}") != -1) {                  Translation.activeLang = newLang;
158                          String substring = eatUp.substring(0, eatUp.indexOf("}"));                  fireActiveLangChangeEvents();
159    
160                          // log.debug("substring = "+substring);                  Locale newLocale = new Locale(newLang);
161                          String key = substring.substring(0, substring.indexOf("{"));                  if (setDefaultLocale) {
162                          String value = substring.substring(substring.indexOf("{") + 1,  
163                                          substring.length());                          Locale.setDefault(newLocale);
164                          // log.debug("key="+key);                          /**
165                          // log.debug("value="+value);                           * Setting default locale for Swing JComponents to work around bug
166                          put(key, value);                           * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
167                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);                           */
168                  }                          JComponent.setDefaultLocale(newLocale);
169          }  
170                            fireLocaleChangeEvents();
171          /**  
172           * Exports the Translations to a String of the Format: "de{Baum}en{tree}"                          LOGGER.info(Translation.class.getName()
173           *                                          + " switched ActiveLang and Locale to " + newLang);
174           * @author Stefan Alfons Krüger                  } else {
175           */                          LOGGER.info(Translation.class.getName()
176          public String toOneLine() {                                          + " switched ActiveLang to " + newLang);
177                  StringBuffer oneLine = new StringBuffer();                  }
178                  for (String key : keySet()) {  
179                          oneLine.append(key + "{" + get(key) + "}");          }
180                  }  
181                  return oneLine.toString();          /**
182          }           * Initializes a new {@link Translation} with a default translation if a
183             * simple text is passed. If a "oneLine" text is parsed, it is interpreted.
184          /**           * Other translations may be added later - this is a HashMap<br/>
185           * Returns the right translation by using the {@link #activeLang} field. If           *
186           * no translation is set, an ugly String {@link #NO_TRANSLATION} will re           * @param defaultTranslation
187           * returned. This might be changed for the final release. If the correct           *
188           * language was not found, any entry in the {@link Translation}           * @see public Translation(List<String> languages, String
189           * {@link HashMap} will be returned, that contains more than an empty           *      defaultTranslation) {
190           * string.           *
191           */           */
192          @Override          public Translation(String defaultTranslation) {
193          public String toString() {  
194                  if (get(activeLang) != null) {                  fromOneLine(defaultTranslation);
195                          return get(activeLang);  
196                  }          }
197                  // ****************************************************************************  
198                  // MS: The ISDSS needs the concept of the default lang!! So I took the          /**
199                  // following in again!!           * Initializes a new {@link Translation}, an uses the given String to
200                  // ****************************************************************************           * initialize the {@link Translation} for all languages codes passed. The
201                  // else return "";           * translations can be changed later. This class is not immutable.
202                  // //****************************************************************************           *
203                  // // The following is commented out.. the concept of the default lang           * @param languages
204                  // seems to be bad....           *            if empty or null, the given default {@link Translation} till
205                  // //****************************************************************************           *            be stored under a special key {@link #DEFAULT_KEY}
206                  // MS:           *
207                  else {           *
208                          if (get(DEFAULT_KEY) != null) {           */
209                                  // 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.");          public Translation(List<String> languages, String defaultTranslation) {
210                                  return get(DEFAULT_KEY);                  if (languages == null || languages.isEmpty()) {
211                          }                          put(DEFAULT_KEY, defaultTranslation);
212                    } else
213                          // log.debug("return first best <> '' ");                          for (String code : languages) {
214                          if (size() > 0)                                  put(code, defaultTranslation);
215                                  for (String s : values()) {                          }
216                                          if ((s != null) && (s.trim().length() > 0))          }
217                                                  return s;  
218                                  }          /**
219                  }           * Sometimes Translations are optional, like for keywords.
220                  log.warn("No translation found!");           */
221                  return NO_TRANSLATION;          public Translation() {
222          }          }
223    
224          /**          /**
225           * Copy this {@link Translation} to another {@link Translation} e.g. for           * Fills the {@link Translation} with the values coded into the String
226           * editing           * Format of {@link String} is: "de{Baum}en{tree}"
227           *           * <p>
228           * @return the destination {@link Translation}           * <ul>
229           */           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
230          public Translation copy(Translation backup) {           * <li>If format can't be recognized, the {@link String} is interpreted as
231                  if (backup == null)           * the translation in the <code>{@value #DEFAULT_KEY}</code> language
232                          throw new IllegalArgumentException(           *
233                                          "Target translation may not be null.");           * @author Stefan Alfons Tzeggai
234                  for (String s : keySet()) {           */
235                          backup.put(s, get(s));          public void fromOneLine(final String oneLineCoded) {
236                  }  
237                  return backup;                  clear();
238          }  
239                    try {
240          /**  
241           * {@link PropertyChangeListener} can be registered to be informed when the                          if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
242           * {@link Locale} changed.                                  put(DEFAULT_KEY, "");
243           *                                  return;
244           * @param propertyChangeListener                          }
245           */  
246          public static void addLocaleChangeListener(                          if (oneLineCoded.indexOf("}") == -1) {
247                          PropertyChangeListener propertyChangeListener) {                                  // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
248                  listeners.add(propertyChangeListener);                                  put(DEFAULT_KEY, oneLineCoded);
249          }                          }
250    
251          /**                          String eatUp = oneLineCoded;
252           * Informs all registered {@link PropertyChangeListener}s about a change of                          while (eatUp.indexOf("}") != -1) {
253           * the the {@link Locale}.                                  String substring = eatUp.substring(0, eatUp.indexOf("}"));
254           */  
255          public static void fireChangeEvents() {                                  // log.debug("substring = "+substring);
256                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(                                  String key = substring.substring(0, substring.indexOf("{"));
257                                  new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,                                  String value = substring.substring(substring.indexOf("{") + 1,
258                                  null, getActiveLang());                                                  substring.length());
259                  for (PropertyChangeListener pcl : listeners) {                                  // log.debug("key="+key);
260                          if (pcl != null)                                  // log.debug("value="+value);
261                                  pcl.propertyChange(pce);                                  put(key, value);
262                  }                                  eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
263          }                          }
264                    } catch (Exception e) {
265                            LOGGER.warn("Error while reading the oneLineCode '" + oneLineCoded
266  }                                          + "'", e);
267                            LOGGER.warn("Translation will be empty!");
268                    }
269            }
270    
271            /**
272             * Exports the Translations to a String of the Format: "de{Baum}en{tree}"
273             *
274             * @author Stefan Alfons Tzeggai
275             */
276            public String toOneLine() {
277                    return I8NUtil.toOneLine(this);
278            }
279    
280            /**
281             * Returns the correct translation by using the {@link #activeLang} field.
282             * If no translation is set, an ugly String {@link #NO_TRANSLATION} will re
283             * returned. This might be changed for the final release. If the correct
284             * language was not found, any entry in the {@link Translation}
285             * {@link HashMap} will be returned, that contains more than an empty
286             * string.
287             */
288            @Override
289            public String toString() {
290                    final String string = get(activeLang);
291                    if (string != null) {
292                            return get(activeLang);
293                    }
294                    // ****************************************************************************
295                    // MS: The ISDSS needs the concept of the default lang!! So I took the
296                    // following in again!!
297                    // ****************************************************************************
298                    // else return "";
299                    // //****************************************************************************
300                    // // ST: The following is commented out.. the concept of the default
301                    // lang
302                    // seems to be bad....
303                    // //****************************************************************************
304                    // MS:
305                    else {
306                            if (get(DEFAULT_KEY) != null) {
307                                    return get(DEFAULT_KEY);
308                            }
309    
310                            // log.debug("return first best <> '' ");
311                            if (size() > 0)
312                                    for (String s : values()) {
313                                            if ((s != null) && (s.trim().length() > 0))
314                                                    return s;
315                                    }
316                    }
317                    // log.warn("No translation found!");
318                    return NO_TRANSLATION;
319            }
320    
321            /**
322             * {@link PropertyChangeListener} can be registered to be informed when the
323             * {@link Locale} changed.<br>
324             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
325             * reference to the listener or it will be removed!
326             *
327             * @param propertyChangeListener
328             *            A {@link PropertyChangeListener} that will be called when
329             *            {@link #setActiveLang(String)} changes the language.
330             */
331            public static void addLocaleChangeListener(
332                            PropertyChangeListener propertyChangeListener) {
333                    listenersLocaleChange.add(propertyChangeListener);
334            }
335    
336            /**
337             * {@link PropertyChangeListener} can be registered to be informed when the
338             * {@link Locale} changed.<br>
339             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
340             * reference to the listener or it will be removed!
341             *
342             * @param propertyChangeListener
343             *            A {@link PropertyChangeListener} that will be called when
344             *            {@link #setActiveLang(String)} changes the language.
345             */
346            public static boolean removeLocaleChangeListener(
347                            PropertyChangeListener propertyChangeListener) {
348                    return listenersLocaleChange.remove(propertyChangeListener);
349            }
350    
351            /**
352             * {@link PropertyChangeListener} can be registered to be informed when the
353             * {@link Locale} changed.<br>
354             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
355             * reference to the listener or it will be removed!
356             *
357             * @param propertyChangeListener
358             *            A {@link PropertyChangeListener} that will be called when
359             *            {@link #setActiveLang(String)} changes the language.
360             */
361            public static void addActiveLangChangeListener(
362                            PropertyChangeListener propertyChangeListener) {
363                    listenersActiveLangChange.add(propertyChangeListener);
364            }
365    
366            /**
367             * {@link PropertyChangeListener} can be registered to be informed when the
368             * {@link Locale} changed.<br>
369             * The listeners are kept in a {@link WeakHashMap}, so you have to keep a
370             * reference to the listener or it will be removed!
371             *
372             * @param propertyChangeListener
373             *            A {@link PropertyChangeListener} that will be called when
374             *            {@link #setActiveLang(String)} changes the language.
375             */
376            public static boolean removeActiveLangListener(
377                            PropertyChangeListener propertyChangeListener) {
378                    return listenersActiveLangChange.remove(propertyChangeListener);
379            }
380    
381            /**
382             * Informs all registered {@link PropertyChangeListener}s about a change of
383             * the the {@link Locale}.
384             */
385            public static void fireLocaleChangeEvents() {
386                    PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
387                                    new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
388                                    null, getActiveLang());
389                    for (PropertyChangeListener pcl : listenersLocaleChange) {
390                            if (pcl != null)
391                                    pcl.propertyChange(pce);
392                    }
393            }
394    
395            /**
396             * Informs all registered {@link PropertyChangeListener}s about a change of
397             * the the {@link Locale}.
398             */
399            public static void fireActiveLangChangeEvents() {
400                    PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
401                                    new ArrayList<String>(), "fakeSource"),
402                                    ACTIVELANG_CHANGE_PROPERTY, null, getActiveLang());
403                    for (PropertyChangeListener pcl : listenersActiveLangChange) {
404                            if (pcl != null)
405                                    pcl.propertyChange(pce);
406                    }
407            }
408    
409            /**
410             * The listeneras are stored in a {@link WeakHashSet}! So you HAVE TO KEEP a
411             * reference as long as you need the listener.
412             */
413            public void addTranslationChangeListener(ActionListener actionListener) {
414                    if (changeListeners.add(actionListener)) {
415                            // LOGGER
416                            // .debug("registering a new translationChangeActionListener in the WeakHashSet");
417                    }
418            }
419    
420            /**
421             * The listeneras are stored in a {@link WeakHashSet}! You don't have to
422             * remove the listener, as long as you throw away the reference to the
423             * listener.
424             */
425            public boolean removeTranslationChangeListener(ActionListener actionListener) {
426                    return changeListeners.remove(actionListener);
427            }
428    
429            public void fireTranslationChangedEvents(String lang) {
430                    ActionEvent ae = new ActionEvent(this, new Random().nextInt(), lang);
431    
432                    final Iterator<ActionListener> iterator = changeListeners.iterator();
433                    while (iterator.hasNext()) {
434                            ActionListener al = iterator.next();
435                            al.actionPerformed(ae);
436                    }
437            }
438    
439            @Override
440            public String put(String lang, String value) {
441                    String result = super.put(lang, value);
442                    fireTranslationChangedEvents(lang);
443                    return result;
444            }
445    
446            public void fromOneLine(InternationalString iString) {
447                    if (iString != null)
448                            fromOneLine(iString.toString());
449                    else
450                            fromOneLine((String) null);
451            }
452    
453            /**
454             * Copy this {@link Translation} to another {@link Translation} e.g. for
455             * editing and return the target.
456             *
457             * @return the destination {@link Translation}
458             */
459            @Override
460            public Translation copyTo(Translation translation2) {
461                    if (translation2 == null)
462                            return copy();
463    
464                    translation2.fromOneLine(toOneLine());
465                    return translation2;
466            }
467    
468            @Override
469            public Translation copy() {
470                    return copyTo(new Translation());
471            }
472    
473            /**
474             * Checks if the {@link String}s stored in the {@link Translation} are all
475             * valid.
476             *
477             * @return <code>true</code> if all good
478             */
479            public static boolean checkValid(Translation translationToCheck) {
480    
481                    for (String l : translationToCheck.values()) {
482    
483                            if (l.contains("{") || l.contains("}")) {
484    
485                                    return false;
486                            }
487                    }
488                    return true;
489            }
490    
491            /**
492             * Goes through the available languages of the FIRST registered
493             * {@link ResourceProvider} and set the active locale to the fist match.
494             *
495             * @param fireChangeEvent
496             *            if <code>true</code>, a Translation.fireLocaleChangeEvents()
497             *            is issued.
498             *
499             * @return
500             */
501            public static boolean setFirstmatchingLanguage(List<String> languages,
502                            boolean fireChangeEvent) {
503    
504                    SortableVector<ResourceProvider> registeredResourceProvider = ResourceProvider
505                                    .getRegisteredResourceProvider();
506                    Set<Locale> available = ResourceProvider.getAvailableLocales(
507                                    registeredResourceProvider.get(0), true);
508    
509                    for (String l : languages) {
510                            for (Locale loc : available) {
511                                    if (loc.getLanguage().equals(l)) {
512                                            Translation.setActiveLang(l);
513                                            if (fireChangeEvent)
514                                                    Translation.fireLocaleChangeEvents();
515                                            return true;
516                                    }
517                            }
518                    }
519    
520                    return false;
521    
522            }
523    
524            /**
525             * Returns the translation in a requested language
526             */
527            public String toString(String lang) {
528                    return get(lang);
529            }
530    }

Legend:
Removed from v.44  
changed lines
  Added in v.1097

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26