/[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 186 by alfonx, Fri Jul 3 14:40:10 2009 UTC revision 261 by mojays, Fri Jul 31 17:05:18 2009 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  import javax.swing.JTable;   * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13  import org.apache.log4j.Logger;   * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15  /**   *
16   * Represents a {@link HashMap} of translations. toString() returns the   * This program is distributed in the hope that it will be useful,
17   * appropriate translation   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   *   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * @author @author <a href="mailto:[email protected]">Stefan Alfons   * GNU General Public License for more details.
20   *         Kr&uuml;ger</a>   *
21   */   * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23  public class Translation extends HashMap<String, String> {   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24          public static final String LOCALECHANGE_PROPERTY = "localechange";   * or try this link: http://www.gnu.org/licenses/lgpl.html
25          public static final String NO_TRANSLATION = "NO TRANSLATION";   *
26          public static final String DEFAULT_KEY = "default";   * Contributors:
27          static final Logger log = Logger.getLogger(Translation.class);   *     Martin O. J. Schmitz - initial API and implementation
28          static String activeLang = "fr";   *     Stefan A. Krüger - additional utility classes
29     ******************************************************************************/
30          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();  package skrueger.i8n;
31    
32          static {  import java.beans.PropertyChangeEvent;
33    import java.beans.PropertyChangeListener;
34                  // TODO default aus Locale auslesen und mit möglichen vergleichen...  import java.util.ArrayList;
35                  // mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht  import java.util.HashMap;
36    import java.util.List;
37                  // Get default locale  import java.util.Locale;
38                  Locale locale = Locale.getDefault();  
39                  setActiveLang(locale.getLanguage());  import javax.swing.JComponent;
40          }  
41    import org.apache.log4j.Logger;
42          @Override  
43          /*  /**
44           * @comment To make a copy of a translation see methods toOneLine() and   * Represents a {@link HashMap} of translations. toString() returns the
45           * fromOneLine()   * appropriate translation
46           */   *
47          public Translation clone() {   * @author @author <a href="mailto:[email protected]">Stefan Alfons
48                  return (Translation) super.clone();   *         Kr&uuml;ger</a>
49          }   */
50    
51          /**  public class Translation extends HashMap<String, String> {
52           * Get the two-letter language sting that is active          public static final String LOCALECHANGE_PROPERTY = "localechange";
53           */          public static final String NO_TRANSLATION = "NO TRANSLATION";
54          public static String getActiveLang() {          public static final String DEFAULT_KEY = "default";
55                  return activeLang;          static final Logger log = Logger.getLogger(Translation.class);
56          }          static String activeLang = "fr";
57    
58          /**          static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
59           * 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          static {
61           * language equals the old language. The system's default locale is changed.  
62           *                  // TODO default aus Locale auslesen und mit möglichen vergleichen...
63           * @param newLang                  // mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht
64           *            The ISO Code of the new active language  
65           */                  // Get default locale
66          public static void setActiveLang(String newLang) {                  Locale locale = Locale.getDefault();
67                  setActiveLang(newLang, true);                  setActiveLang(locale.getLanguage());
68          }          }
69    
70          /**          @Override
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           * @comment To make a copy of a translation see methods toOneLine() and
73           * language equals the old language.           * fromOneLine()
74           *           */
75           * @param newLang          public Translation clone() {
76           *            The ISO Code of the new active language                  return (Translation) super.clone();
77           *          }
78           * @param setDefaultLocale  
79           *            Shall the system's default locale be changed?          /**
80           */           * Get the two-letter language sting that is active
81          public static void setActiveLang(String newLang, boolean setDefaultLocale) {           */
82                  if (getActiveLang().equals(newLang)) {          public static String getActiveLang() {
83                          return;                  return activeLang;
84                  }          }
85    
86                  if (!I8NUtil.isValidISOLangCode(newLang)) {          /**
87                          throw new IllegalArgumentException("'" + newLang           * Set up the {@link Translation}-system to use language. If a change is
88                                          + "' is not a valid ISO language code.");           * performed, events are fired to listeners. Nothing is done if the new
89                  }           * language equals the old language. The system's default locale is changed.
90             *
91                  Locale newLocale = new Locale(newLang);           * @param newLang
92                  if (setDefaultLocale)           *            The ISO Code of the new active language
93                          Locale.setDefault(newLocale);           */
94            public static void setActiveLang(String newLang) {
95                  /**                  setActiveLang(newLang, true);
96                   * Setting default locale for Swing JComponents to work around bug          }
97                   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480  
98                   */          /**
99                  JComponent.setDefaultLocale(newLocale);           * Set up the {@link Translation}-system to use language. If a change is
100             * performed, events are fired to listeners. Nothing is done if the new
101                  Translation.activeLang = newLang;           * language equals the old language.
102             *
103                  fireChangeEvents();           * @param newLang
104             *            The ISO Code of the new active language
105                  log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang);           *
106          }           * @param setDefaultLocale
107             *            Shall the system's default locale be changed?
108          /**           */
109           * Initializes a new {@link Translation} with a default translation if a          public static void setActiveLang(String newLang, boolean setDefaultLocale) {
110           * simple text is passed. If a "oneLine" text is parsed, it is interpreted.                  if (getActiveLang().equals(newLang)) {
111           * Other translations may be added later - this is a HashMap<br/>                          return;
112           *                  }
113           * @param defaultTranslation  
114           *                  if (!I8NUtil.isValidISOLangCode(newLang)) {
115           * @see public Translation(List<String> languages, String                          throw new IllegalArgumentException("'" + newLang
116           *      defaultTranslation) {                                          + "' is not a valid ISO language code.");
117           *                  }
118           */  
119          public Translation(String defaultTranslation) {                  Locale newLocale = new Locale(newLang);
120                    if (setDefaultLocale)
121                  fromOneLine(defaultTranslation);                          Locale.setDefault(newLocale);
122    
123          }                  /**
124                     * Setting default locale for Swing JComponents to work around bug
125          /**                   * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
126           * Initializes a new {@link Translation}, an uses the given String to                   */
127           * initialize the {@link Translation} for all languages codes passed.                  JComponent.setDefaultLocale(newLocale);
128           *  
129           * The translations can be changed later                  Translation.activeLang = newLang;
130           */  
131          public Translation(List<String> languages, String defaultTranslation) {                  fireLocaleChangeEvents();
132                  // put(DEFAULT_KEY, defaultTranslation);  
133                  if (languages == null) {                  log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang);
134                          put(DEFAULT_KEY, defaultTranslation);          }
135                  } else  
136                          for (String code : languages) {          /**
137                                  if (code.equals(getActiveLang())) {           * Initializes a new {@link Translation} with a default translation if a
138                                          put(code, defaultTranslation);           * simple text is passed. If a "oneLine" text is parsed, it is interpreted.
139                                  }           * Other translations may be added later - this is a HashMap<br/>
140                          }           *
141          }           * @param defaultTranslation
142             *
143          /**           * @see public Translation(List<String> languages, String
144           * Sometimes Translations are optional, like for keywords.           *      defaultTranslation) {
145           */           *
146          public Translation() {           */
147          }          public Translation(String defaultTranslation) {
148    
149          /**                  fromOneLine(defaultTranslation);
150           * Fills the {@link Translation} with the values coded into the String  
151           * Format of {@link String} is: "de{Baum}en{tree}"          }
152           * <p>  
153           * <ul>          /**
154           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.           * Initializes a new {@link Translation}, an uses the given String to
155           * <li>If format can't be recognized, the {@link String} is interpreted as           * initialize the {@link Translation} for all languages codes passed.
156           * the translation in the <code>{@value #DEFAULT_KEY}</code> language           *
157           *           * The translations can be changed later
158           * @author Stefan Alfons Krüger           */
159           */          public Translation(List<String> languages, String defaultTranslation) {
160          public void fromOneLine(final String oneLineCoded) {                  // put(DEFAULT_KEY, defaultTranslation);
161                  clear();                  if (languages == null) {
162                  if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {                          put(DEFAULT_KEY, defaultTranslation);
163                          put(DEFAULT_KEY, "");                  } else
164                          return;                          for (String code : languages) {
165                  }                                  if (code.equals(getActiveLang())) {
166                                            put(code, defaultTranslation);
167                  if (oneLineCoded.indexOf("}") == -1) {                                  }
168                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");                          }
169                          put(DEFAULT_KEY, oneLineCoded);          }
170                  }  
171            /**
172                  String eatUp = oneLineCoded;           * Sometimes Translations are optional, like for keywords.
173                  while (eatUp.indexOf("}") != -1) {           */
174                          String substring = eatUp.substring(0, eatUp.indexOf("}"));          public Translation() {
175            }
176                          // log.debug("substring = "+substring);  
177                          String key = substring.substring(0, substring.indexOf("{"));          /**
178                          String value = substring.substring(substring.indexOf("{") + 1,           * Fills the {@link Translation} with the values coded into the String
179                                          substring.length());           * Format of {@link String} is: "de{Baum}en{tree}"
180                          // log.debug("key="+key);           * <p>
181                          // log.debug("value="+value);           * <ul>
182                          put(key, value);           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
183                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);           * <li>If format can't be recognized, the {@link String} is interpreted as
184                  }           * the translation in the <code>{@value #DEFAULT_KEY}</code> language
185          }           *
186             * @author Stefan Alfons Krüger
187          /**           */
188           * Exports the Translations to a String of the Format: "de{Baum}en{tree}"          public void fromOneLine(final String oneLineCoded) {
189           *                  clear();
190           * @author Stefan Alfons Krüger                  if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
191           */                          put(DEFAULT_KEY, "");
192          public String toOneLine() {                          return;
193                  return I8NUtil.toOneLine(this);                  }
194          }  
195                    if (oneLineCoded.indexOf("}") == -1) {
196          /**                          // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
197           * Returns the right translation by using the {@link #activeLang} field. If                          put(DEFAULT_KEY, oneLineCoded);
198           * no translation is set, an ugly String {@link #NO_TRANSLATION} will re                  }
199           * returned. This might be changed for the final release. If the correct  
200           * language was not found, any entry in the {@link Translation}                  String eatUp = oneLineCoded;
201           * {@link HashMap} will be returned, that contains more than an empty                  while (eatUp.indexOf("}") != -1) {
202           * string.                          String substring = eatUp.substring(0, eatUp.indexOf("}"));
203           */  
204          @Override                          // log.debug("substring = "+substring);
205          public String toString() {                          String key = substring.substring(0, substring.indexOf("{"));
206                  if (get(activeLang) != null) {                          String value = substring.substring(substring.indexOf("{") + 1,
207                          return get(activeLang);                                          substring.length());
208                  }                          // log.debug("key="+key);
209                  // ****************************************************************************                          // log.debug("value="+value);
210                  // MS: The ISDSS needs the concept of the default lang!! So I took the                          put(key, value);
211                  // following in again!!                          eatUp = eatUp.substring(eatUp.indexOf("}") + 1);
212                  // ****************************************************************************                  }
213                  // else return "";          }
214                  // //****************************************************************************  
215                  // // The following is commented out.. the concept of the default lang          /**
216                  // seems to be bad....           * Exports the Translations to a String of the Format: "de{Baum}en{tree}"
217                  // //****************************************************************************           *
218                  // MS:           * @author Stefan Alfons Krüger
219                  else {           */
220                          if (get(DEFAULT_KEY) != null) {          public String toOneLine() {
221                                  // 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.");                  return I8NUtil.toOneLine(this);
222                                  return get(DEFAULT_KEY);          }
223                          }  
224            /**
225                          // log.debug("return first best <> '' ");           * Returns the right translation by using the {@link #activeLang} field. If
226                          if (size() > 0)           * no translation is set, an ugly String {@link #NO_TRANSLATION} will re
227                                  for (String s : values()) {           * returned. This might be changed for the final release. If the correct
228                                          if ((s != null) && (s.trim().length() > 0))           * language was not found, any entry in the {@link Translation}
229                                                  return s;           * {@link HashMap} will be returned, that contains more than an empty
230                                  }           * string.
231                  }           */
232                  log.warn("No translation found!");          @Override
233                  return NO_TRANSLATION;          public String toString() {
234          }                  if (get(activeLang) != null) {
235                            return get(activeLang);
236          /**                  }
237           * Copy this {@link Translation} to another {@link Translation} e.g. for                  // ****************************************************************************
238           * editing                  // MS: The ISDSS needs the concept of the default lang!! So I took the
239           *                  // following in again!!
240           * @return the destination {@link Translation}                  // ****************************************************************************
241           */                  // else return "";
242          public Translation copy(Translation backup) {                  // //****************************************************************************
243                  if (backup == null)                  // // The following is commented out.. the concept of the default lang
244                          throw new IllegalArgumentException(                  // seems to be bad....
245                                          "Target translation may not be null.");                  // //****************************************************************************
246                  for (String s : keySet()) {                  // MS:
247                          backup.put(s, get(s));                  else {
248                  }                          if (get(DEFAULT_KEY) != null) {
249                  return backup;                                  // 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.");
250          }                                  return get(DEFAULT_KEY);
251                            }
252          /**  
253           * {@link PropertyChangeListener} can be registered to be informed when the                          // log.debug("return first best <> '' ");
254           * {@link Locale} changed.                          if (size() > 0)
255           *                                  for (String s : values()) {
256           * @param propertyChangeListener                                          if ((s != null) && (s.trim().length() > 0))
257           */                                                  return s;
258          public static void addLocaleChangeListener(                                  }
259                          PropertyChangeListener propertyChangeListener) {                  }
260                  listeners.add(propertyChangeListener);  //              log.warn("No translation found!");
261          }                  return NO_TRANSLATION;
262            }
263          /**  
264           * Informs all registered {@link PropertyChangeListener}s about a change of          /**
265           * the the {@link Locale}.           * Copy this {@link Translation} to another {@link Translation} e.g. for
266           */           * editing
267          public static void fireChangeEvents() {           *
268                  PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(           * @return the destination {@link Translation}
269                                  new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,           */
270                                  null, getActiveLang());          public Translation copy(Translation backup) {
271                  for (PropertyChangeListener pcl : listeners) {                  if (backup == null)
272                          if (pcl != null)                          throw new IllegalArgumentException(
273                                  pcl.propertyChange(pce);                                          "Target translation may not be null.");
274                  }                  for (String s : keySet()) {
275          }                          backup.put(s, get(s));
276                    }
277  }                  return backup;
278            }
279    
280            /**
281             * {@link PropertyChangeListener} can be registered to be informed when the
282             * {@link Locale} changed.
283             *
284             * @param propertyChangeListener
285             */
286            public static void addLocaleChangeListener(
287                            PropertyChangeListener propertyChangeListener) {
288                    listeners.add(propertyChangeListener);
289            }
290    
291            /**
292             * Informs all registered {@link PropertyChangeListener}s about a change of
293             * the the {@link Locale}.
294             */
295            public static void fireLocaleChangeEvents() {
296                    PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
297                                    new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
298                                    null, getActiveLang());
299                    for (PropertyChangeListener pcl : listeners) {
300                            if (pcl != null)
301                                    pcl.propertyChange(pce);
302                    }
303            }
304    
305    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26