/[schmitzm]/trunk/src/skrueger/i8n/Translation.java
ViewVC logotype

Annotation of /trunk/src/skrueger/i8n/Translation.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (hide annotations)
Fri Jul 3 14:40:10 2009 UTC (15 years, 8 months ago) by alfonx
File size: 8419 byte(s)


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26