1 | /* |
---|
2 | * Creado el 30/06/2004 por Rodolfo Gonzalez Gonzalez <rodolfo@equinoxe.g-networks.net> |
---|
3 | * |
---|
4 | * This class is a wrapper for easier i18n of Jeti. It uses the standard i18n |
---|
5 | * mechanisms given by Java. |
---|
6 | * |
---|
7 | */ |
---|
8 | package nu.fw.jeti.util; |
---|
9 | |
---|
10 | import java.io.*; |
---|
11 | import java.io.BufferedReader; |
---|
12 | import java.io.File; |
---|
13 | import java.io.IOException; |
---|
14 | import java.io.InputStreamReader; |
---|
15 | import java.net.MalformedURLException; |
---|
16 | import java.net.URL; |
---|
17 | import java.util.*; |
---|
18 | |
---|
19 | import javax.swing.AbstractButton; |
---|
20 | import javax.swing.JComponent; |
---|
21 | import javax.swing.JLabel; |
---|
22 | import javax.swing.UIManager; |
---|
23 | |
---|
24 | import nu.fw.jeti.backend.Start; |
---|
25 | import nu.fw.jeti.backend.URLClassloader; |
---|
26 | |
---|
27 | /** |
---|
28 | * @author Rodolfo Gonzalez <rodolfo@equinoxe.g-networks.net> |
---|
29 | */ |
---|
30 | public class I18N |
---|
31 | { |
---|
32 | private static ResourceBundle jetiBundle; |
---|
33 | private static Map pluginsBundle; |
---|
34 | private Language[] languages; |
---|
35 | private Map countries; |
---|
36 | |
---|
37 | public I18N() |
---|
38 | { |
---|
39 | countries = new HashMap(); |
---|
40 | if ( Start.applet || Start.webstart ) |
---|
41 | { |
---|
42 | try |
---|
43 | { |
---|
44 | BufferedReader data = null; |
---|
45 | InputStream is = getClass().getResourceAsStream("/languages/list.txt"); |
---|
46 | if(is==null)return;//no languages |
---|
47 | data =new BufferedReader(new InputStreamReader(is)); |
---|
48 | //data =new BufferedReader(new InputStreamReader(new URL(Start.programURL + "languages/list.txt").openStream())); |
---|
49 | List locales = new LinkedList(); |
---|
50 | while (true) |
---|
51 | { |
---|
52 | String file = data.readLine(); |
---|
53 | if (file ==null) break;//end of stream |
---|
54 | locales.add(new Locale(file)); |
---|
55 | } |
---|
56 | data.close(); |
---|
57 | extractLanguages(locales); |
---|
58 | extractCountries(); |
---|
59 | } |
---|
60 | catch (IOException ex){ex.printStackTrace();} |
---|
61 | } |
---|
62 | else |
---|
63 | { |
---|
64 | extractLanguages(searchTranslations()); |
---|
65 | extractCountries(); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | private List searchTranslations() |
---|
70 | { |
---|
71 | String urlString = Start.path + "languages" + File.separator; |
---|
72 | File path = new File(urlString); |
---|
73 | File file[] = path.listFiles(); |
---|
74 | List locales = new LinkedList(); |
---|
75 | //locales.add(new Locale("en"));//english is available, but java always selects default locale before base locale |
---|
76 | if (file == null) return locales; |
---|
77 | for (int i = 0; i < file.length; i++) |
---|
78 | { |
---|
79 | Locale locale = getLocale(file[i].getName()); |
---|
80 | if (locale != null) locales.add(locale); |
---|
81 | } |
---|
82 | return locales; |
---|
83 | } |
---|
84 | |
---|
85 | private Locale getLocale(String filename) |
---|
86 | { |
---|
87 | if (!filename.startsWith("jeti") || !filename.endsWith(".properties")) return null; |
---|
88 | int nlen = "jeti".length(); |
---|
89 | String language, country = ""; |
---|
90 | switch (filename.length() - nlen - 11) |
---|
91 | { |
---|
92 | case 6: |
---|
93 | // E.g. name+"_en_US"+".properties" |
---|
94 | if (filename.charAt(3 + nlen) != '_') return null; |
---|
95 | country = filename.substring(nlen + 4, nlen + 6); |
---|
96 | // no break; here! |
---|
97 | case 3: |
---|
98 | if (filename.charAt(nlen) != '_') return null; |
---|
99 | language = filename.substring(nlen + 1, nlen + 3); |
---|
100 | Locale locale = new Locale(language, country); |
---|
101 | return locale; |
---|
102 | } |
---|
103 | return null; |
---|
104 | } |
---|
105 | |
---|
106 | private void extractLanguages(List availableLocales) |
---|
107 | { |
---|
108 | Set lang = new HashSet(); |
---|
109 | for (Iterator i = availableLocales.iterator(); i.hasNext();) |
---|
110 | { |
---|
111 | Locale l = (Locale) i.next(); |
---|
112 | String language = l.getLanguage(); |
---|
113 | List langLocs = (List) countries.get(language); |
---|
114 | if (langLocs == null) |
---|
115 | { |
---|
116 | langLocs = new ArrayList(); |
---|
117 | countries.put(language, langLocs); |
---|
118 | } |
---|
119 | langLocs.add(new Country(l)); |
---|
120 | lang.add(new Language(l)); |
---|
121 | } |
---|
122 | //add default locale: |
---|
123 | Locale l = Locale.getDefault(); |
---|
124 | String language = l.getLanguage(); |
---|
125 | List langLocs = (List) countries.get(language); |
---|
126 | if (langLocs == null) |
---|
127 | { |
---|
128 | langLocs = new ArrayList(); |
---|
129 | countries.put(language, langLocs); |
---|
130 | } |
---|
131 | lang.add(new Language(l)); |
---|
132 | languages = (Language[]) lang.toArray(new Language[0]); |
---|
133 | } |
---|
134 | |
---|
135 | private void extractCountries() |
---|
136 | { |
---|
137 | Locale[] allLocales = Locale.getAvailableLocales(); |
---|
138 | for (int i = 0; i < allLocales.length; i++) |
---|
139 | { |
---|
140 | String language = allLocales[i].getLanguage(); |
---|
141 | for (int j = 0; j < languages.length; j++) |
---|
142 | { |
---|
143 | if (language.equals(languages[j].getLanguageCode())) |
---|
144 | { |
---|
145 | List langLocs = (List) countries.get(language); |
---|
146 | Country country = new Country(allLocales[i]); |
---|
147 | if (!langLocs.contains(country)) langLocs.add(country); |
---|
148 | //System.out.println(allLocales[i].getDisplayCountry()); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * @return The available languages |
---|
156 | */ |
---|
157 | public Language[] getLanguages() |
---|
158 | { |
---|
159 | return languages; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * @return The countries that are possible with the available languages |
---|
164 | */ |
---|
165 | public Map getCountries() |
---|
166 | { |
---|
167 | return countries; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | public static String getText(String type, String messageID) { |
---|
172 | ResourceBundle bundle = (ResourceBundle)pluginsBundle.get(type); |
---|
173 | if(bundle == null) bundle = jetiBundle; |
---|
174 | return getText(bundle,messageID); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Gives back a translation of the input key without '&' (which is used for mnemonics) |
---|
179 | * |
---|
180 | * @param messageID is the English phrase used as key, following the GNU gettext style |
---|
181 | * @param bundle is the name of the base resource bundle file |
---|
182 | * @return result (or messageID in case of error) |
---|
183 | */ |
---|
184 | public static String gettext(String messageID) |
---|
185 | { |
---|
186 | return getText(jetiBundle, messageID); |
---|
187 | } |
---|
188 | |
---|
189 | private static String getText(ResourceBundle bundle, String messageID) |
---|
190 | { |
---|
191 | if (messageID==null)return null; |
---|
192 | String translation = getTextWithAmp(bundle,messageID); |
---|
193 | //remove & |
---|
194 | int i = translation.indexOf("&"); |
---|
195 | if (i > -1) |
---|
196 | { |
---|
197 | translation = translation.substring(0, i) + translation.substring(i + 1, translation.length()); |
---|
198 | //i = translation.indexOf("&",i); |
---|
199 | } |
---|
200 | return translation; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Gives back a translation of the input key |
---|
205 | * |
---|
206 | * @param messageID is the English phrase used as key, following the GNU gettext style |
---|
207 | * @param bundle is the name of the base resource bundle file |
---|
208 | * @return result (or messageID in case of error) |
---|
209 | */ |
---|
210 | public static String getTextWithAmp(String messageID) |
---|
211 | {//TODO change back to private when string prep is implemented |
---|
212 | return getTextWithAmp(jetiBundle, messageID); |
---|
213 | } |
---|
214 | |
---|
215 | private static String getTextWithAmp(ResourceBundle bundle, String messageID) |
---|
216 | { |
---|
217 | if (messageID==null)return null; |
---|
218 | if(bundle!=null) |
---|
219 | { |
---|
220 | try |
---|
221 | { |
---|
222 | return (String) bundle.getObject(messageID); |
---|
223 | } catch (MissingResourceException e) |
---|
224 | { |
---|
225 | System.out.println(messageID + " is not translated"); |
---|
226 | } |
---|
227 | } |
---|
228 | int dotPos = messageID.lastIndexOf('.'); |
---|
229 | if (dotPos>0) messageID = messageID.substring(dotPos+1); |
---|
230 | //return messageID.replaceAll("_"," "); |
---|
231 | return messageID.replace('_',' '); |
---|
232 | } |
---|
233 | |
---|
234 | public static void setTextAndMnemonic(String messageID,JLabel label) { |
---|
235 | setTextAndMnemonic(messageID, label, false); |
---|
236 | } |
---|
237 | |
---|
238 | public static void setTextAndMnemonic(String messageID,JLabel label, boolean addDots) |
---|
239 | { |
---|
240 | String mnemonicText = getTextWithAmp(messageID); |
---|
241 | String translation=mnemonicText; |
---|
242 | int pos = mnemonicText.indexOf("&"); |
---|
243 | if (pos > -1) |
---|
244 | { |
---|
245 | translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); |
---|
246 | } |
---|
247 | if (addDots) { |
---|
248 | translation += "..."; |
---|
249 | } |
---|
250 | label.setText(translation); |
---|
251 | pos = getMnemonicPosition(mnemonicText); |
---|
252 | if(pos>-1) label.setDisplayedMnemonic(mnemonicText.charAt(pos)); |
---|
253 | if(pos>0) label.setDisplayedMnemonicIndex(pos-1); |
---|
254 | } |
---|
255 | |
---|
256 | public static void setMnemonic(String messageID, JLabel label) |
---|
257 | { |
---|
258 | String translation = getTextWithAmp(messageID); |
---|
259 | int pos = getMnemonicPosition(translation); |
---|
260 | if(pos>-1)label.setDisplayedMnemonic(translation.charAt(pos)); |
---|
261 | if(pos>1)label.setDisplayedMnemonicIndex(pos-1); |
---|
262 | } |
---|
263 | |
---|
264 | public static void setMnemonic(String messageID, AbstractButton button) |
---|
265 | { |
---|
266 | String translation = getTextWithAmp(messageID); |
---|
267 | int pos = getMnemonicPosition(translation); |
---|
268 | if(pos>-1)button.setMnemonic(translation.charAt(pos)); |
---|
269 | if(pos>1)button.setDisplayedMnemonicIndex(pos-1); |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * @param messageID is the English phrase used as key, following the |
---|
274 | * GNU gettext style |
---|
275 | * @param bundle is the name of the base resource bundle file |
---|
276 | * @param button The button to set the mnemonic on |
---|
277 | */ |
---|
278 | public static void setTextAndMnemonic(String messageID,AbstractButton button) |
---|
279 | { |
---|
280 | setTextAndMnemonic(messageID, button, false); |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * @param messageID is the English phrase used as key, following the |
---|
285 | * GNU gettext style |
---|
286 | * @param bundle is the name of the base resource bundle file |
---|
287 | * @param button The button to set the mnemonic on |
---|
288 | * @param addDots If true add '...' to the translated text |
---|
289 | */ |
---|
290 | public static void setTextAndMnemonic(String messageID,AbstractButton button, |
---|
291 | boolean addDots) |
---|
292 | { |
---|
293 | String mnemonicText = getTextWithAmp(messageID); |
---|
294 | |
---|
295 | String translation=mnemonicText; |
---|
296 | int pos = mnemonicText.indexOf("&"); |
---|
297 | if (pos > -1) |
---|
298 | { |
---|
299 | translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); |
---|
300 | } |
---|
301 | if (addDots) { |
---|
302 | translation += "..."; |
---|
303 | } |
---|
304 | button.setText(translation); |
---|
305 | pos = getMnemonicPosition(mnemonicText); |
---|
306 | if(pos>-1)button.setMnemonic(mnemonicText.charAt(pos)); |
---|
307 | if(pos>0) button.setDisplayedMnemonicIndex(pos-1); |
---|
308 | |
---|
309 | } |
---|
310 | |
---|
311 | private static int getMnemonicPosition(String translation) |
---|
312 | { |
---|
313 | int index = translation.indexOf("&"); |
---|
314 | if (index >= 0) return index+1; |
---|
315 | return -1; |
---|
316 | } |
---|
317 | |
---|
318 | static public void init() |
---|
319 | { |
---|
320 | ClassLoader classLoader=null; |
---|
321 | if (!Start.applet && !Start.webstart) { |
---|
322 | try { |
---|
323 | URL url = new URL(Start.programURL, "languages/"); |
---|
324 | classLoader = new URLClassloader( |
---|
325 | new URL[] { url}, I18N.class.getClassLoader()); |
---|
326 | } catch (MalformedURLException e) { |
---|
327 | e.printStackTrace(); |
---|
328 | } |
---|
329 | } else classLoader = I18N.class.getClassLoader(); |
---|
330 | // see if the user has some preferences |
---|
331 | String myLanguage = Preferences.getString("jeti", "language", ""); |
---|
332 | String myCountry = Preferences.getString("jeti", "country", ""); |
---|
333 | Locale myLocale=null; |
---|
334 | if (myLanguage != "") { |
---|
335 | myLocale = new Locale(myLanguage, myCountry); |
---|
336 | } else { |
---|
337 | myLocale = Locale.getDefault(); |
---|
338 | } |
---|
339 | jetiBundle = loadLanguage("jeti",classLoader,myLocale); |
---|
340 | if(jetiBundle==null || jetiBundle.getLocale().getLanguage().equals("")) |
---|
341 | { |
---|
342 | // Make it fall back to English, this way we always get at least |
---|
343 | // the english texts (which contains the menu mnemonic markings) |
---|
344 | myLocale= new Locale(Locale.ENGLISH.getLanguage(),myCountry); |
---|
345 | System.out.println("lang not found, default: " + myLocale); |
---|
346 | jetiBundle = loadLanguage("jeti",classLoader,myLocale); |
---|
347 | } |
---|
348 | pluginsBundle = new HashMap(); |
---|
349 | pluginsBundle.put("drawing",loadLanguage("drawing",classLoader,myLocale)); |
---|
350 | JComponent.setDefaultLocale(myLocale); |
---|
351 | |
---|
352 | //loadLanguage(classLoader, myLocale); |
---|
353 | UIManager.put("OptionPane.okButtonText", gettext("OK")); |
---|
354 | UIManager.put("OptionPane.cancelButtonText", gettext("Cancel")); |
---|
355 | |
---|
356 | } |
---|
357 | |
---|
358 | private static ResourceBundle loadLanguage(String type, ClassLoader classLoader, Locale myLocale) |
---|
359 | { |
---|
360 | try { |
---|
361 | return ResourceBundle.getBundle("languages/" +type, myLocale); |
---|
362 | } catch (MissingResourceException e) { |
---|
363 | try { |
---|
364 | return ResourceBundle.getBundle(type, |
---|
365 | myLocale, classLoader); |
---|
366 | } catch (MissingResourceException e2) { |
---|
367 | System.out.println(e2.getMessage()); |
---|
368 | return null; |
---|
369 | } |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | public static class Country |
---|
374 | { |
---|
375 | private String country; |
---|
376 | private String countryCode; |
---|
377 | |
---|
378 | public Country(Locale locale) |
---|
379 | { |
---|
380 | country = locale.getDisplayCountry(); |
---|
381 | if (country.equals("")) country = gettext("main.options.standard.Other"); |
---|
382 | countryCode = locale.getCountry(); |
---|
383 | } |
---|
384 | |
---|
385 | public String getCountryCode() |
---|
386 | { |
---|
387 | return countryCode; |
---|
388 | } |
---|
389 | |
---|
390 | public String toString() |
---|
391 | { |
---|
392 | return country; |
---|
393 | } |
---|
394 | |
---|
395 | } |
---|
396 | |
---|
397 | public static class Language |
---|
398 | { |
---|
399 | private String language; |
---|
400 | private String languageCode; |
---|
401 | |
---|
402 | public Language(Locale locale) |
---|
403 | { |
---|
404 | language = locale.getDisplayLanguage(); |
---|
405 | languageCode = locale.getLanguage(); |
---|
406 | } |
---|
407 | |
---|
408 | public String getLanguageCode() |
---|
409 | { |
---|
410 | return languageCode; |
---|
411 | } |
---|
412 | |
---|
413 | public String toString() |
---|
414 | { |
---|
415 | return language; |
---|
416 | } |
---|
417 | |
---|
418 | public boolean equals(Object obj) |
---|
419 | { |
---|
420 | return language.equals(((Language)obj).language); |
---|
421 | } |
---|
422 | |
---|
423 | public int hashCode() |
---|
424 | { |
---|
425 | return language.hashCode(); |
---|
426 | } |
---|
427 | |
---|
428 | |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | /* |
---|
434 | * Overrides for emacs |
---|
435 | * Local variables: |
---|
436 | * tab-width: 4 |
---|
437 | * End: |
---|
438 | */ |
---|