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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26