/[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 34 by alfonx, Sat Mar 28 18:08:42 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 48  public class Translation extends HashMap Line 59  public class Translation extends HashMap
59                          throw new IllegalArgumentException("'"+activeLang+"' is not a valid ISO language code.");                          throw new IllegalArgumentException("'"+activeLang+"' is not a valid ISO language code.");
60                  }                  }
61    
62                  Locale.setDefault(new Locale(activeLang));                  Locale newLocale = new Locale(activeLang);
63                    Locale.setDefault(newLocale);
64                    
65                    /**
66                     * Setting default locale for Swing JComponents to work around bug
67                     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
68                     */
69                    JComponent.setDefaultLocale(newLocale);
70                    
71                  Translation.activeLang = activeLang;                  Translation.activeLang = activeLang;
72                  log.info("Translation-system switched to "+activeLang);                  
73                    fireChangeEvents();
74                    
75                    log.info("skrueger.i8n.Translation switched ActiveLang to "+activeLang);
76          }          }
77    
78          /**          /**
79           * Initilises a new {@link Translation} with a default translation.           * Initializes a new {@link Translation} with a default translation.
80           * Other translations may be added later.           * Other translations may be added later.
81           *           *
82           * @param defaultTranslation           * @param defaultTranslation
# Line 70  public class Translation extends HashMap Line 92  public class Translation extends HashMap
92          }          }
93    
94          /**          /**
95           * Initilises a new {@link Translation}, an uses the given String to           * Initializes a new {@link Translation}, an uses the given String to
96           * initialise the {@link Translation} for all languages codes passed.           * initialize the {@link Translation} for all languages codes passed.
97           *           *
98           * The translations can be cahnged later           * The translations can be changed later
99           */           */
100          public Translation(List<String> languages, String defaultTranslation) {          public Translation(List<String> languages, String defaultTranslation) {
101                  // put(DEFAULT_KEY, defaultTranslation);                  // put(DEFAULT_KEY, defaultTranslation);
# Line 190  public class Translation extends HashMap Line 212  public class Translation extends HashMap
212                  }                  }
213                  return backup;                  return backup;
214          }          }
215            
216            
217            /**
218             * {@link PropertyChangeListener} can be registered to be informed when the
219             * {@link Locale} changed.
220             *
221             * @param propertyChangeListener
222             */
223            public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) {
224                    listeners.add(propertyChangeListener);
225            }
226    
227            /**
228             * Informs all registered {@link PropertyChangeListener}s about a change of the
229             * the {@link Locale}.  
230             */
231            public static void fireChangeEvents() {
232                    PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
233                                    null, getActiveLang());
234                    for (PropertyChangeListener pcl : listeners) {
235                            if (pcl != null)
236                                    pcl.propertyChange(pce);
237                    }
238            }
239    
240    
241  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26