/[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 38 by alfonx, Sun Apr 5 15:06:56 2009 UTC revision 186 by alfonx, Fri Jul 3 14:40:10 2009 UTC
# Line 1  Line 1 
1  package skrueger.i8n;  package skrueger.i8n;
2    
3  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
4  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
5  import java.util.ArrayList;  import java.util.ArrayList;
# Line 7  import java.util.List; Line 8  import java.util.List;
8  import java.util.Locale;  import java.util.Locale;
9    
10  import javax.swing.JComponent;  import javax.swing.JComponent;
11    import javax.swing.JTable;
12    
13  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
14    
15  /**  /**
16   * Represents a {@link HashMap} of translations.   * Represents a {@link HashMap} of translations. toString() returns the
17   * toString() returns the appropriate translation   * appropriate translation
18   *   *
19   * @author @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author @author <a href="mailto:[email protected]">Stefan Alfons
20     *         Kr&uuml;ger</a>
21   */   */
22    
23  public class Translation extends HashMap<String, String>{  public class Translation extends HashMap<String, String> {
24          public static final String LOCALECHANGE_PROPERTY = "localechange";          public static final String LOCALECHANGE_PROPERTY = "localechange";
25          public static final String NO_TRANSLATION = "NO TRANSLATION";          public static final String NO_TRANSLATION = "NO TRANSLATION";
26          public static final String DEFAULT_KEY = "default";          public static final String DEFAULT_KEY = "default";
27          static final Logger log = Logger.getLogger( Translation.class );          static final Logger log = Logger.getLogger(Translation.class);
28          static String activeLang = "fr";          static String activeLang = "fr";
29    
30          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
31            
32          static {          static {
                   
                 //TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht  
33    
34                    // Get default locale                  // TODO default aus Locale auslesen und mit möglichen vergleichen...
35              Locale locale = Locale.getDefault();                  // mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht
36    
37                    // Get default locale
38                    Locale locale = Locale.getDefault();
39                  setActiveLang(locale.getLanguage());                  setActiveLang(locale.getLanguage());
40          }          }
41            
42          @Override          @Override
43          /**          /*
44           * @comment To make a copy of a translation see methods toOneLine() and fromOneLine()           * @comment To make a copy of a translation see methods toOneLine() and
45             * fromOneLine()
46           */           */
47          public Translation clone() {          public Translation clone() {
48                  return (Translation) super.clone();                  return (Translation) super.clone();
# Line 51  public class Translation extends HashMap Line 56  public class Translation extends HashMap
56          }          }
57    
58          /**          /**
59           * 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.           * Set up the {@link Translation}-system to use language. If a change is
60             * performed, events are fired to listeners. Nothing is done if the new
61             * language equals the old language. The system's default locale is changed.
62           *           *
63           * @param newLang The ISO Code of the new active language           * @param newLang
64             *            The ISO Code of the new active language
65           */           */
66          public static void setActiveLang(String newLang) {          public static void setActiveLang(String newLang) {
67                    setActiveLang(newLang, true);
68            }
69    
70            /**
71             * Set up the {@link Translation}-system to use language. If a change is
72             * performed, events are fired to listeners. Nothing is done if the new
73             * language equals the old language.
74             *
75             * @param newLang
76             *            The ISO Code of the new active language
77             *
78             * @param setDefaultLocale
79             *            Shall the system's default locale be changed?
80             */
81            public static void setActiveLang(String newLang, boolean setDefaultLocale) {
82                  if (getActiveLang().equals(newLang)) {                  if (getActiveLang().equals(newLang)) {
83                          return;                          return;
84                  }                  }
85                    
86                  if (!I8NUtil.isValidISOLangCode(newLang)) {                  if (!I8NUtil.isValidISOLangCode(newLang)) {
87                          throw new IllegalArgumentException("'"+newLang+"' is not a valid ISO language code.");                          throw new IllegalArgumentException("'" + newLang
88                                            + "' is not a valid ISO language code.");
89                  }                  }
90    
91                  Locale newLocale = new Locale(newLang);                  Locale newLocale = new Locale(newLang);
92                  Locale.setDefault(newLocale);                  if (setDefaultLocale)
93                                            Locale.setDefault(newLocale);
94    
95                  /**                  /**
96                   * Setting default locale for Swing JComponents to work around bug                   * Setting default locale for Swing JComponents to work around bug
97                   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480                   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
98                   */                   */
99                  JComponent.setDefaultLocale(newLocale);                  JComponent.setDefaultLocale(newLocale);
100                    
101                  Translation.activeLang = newLang;                  Translation.activeLang = newLang;
102                    
103                  fireChangeEvents();                  fireChangeEvents();
104                    
105                  log.info("skrueger.i8n.Translation switched ActiveLang to "+newLang);                  log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang);
106          }          }
107    
108          /**          /**
109           * Initializes a new {@link Translation} with a default translation.           * Initializes a new {@link Translation} with a default translation if a
110           * Other translations may be added later.           * simple text is passed. If a "oneLine" text is parsed, it is interpreted.
111           *           * Other translations may be added later - this is a HashMap<br/>
112             *
113           * @param defaultTranslation           * @param defaultTranslation
114           *           *
115           * @deprecated SK: The concept of the default translation doesn't seem so nice to me anymore..           * @see public Translation(List<String> languages, String
116           * I would prefer the default translation to be set for all valid languages.           *      defaultTranslation) {
117           *           *
          * @see public Translation(List<String> languages, String defaultTranslation) {  
          *  
118           */           */
119          public Translation(String defaultTranslation) {          public Translation(String defaultTranslation) {
120                  put(DEFAULT_KEY, defaultTranslation);  
121                    fromOneLine(defaultTranslation);
122    
123          }          }
124    
125          /**          /**
126           * Initializes a new {@link Translation}, an uses the given String to           * Initializes a new {@link Translation}, an uses the given String to
127           * initialize the {@link Translation} for all languages codes passed.           * initialize the {@link Translation} for all languages codes passed.
128           *           *
129           * The translations can be changed later           * The translations can be changed later
130           */           */
131          public Translation(List<String> languages, String defaultTranslation) {          public Translation(List<String> languages, String defaultTranslation) {
132                  // put(DEFAULT_KEY, defaultTranslation);                  // put(DEFAULT_KEY, defaultTranslation);
133                  if (languages == null) {                  if (languages == null) {
134                          put(DEFAULT_KEY, defaultTranslation);                          put(DEFAULT_KEY, defaultTranslation);
135                  }                  } else
136                  else for (String code : languages){                          for (String code : languages) {
137                          if (code.equals(getActiveLang())) {                                  if (code.equals(getActiveLang())) {
138                                  put(code, defaultTranslation);                                          put(code, defaultTranslation);
139                                    }
140                          }                          }
                 }  
141          }          }
142    
143          /**          /**
144           * Sometimes Translations are optional, like for keywords.           * Sometimes Translations are optional, like for keywords.
145           */           */
146          public Translation() {          public Translation() {
                 super();  
147          }          }
148    
149          /**          /**
# Line 126  public class Translation extends HashMap Line 151  public class Translation extends HashMap
151           * Format of {@link String} is: "de{Baum}en{tree}"           * Format of {@link String} is: "de{Baum}en{tree}"
152           * <p>           * <p>
153           * <ul>           * <ul>
154           * <li> If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
155           * <li> If format can't be recognized, the {@link String} is interpreted as the translation in the <code>{@value #DEFAULT_KEY}</code> language           * <li>If format can't be recognized, the {@link String} is interpreted as
156           *           * the translation in the <code>{@value #DEFAULT_KEY}</code> language
157             *
158           * @author Stefan Alfons Krüger           * @author Stefan Alfons Krüger
159           */           */
160          public void fromOneLine( final String oneLineCoded) {          public void fromOneLine(final String oneLineCoded) {
161                  clear();                  clear();
162                  if ( (oneLineCoded == null) || (oneLineCoded.equals("")) ) {                  if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
163                          put(DEFAULT_KEY,NO_TRANSLATION);                          put(DEFAULT_KEY, "");
164                          return;                          return;
165                  }                  }
166    
167                  if (oneLineCoded.indexOf("}") == -1) {                  if (oneLineCoded.indexOf("}") == -1) {
168                  //      log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
169                          put(DEFAULT_KEY,oneLineCoded);                          put(DEFAULT_KEY, oneLineCoded);
170                  }                  }
171    
172                  String eatUp = oneLineCoded;                  String eatUp = oneLineCoded;
173                  while ( eatUp.indexOf("}") != -1) {                  while (eatUp.indexOf("}") != -1) {
174                          String substring = eatUp.substring(0, eatUp.indexOf("}"));                          String substring = eatUp.substring(0, eatUp.indexOf("}"));
175    
176  //                      log.debug("substring = "+substring);                          // log.debug("substring = "+substring);
177                          String key   = substring.substring(0, substring.indexOf("{") );                          String key = substring.substring(0, substring.indexOf("{"));
178                          String value = substring.substring(substring.indexOf("{")+1, substring.length() );                          String value = substring.substring(substring.indexOf("{") + 1,
179  //                      log.debug("key="+key);                                          substring.length());
180  //                      log.debug("value="+value);                          // log.debug("key="+key);
181                          put(key,value);                          // log.debug("value="+value);
182                          eatUp = eatUp.substring(eatUp.indexOf("}")+1);                          put(key, value);
183                            eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
184                  }                  }
185          }          }
186    
187          /**          /**
188           * Exports the Translations to a String of the Format: "de{Baum}en{tree}"           * Exports the Translations to a String of the Format: "de{Baum}en{tree}"
189             *
190           * @author Stefan Alfons Krüger           * @author Stefan Alfons Krüger
191           */           */
192          public String toOneLine(){          public String toOneLine() {
193                  StringBuffer oneLine = new StringBuffer();                  return I8NUtil.toOneLine(this);
                 for (String key: keySet()) {  
                         oneLine.append(key+"{"+ get(key) + "}");  
                 }  
                 return oneLine.toString();  
194          }          }
195    
196          /**          /**
197           * Returns the right translation by using the {@link #activeLang} field.           * Returns the right translation by using the {@link #activeLang} field. If
198           * If no translation is set, an ugly String {@link #NO_TRANSLATION} will re returned. This might be changed for the final release.           * no translation is set, an ugly String {@link #NO_TRANSLATION} will re
199           * If the correct language was not found, any entry in the {@link Translation} {@link HashMap} will be returned, that contains           * returned. This might be changed for the final release. If the correct
200           *  more than an empty string.           * language was not found, any entry in the {@link Translation}
201             * {@link HashMap} will be returned, that contains more than an empty
202             * string.
203           */           */
204          @Override          @Override
205          public String toString(){          public String toString() {
206                  if ( get(activeLang) != null ) {                  if (get(activeLang) != null) {
207                          return get(activeLang);                          return get(activeLang);
208                  }                  }
209                  //****************************************************************************                  // ****************************************************************************
210                  // MS: The ISDSS needs the concept of the default lang!! So I took the                  // MS: The ISDSS needs the concept of the default lang!! So I took the
211                  //     following in again!!                  // following in again!!
212                  //****************************************************************************                  // ****************************************************************************
213  //              else return "";                  // else return "";
214  //              //****************************************************************************                  // //****************************************************************************
215  //              // The following is commented out.. the concept of the default lang seems to be bad....                  // // The following is commented out.. the concept of the default lang
216  //              //****************************************************************************                  // seems to be bad....
217                  // MS:                  // //****************************************************************************
218                    // MS:
219                  else {                  else {
220                          if ( get(DEFAULT_KEY) != null ) {                          if (get(DEFAULT_KEY) != null) {
221  //                              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.");                                  // log.debug("default lang returned, cuz the translation to "+activeLang+" was not found. Schmeiss raus martin, wenn du das mit der default trans geklärt hast.");
222                                  return get(DEFAULT_KEY);                                  return get(DEFAULT_KEY);
223                          }                          }
224    
225                          // log.debug("return first best <> '' ");                          // log.debug("return first best <> '' ");
226                          if (size() > 0)                          if (size() > 0)
227                                  for ( String s : values() ) {                                  for (String s : values()) {
228                                          if ( (s != null) && (s.trim().length()>0) )                                          if ((s != null) && (s.trim().length() > 0))
229                                                  return s;                                                  return s;
230                                  }                                  }
231                  }                  }
# Line 207  public class Translation extends HashMap Line 234  public class Translation extends HashMap
234          }          }
235    
236          /**          /**
237           * Copy this {@link Translation} to another {@link Translation}           * Copy this {@link Translation} to another {@link Translation} e.g. for
238           * e.g. for editing           * editing
239           *           *
240           * @return the destination {@link Translation}           * @return the destination {@link Translation}
241           */           */
242          public Translation copy(Translation backup) {          public Translation copy(Translation backup) {
243                  if (backup == null) throw new IllegalArgumentException("Target translation may not be null.");                  if (backup == null)
244                  for (String s : keySet() ) {                          throw new IllegalArgumentException(
245                          backup.put(s, get(s) );                                          "Target translation may not be null.");
246                    for (String s : keySet()) {
247                            backup.put(s, get(s));
248                  }                  }
249                  return backup;                  return backup;
250          }          }
251            
           
252          /**          /**
253           * {@link PropertyChangeListener} can be registered to be informed when the           * {@link PropertyChangeListener} can be registered to be informed when the
254           * {@link Locale} changed.           * {@link Locale} changed.
255           *           *
256           * @param propertyChangeListener           * @param propertyChangeListener
257           */           */
258          public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) {          public static void addLocaleChangeListener(
259                            PropertyChangeListener propertyChangeListener) {
260                  listeners.add(propertyChangeListener);                  listeners.add(propertyChangeListener);
261          }          }
262    
263          /**          /**
264           * Informs all registered {@link PropertyChangeListener}s about a change of the           * Informs all registered {@link PropertyChangeListener}s about a change of
265           * the {@link Locale}.             * the the {@link Locale}.
266           */           */
267          public static void fireChangeEvents() {          public static void fireChangeEvents() {
268                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
269                                    new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
270                                  null, getActiveLang());                                  null, getActiveLang());
271                  for (PropertyChangeListener pcl : listeners) {                  for (PropertyChangeListener pcl : listeners) {
272                          if (pcl != null)                          if (pcl != null)
# Line 244  public class Translation extends HashMap Line 274  public class Translation extends HashMap
274                  }                  }
275          }          }
276    
   
277  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26