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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 237 - (show annotations)
Mon Jul 27 11:56:14 2009 UTC (15 years, 7 months ago) by alfonx
File size: 8421 byte(s)
* Removed a LOGGER.warn
1 package skrueger.i8n;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Locale;
9
10 import javax.swing.JComponent;
11 import javax.swing.JTable;
12
13 import org.apache.log4j.Logger;
14
15 /**
16 * 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 */
22
23 public class Translation extends HashMap<String, String> {
24 public static final String LOCALECHANGE_PROPERTY = "localechange";
25 public static final String NO_TRANSLATION = "NO TRANSLATION";
26 public static final String DEFAULT_KEY = "default";
27 static final Logger log = Logger.getLogger(Translation.class);
28 static String activeLang = "fr";
29
30 static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
31
32 static {
33
34 // 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 setActiveLang(locale.getLanguage());
40 }
41
42 @Override
43 /*
44 * @comment To make a copy of a translation see methods toOneLine() and
45 * fromOneLine()
46 */
47 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 * 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
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 * 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)) {
83 return;
84 }
85
86 if (!I8NUtil.isValidISOLangCode(newLang)) {
87 throw new IllegalArgumentException("'" + newLang
88 + "' is not a valid ISO language code.");
89 }
90
91 Locale newLocale = new Locale(newLang);
92 if (setDefaultLocale)
93 Locale.setDefault(newLocale);
94
95 /**
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);
100
101 Translation.activeLang = newLang;
102
103 fireChangeEvents();
104
105 log.info("skrueger.i8n.Translation switched ActiveLang to " + newLang);
106 }
107
108 /**
109 * Initializes a new {@link Translation} with a default translation if a
110 * 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
114 *
115 * @see public Translation(List<String> languages, String
116 * defaultTranslation) {
117 *
118 */
119 public Translation(String defaultTranslation) {
120
121 fromOneLine(defaultTranslation);
122
123 }
124
125 /**
126 * Initializes a new {@link Translation}, an uses the given String to
127 * initialize the {@link Translation} for all languages codes passed.
128 *
129 * The translations can be changed later
130 */
131 public Translation(List<String> languages, String defaultTranslation) {
132 // put(DEFAULT_KEY, defaultTranslation);
133 if (languages == null) {
134 put(DEFAULT_KEY, defaultTranslation);
135 } else
136 for (String code : languages) {
137 if (code.equals(getActiveLang())) {
138 put(code, defaultTranslation);
139 }
140 }
141 }
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 * <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 * @author Stefan Alfons Krüger
159 */
160 public void fromOneLine(final String oneLineCoded) {
161 clear();
162 if ((oneLineCoded == null) || (oneLineCoded.equals(""))) {
163 put(DEFAULT_KEY, "");
164 return;
165 }
166
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;
173 while (eatUp.indexOf("}") != -1) {
174 String substring = eatUp.substring(0, eatUp.indexOf("}"));
175
176 // 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 }
185 }
186
187 /**
188 * Exports the Translations to a String of the Format: "de{Baum}en{tree}"
189 *
190 * @author Stefan Alfons Krüger
191 */
192 public String toOneLine() {
193 return I8NUtil.toOneLine(this);
194 }
195
196 /**
197 * 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 */
204 @Override
205 public String toString() {
206 if (get(activeLang) != null) {
207 return get(activeLang);
208 }
209 // ****************************************************************************
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 else {
220 if (get(DEFAULT_KEY) != null) {
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.");
222 return get(DEFAULT_KEY);
223 }
224
225 // log.debug("return first best <> '' ");
226 if (size() > 0)
227 for (String s : values()) {
228 if ((s != null) && (s.trim().length() > 0))
229 return s;
230 }
231 }
232 // log.warn("No translation found!");
233 return NO_TRANSLATION;
234 }
235
236 /**
237 * Copy this {@link Translation} to another {@link Translation} e.g. for
238 * editing
239 *
240 * @return the destination {@link Translation}
241 */
242 public Translation copy(Translation backup) {
243 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 }
249 return backup;
250 }
251
252 /**
253 * {@link PropertyChangeListener} can be registered to be informed when the
254 * {@link Locale} changed.
255 *
256 * @param propertyChangeListener
257 */
258 public static void addLocaleChangeListener(
259 PropertyChangeListener propertyChangeListener) {
260 listeners.add(propertyChangeListener);
261 }
262
263 /**
264 * Informs all registered {@link PropertyChangeListener}s about a change of
265 * the the {@link Locale}.
266 */
267 public static void fireChangeEvents() {
268 PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(
269 new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
270 null, getActiveLang());
271 for (PropertyChangeListener pcl : listeners) {
272 if (pcl != null)
273 pcl.propertyChange(pce);
274 }
275 }
276
277 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26