/[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 20 - (show annotations)
Wed Mar 4 17:31:50 2009 UTC (15 years, 11 months ago) by alfonx
File size: 6682 byte(s)
skrueger.i8n.Translation is setting the Locale, we added:

		/**
		 * Setting default locale for Swing JComponents to work around bug 
		 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
		 */
		JComponent.setDefaultLocale(newLocale);
1 package skrueger.i8n;
2 import java.util.HashMap;
3 import java.util.List;
4 import java.util.Locale;
5
6 import javax.swing.JColorChooser;
7 import javax.swing.JComponent;
8 import javax.swing.JFileChooser;
9 import javax.swing.JOptionPane;
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 NO_TRANSLATION = "NO TRANSLATION";
22 public static final String DEFAULT_KEY = "default";
23 static final Logger log = Logger.getLogger( Translation.class );
24 static String activeLang = "fr";
25
26 static {
27
28 //TODO default aus Locale auslesen und mit möglichen vergleichen... mmm.. vor laden von atlasml immer DEFAULT_KEY, also hier nicht
29
30 // Get default locale
31 Locale locale = Locale.getDefault();
32 setActiveLang(locale.getLanguage());
33 }
34
35 @Override
36 public Translation clone() {
37 return (Translation) super.clone();
38 }
39
40 /**
41 * Get the two-letter language sting that is active
42 */
43 public static String getActiveLang() {
44 return activeLang;
45 }
46
47 /**
48 * Set up the {@link Translation}-system to use language.
49 * @param activeLang
50 */
51 public static void setActiveLang(String activeLang) {
52 if (!I8NUtil.isValidISOLangCode(activeLang)) {
53 throw new IllegalArgumentException("'"+activeLang+"' is not a valid ISO language code.");
54 }
55
56 Locale newLocale = new Locale(activeLang);
57 Locale.setDefault(newLocale);
58
59 /**
60 * Setting default locale for Swing JComponents to work around bug
61 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4884480
62 */
63 JComponent.setDefaultLocale(newLocale);
64
65 Translation.activeLang = activeLang;
66 log.info("Translation-system switched to "+activeLang);
67 }
68
69 /**
70 * Initilises a new {@link Translation} with a default translation.
71 * Other translations may be added later.
72 *
73 * @param defaultTranslation
74 *
75 * @deprecated SK: The concept of the default translation doesn't seem so nice to me anymore..
76 * I would prefer the default translation to be set for all valid languages.
77 *
78 * @see public Translation(List<String> languages, String defaultTranslation) {
79 *
80 */
81 public Translation(String defaultTranslation) {
82 put(DEFAULT_KEY, defaultTranslation);
83 }
84
85 /**
86 * Initilises a new {@link Translation}, an uses the given String to
87 * initialise the {@link Translation} for all languages codes passed.
88 *
89 * The translations can be cahnged later
90 */
91 public Translation(List<String> languages, String defaultTranslation) {
92 // put(DEFAULT_KEY, defaultTranslation);
93 if (languages == null) {
94 put(DEFAULT_KEY, defaultTranslation);
95 }
96 else for (String code : languages){
97 put(code, defaultTranslation);
98 }
99 }
100
101 /**
102 * Sometimes Translations are optional, like for keywords.
103 */
104 public Translation() {
105 super();
106 }
107
108 /**
109 * Fills the {@link Translation} with the values coded into the String
110 * Format of {@link String} is: "de{Baum}en{tree}"
111 * <p>
112 * <ul>
113 * <li> If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
114 * <li> If format can't be recognized, the {@link String} is interpreted as the translation in the <code>{@value #DEFAULT_KEY}</code> language
115 *
116 * @author Stefan Alfons Krüger
117 */
118 public void fromOneLine( final String oneLineCoded) {
119 clear();
120 if ( (oneLineCoded == null) || (oneLineCoded.equals("")) ) {
121 put(DEFAULT_KEY,NO_TRANSLATION);
122 return;
123 }
124
125 if (oneLineCoded.indexOf("}") == -1) {
126 // log.warn("The String '"+oneLineCoded+"' is not in oneLine coded => put(DEFAULT_KEY,oneLineCoded);");
127 put(DEFAULT_KEY,oneLineCoded);
128 }
129
130 String eatUp = oneLineCoded;
131 while ( eatUp.indexOf("}") != -1) {
132 String substring = eatUp.substring(0, eatUp.indexOf("}"));
133
134 // log.debug("substring = "+substring);
135 String key = substring.substring(0, substring.indexOf("{") );
136 String value = substring.substring(substring.indexOf("{")+1, substring.length() );
137 // log.debug("key="+key);
138 // log.debug("value="+value);
139 put(key,value);
140 eatUp = eatUp.substring(eatUp.indexOf("}")+1);
141 }
142 }
143
144 /**
145 * Exports the Translations to a String of the Format: "de{Baum}en{tree}"
146 * @author Stefan Alfons Krüger
147 */
148 public String toOneLine(){
149 StringBuffer oneLine = new StringBuffer();
150 for (String key: keySet()) {
151 oneLine.append(key+"{"+ get(key) + "}");
152 }
153 return oneLine.toString();
154 }
155
156 /**
157 * Returns the right translation by using the {@link #activeLang} field.
158 * If no translation is set, an ugly String {@link #NO_TRANSLATION} will re returned. This might be changed for the final release.
159 * If the correct language was not found, any entry in the {@link Translation} {@link HashMap} will be returned, that contains
160 * more than an empty string.
161 */
162 @Override
163 public String toString(){
164 if ( get(activeLang) != null ) {
165 return get(activeLang);
166 }
167 //****************************************************************************
168 // MS: The ISDSS needs the concept of the default lang!! So I took the
169 // following in again!!
170 //****************************************************************************
171 // else return "";
172 // //****************************************************************************
173 // // The following is commented out.. the concept of the default lang seems to be bad....
174 // //****************************************************************************
175 // MS:
176 else {
177 if ( get(DEFAULT_KEY) != null ) {
178 // 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.");
179 return get(DEFAULT_KEY);
180 }
181
182 // log.debug("return first best <> '' ");
183 if (size() > 0)
184 for ( String s : values() ) {
185 if ( (s != null) && (s.trim().length()>0) )
186 return s;
187 }
188 }
189 log.warn("No translation found!");
190 return NO_TRANSLATION;
191 }
192
193 /**
194 * Copy this {@link Translation} to another {@link Translation}
195 * e.g. for editing
196 *
197 * @return the destination {@link Translation}
198 */
199 public Translation copy(Translation backup) {
200 if (backup == null) throw new IllegalArgumentException("Target translation may not be null.");
201 for (String s : keySet() ) {
202 backup.put(s, get(s) );
203 }
204 return backup;
205 }
206
207 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26