001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * I18n.java
028: *
029: * Created on 13 March 2004, 21:43
030: *
031: */
032:
033: package it.businesslogic.ireport.util;
034:
035: import it.businesslogic.ireport.gui.MainFrame;
036: import java.util.*;
037: import java.util.jar.*;
038: import java.net.*;
039: import java.io.*;
040:
041: /**
042: *
043: * @author ertano
044: */
045: public class I18n {
046:
047: public static final String localPackageName = "it/businesslogic/ireport/locale/";
048: public static final String baseName = "Ireport";
049: private static java.util.ResourceBundle oLanguage = null;
050:
051: public static java.util.Vector languageChangedListeners = null;
052:
053: static {
054:
055: languageChangedListeners = new Vector();
056: }
057:
058: public static void addOnLanguageChangedListener(
059: LanguageChangedListener listener) {
060: languageChangedListeners.add(listener);
061: }
062:
063: /**
064: * Get the list of supported translations.
065: *
066: * Load the list of property files in the it/businesslogic/ireport/locale/
067: * package.
068: *
069: */
070:
071: /*
072: public static java.util.List getListOfAvailLanguages(){
073: java.util.List supportedLocales = new java.util.ArrayList();
074: try {
075: Set names = Misc.getResoucesInPackage( localPackageName );
076: Iterator it = names.iterator();
077: while( it.hasNext() ) {
078: String n = (String)it.next();
079:
080: // From
081: // 'it/businesslogic/ireport/locale/Ireport_en.properties'
082: // or
083: // 'it/businesslogic/ireport/locale/Ireport_en_UK.properties'
084: // To
085: // 'en' OR 'en_UK'
086:
087: String lang = n.substring( n.lastIndexOf('/')+1 );
088: lang = lang.substring(0, lang.indexOf(".properties") );
089: if(lang.indexOf("_")>0) // otherwise index exception
090: lang = lang.substring( baseName.length()+1 ); // +1 to include the underscore
091:
092: Locale model;
093: int underscorePos = lang.indexOf('_');
094: if( underscorePos==-1 ) {
095: String language = lang;
096: model = new Locale( language );
097: } else {
098: String language = lang.substring( 0, lang.indexOf('_') );
099: String country = lang.substring( lang.indexOf('_')+1 );
100: model = new Locale( language, country );
101: }
102: supportedLocales.add( model );
103: }
104: } catch(Exception e) {
105: e.printStackTrace();
106: }
107:
108: // Sort the list. Probably should use the current locale when getting the
109: // DisplayLanguage so the sort order is correct for the user.
110: Collections.sort( supportedLocales, new Comparator() {
111: public int compare(Object lhs, Object rhs) {
112: Locale ll = (Locale)lhs;
113: Locale rl = (Locale)rhs;
114: return ll.getDisplayLanguage().compareTo( rl.getDisplayLanguage() );
115: }
116: });
117:
118: return supportedLocales;
119: }
120: */
121:
122: // => Modified by RL: June 3, 2005: Introducing Variant
123: public static java.util.List getListOfAvailLanguages() {
124: java.util.List supportedLocales = new java.util.ArrayList();
125:
126: try {
127: Set names = Misc.getResoucesInPackage(localPackageName);
128: Iterator it = names.iterator();
129: while (it.hasNext()) {
130: String n = (String) it.next();
131:
132: // From
133: // 'it/businesslogic/ireport/locale/Ireport_en.properties'
134: // or
135: // 'it/businesslogic/ireport/locale/Ireport_en_UK.properties'
136: // To
137: // 'en' OR 'en_UK_' OR even en_UK_Brighton dialect
138:
139: String lang = n.substring(n.lastIndexOf('/') + 1);
140:
141: // only except resources with extension '.properties'
142: if (lang.indexOf(".properties") < 0) {
143: continue; // not very nice but efficient
144: //TODO: wrap the curly braces around the next statements
145: }
146:
147: lang = lang.substring(0, lang.indexOf(".properties"));
148:
149: StringTokenizer tokenizer = new StringTokenizer(lang,
150: "_");
151: if (tokenizer.countTokens() <= 1) {
152: // empty filename or "iReport.properties"
153: continue; // not very nice but efficient
154: //TODO: wrap the curly braces around the next statements
155: }
156:
157: String language = "";
158: String country = "";
159: String variant = "";
160:
161: String[] parts = new String[tokenizer.countTokens()];
162: // first token (position 0) is iReport
163: //System.out.println( "\n File: " + lang + "\n" );
164:
165: //System.out.println( "\n Aantal tokens: " + " " + tokenizer.countTokens() + "\n" );
166: int i = 0;
167: while (tokenizer.hasMoreTokens()) {
168: String token = tokenizer.nextToken();
169:
170: //System.out.println( "\n" + i + " " + token + "\n" );
171:
172: switch (i) {
173: case 0:
174: //the word iReport
175: break;
176: case 1:
177: language = token;
178: break;
179: case 2:
180: country = token;
181: break;
182: case 3:
183: variant = token;
184: break;
185: default:
186: //
187: }
188: i++;
189:
190: }
191:
192: Locale model = new Locale(language, country, variant);
193: supportedLocales.add(model);
194:
195: }
196: } catch (Exception e) {
197: e.printStackTrace();
198: }
199:
200: // Sort the list. Probably should use the current locale when getting the
201: // DisplayLanguage so the sort order is correct for the user.
202: Collections.sort(supportedLocales, new Comparator() {
203: public int compare(Object lhs, Object rhs) {
204: //Locale ll = (Locale)lhs;
205: //Locale rl = (Locale)rhs;
206: String ls = ((Locale) lhs).getDisplayLanguage();
207: String rs = ((Locale) rhs).getDisplayLanguage();
208:
209: // this is not very nice
210: // We should introduce a MyLocale
211: if (ls.equals("pap")) {
212: ls = "Papiamentu";
213: }
214: if (rs.equals("pap")) {
215: rs = "Papiamentu";
216: }
217:
218: //return ll.getDisplayLanguage().compareTo( rl.getDisplayLanguage() );
219: return ls.compareTo(rs);
220: }
221: });
222:
223: return supportedLocales;
224: }
225:
226: // public static it.businesslogic.ireport.Language getLanguage(String language){
227: // java.io.File myFiles[] = new java.io.File("lang").listFiles(new it.businesslogic.ireport.util.I18nFilenameFilter());
228: // String filename = "";
229: // java.util.Properties p = new java.util.Properties();
230: // it.businesslogic.ireport.Language model = null;
231: // for(int i = 0; i < java.util.Arrays.asList(myFiles).size(); i++) {
232: // filename = myFiles[i].getName();
233: // try{
234: // p.load(new java.io.FileInputStream("lang"+java.io.File.separatorChar+filename));
235: // if (!filename.equals(baseName+".properties") && !p.getProperty("LanguageName").equals("")
236: // && p.getProperty("LanguageID").equals(language)) {
237: // model = new it.businesslogic.ireport.Language();
238: // model.setFilenameWithPath("lang"+java.io.File.separatorChar+filename);
239: // model.setFilename(filename);
240: // model.setLanguageName(p.getProperty("LanguageName"));
241: // model.setId(p.getProperty("LanguageID"));
242: // break;
243: // }
244: // }catch(Exception e){e.printStackTrace();}
245: // }
246: // return model;
247: // }
248:
249: // Default to english
250: private static Locale currentLocale = Locale.ENGLISH;
251:
252: public static void setCurrentLocale(String language) {
253: setCurrentLocale(language, null);
254: }
255:
256: public static void setCurrentLocale(String language, String country) {
257: if (language != null && !language.equals("")) {
258: if (country != null && !country.equals("")) {
259: setCurrentLocale(new java.util.Locale(language, country));
260: } else {
261: setCurrentLocale(new java.util.Locale(language));
262: }
263: } else {
264: setCurrentLocale(java.util.Locale.getDefault());
265: }
266:
267: }
268:
269: public static void setCurrentLocale(Locale locale) {
270: currentLocale = locale;
271: oLanguage = null;
272:
273: Enumeration enum_listeners = languageChangedListeners
274: .elements();
275: while (enum_listeners.hasMoreElements()) {
276: try {
277: ((LanguageChangedListener) (enum_listeners
278: .nextElement()))
279: .languageChanged(new LanguageChangedEvent(
280: locale));
281: } catch (Exception ex) {
282: }
283: }
284: }
285:
286: public static Locale getCurrentLocale() {
287: return currentLocale;
288: }
289:
290: /**
291: * Retreive a resource string using the current locale.
292: * @param cID The resouce sting identifier
293: * @return The locale specific string
294: */
295: public static String getString(String cID) {
296: return getString(cID, currentLocale);
297: }
298:
299: public static String getString(String cID, String defaultValue) {
300: return getString(cID, currentLocale, defaultValue);
301: }
302:
303: /**
304: * Retreive a resource string using the current locale.
305: * @param cID The resouce sting identifier
306: * @return The locale specific string
307: */
308: public static String getFormattedString(String cID,
309: String defaultValue, Object[] args) {
310: String pattern = getString(cID, getCurrentLocale(),
311: defaultValue);
312: java.text.MessageFormat mf = new java.text.MessageFormat(
313: pattern, I18n.getCurrentLocale());
314: return mf.format(args);
315: }
316:
317: private static String getString(String cID, Locale currentLocale) {
318: if (currentLocale == null) {
319: currentLocale = Locale.getDefault();
320: }
321: if (oLanguage == null) {
322: oLanguage = java.util.ResourceBundle.getBundle(
323: localPackageName + baseName, currentLocale);
324: }
325:
326: try {
327: return oLanguage.getString(cID);
328: } catch (Exception ex) {
329: return cID;
330: }
331: }
332:
333: public static String getString(String cID, Locale currentLocale,
334: String defaultValue) {
335: try {
336: if (oLanguage == null) {
337: if (MainFrame.getMainInstance() != null) {
338: oLanguage = java.util.ResourceBundle.getBundle(
339: localPackageName + baseName, currentLocale,
340: MainFrame.getMainInstance()
341: .getReportClassLoader());
342: } else {
343: oLanguage = java.util.ResourceBundle.getBundle(
344: localPackageName + baseName, currentLocale);
345: }
346: }
347: return oLanguage.getString(cID);
348: } catch (MissingResourceException ex) {
349: System.out.println("Can't find the translation for key = "
350: + cID + ": using default (" + defaultValue + ")");
351: } catch (Exception ex) {
352: System.out.println("Exception loading cID = " + cID + ": "
353: + ex.getMessage());
354: }
355: return defaultValue;
356: }
357:
358: /** ErtanO 16.03.2004: not working currently as MainFrame is not accessible therefore we overgive languageid to getString() **/
359: public static String getCurrentLocaleID() {
360: //return it.businesslogic.ireport.gui.MainFrame.getProperties().getProperty("Language");
361: return "";
362: }
363: }
|