4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.Locale;
7 import java.util.MissingResourceException;
8 import java.util.Properties;
9 import java.util.ResourceBundle;
12 * Manager for all internationalization
13 * Responsible for loading property files
14 * and delivering language-specific texts
16 public abstract class I18nManager
18 private static final String BUNDLE_NAME = "tim.prune.lang.prune-texts";
19 private static final Locale BACKUP_LOCALE = new Locale("en", "GB");
21 private static ResourceBundle EnglishTexts = null;
22 private static ResourceBundle LocalTexts = null;
24 /** External properties file for developer testing */
25 private static Properties ExternalPropsFile = null;
29 * Initialize the library using the (optional) locale
30 * @param inLocale locale to use, or null for default
32 public static void init(Locale inLocale)
34 // Load English texts first to use as defaults
35 EnglishTexts = ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE);
37 // Get bundle for selected locale, if any
40 LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, inLocale);
44 // locale is null so just use the system default
45 LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault());
52 * @param inFilename filename of file
54 public static void addLanguageFile(String inFilename)
56 FileInputStream fis = null;
59 File file = new File(inFilename);
60 ExternalPropsFile = new Properties();
61 fis = new FileInputStream(file);
62 ExternalPropsFile.load(fis);
64 catch (IOException ioe) {}
65 finally { try { fis.close();
66 } catch (Exception e) {}
72 * Lookup the given key and return the associated text
73 * @param inKey key to lookup
74 * @return associated text, or the key if not found
76 public static String getText(String inKey)
79 // look in external props file if available
80 if (ExternalPropsFile != null)
82 value = ExternalPropsFile.getProperty(inKey);
83 if (value != null && !value.equals(""))
86 // look in extra texts if available
87 if (LocalTexts != null)
91 value = LocalTexts.getString(inKey);
92 if (value != null && !value.equals(""))
95 catch (MissingResourceException mre) {}
97 // look in english texts
98 if (EnglishTexts != null)
102 value = EnglishTexts.getString(inKey);
103 if (value != null && !value.equals(""))
106 catch (MissingResourceException mre) {}
108 // return the key itself