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)
58 File file = new File(inFilename);
59 ExternalPropsFile = new Properties();
60 ExternalPropsFile.load(new FileInputStream(file));
62 catch (IOException ioe) {}
67 * Lookup the given key and return the associated text
68 * @param inKey key to lookup
69 * @return associated text, or the key if not found
71 public static String getText(String inKey)
74 // look in external props file if available
75 if (ExternalPropsFile != null)
77 value = ExternalPropsFile.getProperty(inKey);
78 if (value != null && !value.equals(""))
81 // look in extra texts if available
82 if (LocalTexts != null)
86 value = LocalTexts.getString(inKey);
87 if (value != null && !value.equals(""))
90 catch (MissingResourceException mre) {}
92 // look in english texts
93 if (EnglishTexts != null)
97 value = EnglishTexts.getString(inKey);
98 if (value != null && !value.equals(""))
101 catch (MissingResourceException mre) {}
103 // return the key itself