/[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 2 by mojays, Tue Feb 24 22:43:52 2009 UTC revision 38 by alfonx, Sun Apr 5 15:06:56 2009 UTC
# Line 1  Line 1 
1  package skrueger.i8n;  package skrueger.i8n;
2    import java.beans.PropertyChangeEvent;
3    import java.beans.PropertyChangeListener;
4    import java.util.ArrayList;
5  import java.util.HashMap;  import java.util.HashMap;
6  import java.util.List;  import java.util.List;
7  import java.util.Locale;  import java.util.Locale;
8    
9    import javax.swing.JComponent;
10    
11  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
12    
13  /**  /**
# Line 12  import org.apache.log4j.Logger; Line 17  import org.apache.log4j.Logger;
17   * @author @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
18   */   */
19    
20  public class Translation extends HashMap<String, String> {  public class Translation extends HashMap<String, String>{
21            public static final String LOCALECHANGE_PROPERTY = "localechange";
22          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
23          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
24          static final Logger log = Logger.getLogger( Translation.class );          static final Logger log = Logger.getLogger( Translation.class );
25          static String activeLang = "fr";          static String activeLang = "fr";
26    
27            static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
28            
29          static {          static {
30                                    
31                  //TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht                  //TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht
# Line 28  public class Translation extends HashMap Line 36  public class Translation extends HashMap
36          }          }
37                    
38          @Override          @Override
39            /**
40             * @comment To make a copy of a translation see methods toOneLine() and fromOneLine()
41             */
42          public Translation clone() {          public Translation clone() {
43                  return (Translation) super.clone();                  return (Translation) super.clone();
44          }          }
# Line 40  public class Translation extends HashMap Line 51  public class Translation extends HashMap
51          }          }
52    
53          /**          /**
54           * Set up the {@link Translation}-system to use language.           * 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.
55           * @param activeLang           *
56             * @param newLang The ISO Code of the new active language
57           */           */
58          public static void setActiveLang(String activeLang) {          public static void setActiveLang(String newLang) {
59                  if (!I8NUtil.isValidISOLangCode(activeLang)) {                  if (getActiveLang().equals(newLang)) {
60                          throw new IllegalArgumentException("'"+activeLang+"' is not a valid ISO language code.");                          return;
61                    }
62                    
63                    if (!I8NUtil.isValidISOLangCode(newLang)) {
64                            throw new IllegalArgumentException("'"+newLang+"' is not a valid ISO language code.");
65                  }                  }
66    
67                  Locale.setDefault(new Locale(activeLang));                  Locale newLocale = new Locale(newLang);
68                  Translation.activeLang = activeLang;                  Locale.setDefault(newLocale);
69                  log.info("Translation-system switched to "+activeLang);                  
70                    /**
71                     * Setting default locale for Swing JComponents to work around bug
72                     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
73                     */
74                    JComponent.setDefaultLocale(newLocale);
75                    
76                    Translation.activeLang = newLang;
77                    
78                    fireChangeEvents();
79                    
80                    log.info("skrueger.i8n.Translation switched ActiveLang to "+newLang);
81          }          }
82    
83          /**          /**
84           * Initilises a new {@link Translation} with a default translation.           * Initializes a new {@link Translation} with a default translation.
85           * Other translations may be added later.           * Other translations may be added later.
86           *           *
87           * @param defaultTranslation           * @param defaultTranslation
# Line 70  public class Translation extends HashMap Line 97  public class Translation extends HashMap
97          }          }
98    
99          /**          /**
100           * Initilises a new {@link Translation}, an uses the given String to           * Initializes a new {@link Translation}, an uses the given String to
101           * initialise the {@link Translation} for all languages codes passed.           * initialize the {@link Translation} for all languages codes passed.
102           *           *
103           * The translations can be cahnged later           * The translations can be changed later
104           */           */
105          public Translation(List<String> languages, String defaultTranslation) {          public Translation(List<String> languages, String defaultTranslation) {
106                  // put(DEFAULT_KEY, defaultTranslation);                  // put(DEFAULT_KEY, defaultTranslation);
# Line 81  public class Translation extends HashMap Line 108  public class Translation extends HashMap
108                          put(DEFAULT_KEY, defaultTranslation);                          put(DEFAULT_KEY, defaultTranslation);
109                  }                  }
110                  else for (String code : languages){                  else for (String code : languages){
111                          put(code, defaultTranslation);                          if (code.equals(getActiveLang())) {
112                                    put(code, defaultTranslation);
113                            }
114                  }                  }
115          }          }
116    
# Line 190  public class Translation extends HashMap Line 219  public class Translation extends HashMap
219                  }                  }
220                  return backup;                  return backup;
221          }          }
222            
223            
224            /**
225             * {@link PropertyChangeListener} can be registered to be informed when the
226             * {@link Locale} changed.
227             *
228             * @param propertyChangeListener
229             */
230            public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) {
231                    listeners.add(propertyChangeListener);
232            }
233    
234            /**
235             * Informs all registered {@link PropertyChangeListener}s about a change of the
236             * the {@link Locale}.  
237             */
238            public static void fireChangeEvents() {
239                    PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
240                                    null, getActiveLang());
241                    for (PropertyChangeListener pcl : listeners) {
242                            if (pcl != null)
243                                    pcl.propertyChange(pce);
244                    }
245            }
246    
247    
248  }  }

Legend:
Removed from v.2  
changed lines
  Added in v.38

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26