KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > util > I18n


1   /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * I18n.java
28  *
29  * Created on 13 March 2004, 21:43
30  *
31  */

32
33 package it.businesslogic.ireport.util;
34
35 import it.businesslogic.ireport.gui.MainFrame;
36 import java.util.*;
37 import java.util.jar.*;
38 import java.net.*;
39 import java.io.*;
40
41 /**
42  *
43  * @author ertano
44  */

45 public class I18n
46 {
47
48     public static final String JavaDoc localPackageName = "it/businesslogic/ireport/locale/";
49     public static final String JavaDoc baseName = "Ireport";
50     private static java.util.ResourceBundle JavaDoc oLanguage = null;
51
52     public static java.util.Vector JavaDoc languageChangedListeners = null;
53
54     static
55     {
56
57         languageChangedListeners = new Vector();
58     }
59
60     public static void addOnLanguageChangedListener(LanguageChangedListener listener)
61     {
62         languageChangedListeners.add( listener );
63     }
64
65     /**
66      * Get the list of supported translations.
67      *
68      * Load the list of property files in the it/businesslogic/ireport/locale/
69      * package.
70      *
71      */

72
73     /*
74     public static java.util.List getListOfAvailLanguages(){
75         java.util.List supportedLocales = new java.util.ArrayList();
76         try {
77             Set names = Misc.getResoucesInPackage( localPackageName );
78             Iterator it = names.iterator();
79             while( it.hasNext() ) {
80                 String n = (String)it.next();
81
82                 // From
83                 // 'it/businesslogic/ireport/locale/Ireport_en.properties'
84                 // or
85                 // 'it/businesslogic/ireport/locale/Ireport_en_UK.properties'
86                 // To
87                 // 'en' OR 'en_UK'
88
89                 String lang = n.substring( n.lastIndexOf('/')+1 );
90                 lang = lang.substring(0, lang.indexOf(".properties") );
91                 if(lang.indexOf("_")>0) // otherwise index exception
92                     lang = lang.substring( baseName.length()+1 ); // +1 to include the underscore
93
94                 Locale model;
95                 int underscorePos = lang.indexOf('_');
96                 if( underscorePos==-1 ) {
97                     String language = lang;
98                     model = new Locale( language );
99                 } else {
100                     String language = lang.substring( 0, lang.indexOf('_') );
101                     String country = lang.substring( lang.indexOf('_')+1 );
102                     model = new Locale( language, country );
103                 }
104                 supportedLocales.add( model );
105             }
106         } catch(Exception e) {
107             e.printStackTrace();
108         }
109
110         // Sort the list. Probably should use the current locale when getting the
111         // DisplayLanguage so the sort order is correct for the user.
112         Collections.sort( supportedLocales, new Comparator() {
113            public int compare(Object lhs, Object rhs) {
114                Locale ll = (Locale)lhs;
115                Locale rl = (Locale)rhs;
116                return ll.getDisplayLanguage().compareTo( rl.getDisplayLanguage() );
117            }
118         });
119
120         return supportedLocales;
121     }
122     */

123
124
125     // => Modified by RL: June 3, 2005: Introducing Variant
126
public static java.util.List JavaDoc getListOfAvailLanguages()
127     {
128         java.util.List JavaDoc supportedLocales = new java.util.ArrayList JavaDoc();
129         try
130         {
131             Set names = Misc.getResoucesInPackage( localPackageName );
132             Iterator it = names.iterator();
133             while( it.hasNext() )
134             {
135                 String JavaDoc n = (String JavaDoc)it.next();
136
137                 // From
138
// 'it/businesslogic/ireport/locale/Ireport_en.properties'
139
// or
140
// 'it/businesslogic/ireport/locale/Ireport_en_UK.properties'
141
// To
142
// 'en' OR 'en_UK_' OR even en_UK_Brighton dialect
143

144                 String JavaDoc lang = n.substring( n.lastIndexOf('/')+1 );
145
146                 // only except resources with extension '.properties'
147
if ( lang.indexOf(".properties") < 0 )
148                 {
149                     continue; // not very nice but efficient
150
//TODO: wrap the curly braces around the next statements
151
}
152
153                 lang = lang.substring(0, lang.indexOf(".properties") );
154
155                 StringTokenizer tokenizer = new StringTokenizer( lang, "_");
156                 if ( tokenizer.countTokens() <= 1 )
157                 {
158                     // empty filename or "iReport.properties"
159
continue; // not very nice but efficient
160
//TODO: wrap the curly braces around the next statements
161
}
162
163                 String JavaDoc language = "";
164                 String JavaDoc country = "";
165                 String JavaDoc variant = "";
166
167                 String JavaDoc[] parts = new String JavaDoc[tokenizer.countTokens()];
168                 // first token (position 0) is iReport
169
//System.out.println( "\n File: " + lang + "\n" );
170

171                 //System.out.println( "\n Aantal tokens: " + " " + tokenizer.countTokens() + "\n" );
172
int i = 0;
173                 while (tokenizer.hasMoreTokens() )
174                 {
175                     String JavaDoc token = tokenizer.nextToken();
176
177                     //System.out.println( "\n" + i + " " + token + "\n" );
178

179                     switch (i)
180                     {
181                         case 0:
182                             //the word iReport
183
break;
184                         case 1:
185                             language = token;
186                             break;
187                         case 2:
188                             country = token;
189                             break;
190                         case 3:
191                             variant = token;
192                             break;
193                         default:
194                             //
195
}
196                     i++;
197
198                 }
199
200                 Locale model = new Locale( language, country, variant );
201                 supportedLocales.add( model );
202
203             }
204         }
205         catch(Exception JavaDoc e)
206         {
207             e.printStackTrace();
208         }
209
210         // Sort the list. Probably should use the current locale when getting the
211
// DisplayLanguage so the sort order is correct for the user.
212
Collections.sort( supportedLocales, new Comparator()
213         {
214             public int compare(Object JavaDoc lhs, Object JavaDoc rhs)
215             {
216                 //Locale ll = (Locale)lhs;
217
//Locale rl = (Locale)rhs;
218
String JavaDoc ls = ((Locale)lhs).getDisplayLanguage();
219                 String JavaDoc rs = ((Locale)rhs).getDisplayLanguage();
220
221                 // this is not very nice
222
// We should introduce a MyLocale
223
if (ls.equals("pap"))
224                 {
225                     ls = "Papiamentu";
226                 }
227                 if (rs.equals("pap"))
228                 {
229                     rs = "Papiamentu";
230                 }
231
232
233                 //return ll.getDisplayLanguage().compareTo( rl.getDisplayLanguage() );
234
return ls.compareTo( rs );
235             }
236         });
237
238         return supportedLocales;
239     }
240
241
242
243 // public static it.businesslogic.ireport.Language getLanguage(String language){
244
// java.io.File myFiles[] = new java.io.File("lang").listFiles(new it.businesslogic.ireport.util.I18nFilenameFilter());
245
// String filename = "";
246
// java.util.Properties p = new java.util.Properties();
247
// it.businesslogic.ireport.Language model = null;
248
// for(int i = 0; i < java.util.Arrays.asList(myFiles).size(); i++) {
249
// filename = myFiles[i].getName();
250
// try{
251
// p.load(new java.io.FileInputStream("lang"+java.io.File.separatorChar+filename));
252
// if (!filename.equals(baseName+".properties") && !p.getProperty("LanguageName").equals("")
253
// && p.getProperty("LanguageID").equals(language)) {
254
// model = new it.businesslogic.ireport.Language();
255
// model.setFilenameWithPath("lang"+java.io.File.separatorChar+filename);
256
// model.setFilename(filename);
257
// model.setLanguageName(p.getProperty("LanguageName"));
258
// model.setId(p.getProperty("LanguageID"));
259
// break;
260
// }
261
// }catch(Exception e){e.printStackTrace();}
262
// }
263
// return model;
264
// }
265

266
267     // Default to english
268
private static Locale currentLocale = Locale.ENGLISH;
269
270     public static void setCurrentLocale( String JavaDoc language )
271     {
272         setCurrentLocale(language, null);
273     }
274
275     public static void setCurrentLocale( String JavaDoc language, String JavaDoc country )
276     {
277         if(language != null && !language.equals(""))
278         {
279             if(country != null && !country.equals(""))
280             {
281                 setCurrentLocale(new java.util.Locale JavaDoc(language, country));
282             }
283             else
284             {
285                 setCurrentLocale(new java.util.Locale JavaDoc(language));
286             }
287         }
288         else
289         {
290             setCurrentLocale(java.util.Locale.getDefault());
291         }
292
293     }
294
295     public static void setCurrentLocale( Locale locale )
296     {
297         currentLocale=locale;
298         oLanguage = null;
299
300         Enumeration enum_listeners = languageChangedListeners.elements();
301         while (enum_listeners.hasMoreElements())
302         {
303             try
304             {
305                 ((LanguageChangedListener)(enum_listeners.nextElement())).languageChanged(new LanguageChangedEvent(locale));
306             }
307             catch (Exception JavaDoc ex)
308             {}
309         }
310     }
311
312     public static Locale getCurrentLocale()
313     {
314         return currentLocale;
315     }
316
317     /**
318      * Retreive a resource string using the current locale.
319      * @param cID The resouce sting identifier
320      * @return The locale specific string
321      */

322     public static String JavaDoc getString(String JavaDoc cID)
323     {
324         return getString(cID, currentLocale );
325     }
326
327     public static String JavaDoc getString(String JavaDoc cID,String JavaDoc defaultValue)
328     {
329         return getString(cID, currentLocale, defaultValue );
330     }
331
332     /**
333      * Retreive a resource string using the current locale.
334      * @param cID The resouce sting identifier
335      * @return The locale specific string
336      */

337     public static String JavaDoc getFormattedString(String JavaDoc cID, String JavaDoc defaultValue, Object JavaDoc[] args)
338     {
339         String JavaDoc pattern = getString(cID, getCurrentLocale(), defaultValue );
340         java.text.MessageFormat JavaDoc mf = new java.text.MessageFormat JavaDoc(pattern, I18n.getCurrentLocale());
341         return mf.format(args);
342     }
343
344
345     private static String JavaDoc getString(String JavaDoc cID, Locale currentLocale)
346     {
347         if (currentLocale == null)
348         {
349             currentLocale = Locale.getDefault();
350         }
351         if(oLanguage == null)
352         {
353             oLanguage = java.util.ResourceBundle.getBundle( localPackageName + baseName,
354                     currentLocale);
355         }
356
357         try {
358             return oLanguage.getString(cID);
359         } catch (Exception JavaDoc ex)
360         {
361             return cID;
362         }
363     }
364
365     public static String JavaDoc getString(String JavaDoc cID, Locale currentLocale, String JavaDoc defaultValue)
366     {
367         try
368         {
369             if(oLanguage == null)
370             {
371                 if (MainFrame.getMainInstance() != null)
372                 {
373                     oLanguage = java.util.ResourceBundle.getBundle( localPackageName + baseName,
374                         currentLocale, MainFrame.getMainInstance().getReportClassLoader());
375                 }
376                 else
377                 {
378                     oLanguage = java.util.ResourceBundle.getBundle( localPackageName + baseName,
379                         currentLocale);
380                 }
381             }
382             return oLanguage.getString(cID);
383         }
384         catch (MissingResourceException ex)
385         {
386             System.out.println("Can't find the translation for key = " + cID +": using default (" + defaultValue + ")");
387         }
388         catch (Exception JavaDoc ex)
389         {
390             System.out.println("Exception loading cID = " + cID +": " + ex.getMessage());
391         }
392         return defaultValue;
393     }
394
395     /** ErtanO 16.03.2004: not working currently as MainFrame is not accessible therefore we overgive languageid to getString() **/
396     public static String JavaDoc getCurrentLocaleID()
397     {
398         //return it.businesslogic.ireport.gui.MainFrame.getProperties().getProperty("Language");
399
return "";
400     }
401 }
402
Popular Tags