4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.util.Enumeration;
8 import java.util.Locale;
9 import java.util.MissingResourceException;
10 import java.util.Properties;
11 import java.util.ResourceBundle;
14 * Manager for all internationalization
15 * Responsible for loading property files
16 * and delivering language-specific texts
18 public abstract class I18nManager
20 /** Properties object into which all the texts are copied */
21 private static Properties LocalTexts = null;
23 /** External properties file for developer testing */
24 private static Properties ExternalPropsFile = null;
28 * Initialize the library using the (optional) locale
29 * @param inLocale locale to use, or null for default
31 public static void init(Locale inLocale)
33 final String BUNDLE_NAME = "tim.prune.lang.prune-texts";
34 final Locale BACKUP_LOCALE = new Locale("en", "GB");
36 LocalTexts = new Properties();
37 // Load English texts first to use as defaults
38 loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE));
40 // Get bundle for selected locale, if any
45 loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, inLocale));
49 // locale is null so just use the system default
50 loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault()));
53 catch (MissingResourceException mre) { // ignore error, default to english
58 * Copy all the translations from the given bundle and store in the Properties object
59 * overwriting the existing translations if necessary
60 * @param inBundle bundle object loaded from file
62 private static void loadFromBundle(ResourceBundle inBundle)
64 Enumeration<String> e = inBundle.getKeys();
65 while (e.hasMoreElements())
67 String key = e.nextElement();
68 LocalTexts.setProperty(key, inBundle.getString(key));
74 * @param inFilename filename of file
75 * @throws FileNotFoundException if load failed
77 public static void addLanguageFile(String inFilename) throws FileNotFoundException
79 FileInputStream fis = null;
80 boolean fileLoaded = false;
83 File file = new File(inFilename);
84 ExternalPropsFile = new Properties();
85 fis = new FileInputStream(file);
86 ExternalPropsFile.load(fis);
87 fileLoaded = true; // everything worked
89 catch (IOException ioe) {}
90 finally { try { fis.close();
91 } catch (Exception e) {}
93 // complain if file wasn't loaded, by throwing a filenotfound exception
94 if (!fileLoaded) throw new FileNotFoundException();
99 * Lookup the given key and return the associated text
100 * @param inKey key to lookup
101 * @return associated text, or the key if not found
103 public static String getText(String inKey)
105 // look in external props file if available
106 if (ExternalPropsFile != null)
108 String extText = ExternalPropsFile.getProperty(inKey);
109 if (extText != null) return extText;
111 // look in texts if available
112 if (LocalTexts != null)
116 String localText = LocalTexts.getProperty(inKey);
117 if (localText != null) return localText;
119 catch (MissingResourceException mre) {}
121 // return the key itself
126 * Lookup the given key and return the associated text, formatting with the number
127 * @param inKey key to lookup (text should contain a %d)
128 * @param inNumber number to substitute into the %d
129 * @return associated text, or the key if not found
131 public static String getTextWithNumber(String inKey, int inNumber)
133 String localText = getText(inKey);
136 localText = String.format(localText, inNumber);
139 {} // printf formatting didn't work, maybe the placeholders are wrong?