/[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 38 - (hide annotations)
Sun Apr 5 15:06:56 2009 UTC (15 years, 10 months ago) by alfonx
File size: 8012 byte(s)
* Further improved the TranslationAskJDialog
* Removed deprecated stuff from TranslationEditJPanel


1 mojays 2 package skrueger.i8n;
2 alfonx 34 import java.beans.PropertyChangeEvent;
3     import java.beans.PropertyChangeListener;
4     import java.util.ArrayList;
5 mojays 2 import java.util.HashMap;
6     import java.util.List;
7     import java.util.Locale;
8    
9 alfonx 20 import javax.swing.JComponent;
10    
11 mojays 2 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 alfonx 34 public class Translation extends HashMap<String, String>{
21     public static final String LOCALECHANGE_PROPERTY = "localechange";
22 mojays 2 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 alfonx 34 static protected List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
28    
29 mojays 2 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 alfonx 34 /**
40     * @comment To make a copy of a translation see methods toOneLine() and fromOneLine()
41     */
42 mojays 2 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 alfonx 37 * 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 mojays 2 */
58 alfonx 37 public static void setActiveLang(String newLang) {
59     if (getActiveLang().equals(newLang)) {
60     return;
61 mojays 2 }
62 alfonx 37
63     if (!I8NUtil.isValidISOLangCode(newLang)) {
64     throw new IllegalArgumentException("'"+newLang+"' is not a valid ISO language code.");
65     }
66 mojays 2
67 alfonx 37 Locale newLocale = new Locale(newLang);
68 alfonx 20 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 alfonx 37 Translation.activeLang = newLang;
77 alfonx 34
78     fireChangeEvents();
79    
80 alfonx 37 log.info("skrueger.i8n.Translation switched ActiveLang to "+newLang);
81 mojays 2 }
82    
83     /**
84 alfonx 34 * Initializes a new {@link Translation} with a default translation.
85 mojays 2 * 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 alfonx 34 * Initializes a new {@link Translation}, an uses the given String to
101     * initialize the {@link Translation} for all languages codes passed.
102 mojays 2 *
103 alfonx 34 * The translations can be changed later
104 mojays 2 */
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 alfonx 38 if (code.equals(getActiveLang())) {
112     put(code, defaultTranslation);
113     }
114 mojays 2 }
115     }
116    
117     /**
118     * Sometimes Translations are optional, like for keywords.
119     */
120     public Translation() {
121     super();
122     }
123    
124     /**
125     * Fills the {@link Translation} with the values coded into the String
126     * Format of {@link String} is: "de{Baum}en{tree}"
127     * <p>
128     * <ul>
129     * <li> If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
130     * <li> If format can't be recognized, the {@link String} is interpreted as the translation in the <code>{@value #DEFAULT_KEY}</code> language
131     *
132     * @author Stefan Alfons Krüger
133     */
134     public void fromOneLine( final String oneLineCoded) {
135     clear();
136     if ( (oneLineCoded == null) || (oneLineCoded.equals("")) ) {
137     put(DEFAULT_KEY,NO_TRANSLATION);
138     return;
139     }
140    
141     if (oneLineCoded.indexOf("}") == -1) {
142     // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
143     put(DEFAULT_KEY,oneLineCoded);
144     }
145    
146     String eatUp = oneLineCoded;
147     while ( eatUp.indexOf("}") != -1) {
148     String substring = eatUp.substring(0, eatUp.indexOf("}"));
149    
150     // log.debug("substring = "+substring);
151     String key = substring.substring(0, substring.indexOf("{") );
152     String value = substring.substring(substring.indexOf("{")+1, substring.length() );
153     // log.debug("key="+key);
154     // log.debug("value="+value);
155     put(key,value);
156     eatUp = eatUp.substring(eatUp.indexOf("}")+1);
157     }
158     }
159    
160     /**
161     * Exports the Translations to a String of the Format: "de{Baum}en{tree}"
162     * @author Stefan Alfons Krüger
163     */
164     public String toOneLine(){
165     StringBuffer oneLine = new StringBuffer();
166     for (String key: keySet()) {
167     oneLine.append(key+"{"+ get(key) + "}");
168     }
169     return oneLine.toString();
170     }
171    
172     /**
173     * Returns the right translation by using the {@link #activeLang} field.
174     * If no translation is set, an ugly String {@link #NO_TRANSLATION} will re returned. This might be changed for the final release.
175     * If the correct language was not found, any entry in the {@link Translation} {@link HashMap} will be returned, that contains
176     * more than an empty string.
177     */
178     @Override
179     public String toString(){
180     if ( get(activeLang) != null ) {
181     return get(activeLang);
182     }
183     //****************************************************************************
184     // MS: The ISDSS needs the concept of the default lang!! So I took the
185     // following in again!!
186     //****************************************************************************
187     // else return "";
188     // //****************************************************************************
189     // // The following is commented out.. the concept of the default lang seems to be bad....
190     // //****************************************************************************
191     // MS:
192     else {
193     if ( get(DEFAULT_KEY) != null ) {
194     // 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.");
195     return get(DEFAULT_KEY);
196     }
197    
198     // log.debug("return first best <> '' ");
199     if (size() > 0)
200     for ( String s : values() ) {
201     if ( (s != null) && (s.trim().length()>0) )
202     return s;
203     }
204     }
205     log.warn("No translation found!");
206     return NO_TRANSLATION;
207     }
208    
209     /**
210     * Copy this {@link Translation} to another {@link Translation}
211     * e.g. for editing
212     *
213     * @return the destination {@link Translation}
214     */
215     public Translation copy(Translation backup) {
216     if (backup == null) throw new IllegalArgumentException("Target translation may not be null.");
217     for (String s : keySet() ) {
218     backup.put(s, get(s) );
219     }
220     return backup;
221     }
222 alfonx 34
223    
224     /**
225     * {@link PropertyChangeListener} can be registered to be informed when the
226     * {@link Locale} changed.
227     *
228     * @param propertyChangeListener
229     */
230     public static void addLocaleChangeListener(PropertyChangeListener propertyChangeListener) {
231     listeners.add(propertyChangeListener);
232     }
233 mojays 2
234 alfonx 34 /**
235     * Informs all registered {@link PropertyChangeListener}s about a change of the
236     * the {@link Locale}.
237     */
238     public static void fireChangeEvents() {
239     PropertyChangeEvent pce = new PropertyChangeEvent(new Translation(new ArrayList<String>(), "fakeSource"), LOCALECHANGE_PROPERTY,
240     null, getActiveLang());
241     for (PropertyChangeListener pcl : listeners) {
242     if (pcl != null)
243     pcl.propertyChange(pce);
244     }
245     }
246    
247    
248 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26