4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.util.Locale;
8 import java.util.MissingResourceException;
9 import java.util.Properties;
10 import java.util.ResourceBundle;
13 * Manager for all internationalization
14 * Responsible for loading property files
15 * and delivering language-specific texts
17 public abstract class I18nManager
19 private static final String BUNDLE_NAME = "tim.prune.lang.prune-texts";
20 private static final Locale BACKUP_LOCALE = new Locale("en", "GB");
22 private static ResourceBundle EnglishTexts = null;
23 private static ResourceBundle LocalTexts = null;
25 /** External properties file for developer testing */
26 private static Properties ExternalPropsFile = null;
30 * Initialize the library using the (optional) locale
31 * @param inLocale locale to use, or null for default
33 public static void init(Locale inLocale)
35 // Load English texts first to use as defaults
36 EnglishTexts = ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE);
38 // Get bundle for selected locale, if any
43 LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, inLocale);
47 // locale is null so just use the system default
48 LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault());
51 catch (MissingResourceException mre) { // ignore error, default to english
58 * @param inFilename filename of file
59 * @throws FileNotFoundException if load failed
61 public static void addLanguageFile(String inFilename) throws FileNotFoundException
63 FileInputStream fis = null;
64 boolean fileLoaded = false;
67 File file = new File(inFilename);
68 ExternalPropsFile = new Properties();
69 fis = new FileInputStream(file);
70 ExternalPropsFile.load(fis);
71 fileLoaded = true; // everything worked
73 catch (IOException ioe) {}
74 finally { try { fis.close();
75 } catch (Exception e) {}
77 // complain if file wasn't loaded, by throwing a filenotfound exception
78 if (!fileLoaded) throw new FileNotFoundException();
83 * Lookup the given key and return the associated text
84 * @param inKey key to lookup
85 * @return associated text, or the key if not found
87 public static String getText(String inKey)
89 // look in external props file if available
90 if (ExternalPropsFile != null)
92 String extText = ExternalPropsFile.getProperty(inKey);
93 if (extText != null) return extText;
95 // look in extra texts if available
96 if (LocalTexts != null)
100 String localText = LocalTexts.getString(inKey);
101 if (localText != null) return localText;
103 catch (MissingResourceException mre) {}
105 // look in english texts
106 if (EnglishTexts != null)
110 String engText = EnglishTexts.getString(inKey);
111 if (engText != null) return engText;
113 catch (MissingResourceException mre) {}
115 // return the key itself