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