X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2FI18nManager.java;h=fbf781eb3fef3a79adbdc72548eb5b3c66c83f91;hb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465;hp=c056fe24d743ccb5e4cccd4118e21a733fccda02;hpb=312fec956e43f5d0a38617da5d0add9c62563e2c;p=GpsPrune.git diff --git a/tim/prune/I18nManager.java b/tim/prune/I18nManager.java index c056fe2..fbf781e 100644 --- a/tim/prune/I18nManager.java +++ b/tim/prune/I18nManager.java @@ -1,7 +1,13 @@ package tim.prune; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Enumeration; import java.util.Locale; import java.util.MissingResourceException; +import java.util.Properties; import java.util.ResourceBundle; /** @@ -11,34 +17,83 @@ import java.util.ResourceBundle; */ public abstract class I18nManager { - private static final String BUNDLE_NAME = "tim.prune.lang.prune-texts"; - private static final Locale BACKUP_LOCALE = new Locale("en", "GB"); + /** Properties object into which all the texts are copied */ + private static Properties LocalTexts = null; - private static ResourceBundle EnglishTexts = null; - private static ResourceBundle ExtraTexts = null; + /** External properties file for developer testing */ + private static Properties ExternalPropsFile = null; /** - * Initialize the library - * using the (optional) locale + * Initialize the library using the (optional) locale + * @param inLocale locale to use, or null for default */ public static void init(Locale inLocale) { + final String BUNDLE_NAME = "tim.prune.lang.prune-texts"; + final Locale BACKUP_LOCALE = new Locale("en", "GB"); + + LocalTexts = new Properties(); // Load English texts first to use as defaults - EnglishTexts = ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE); + loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE)); // Get bundle for selected locale, if any - if (inLocale != null) + try { - ExtraTexts = ResourceBundle.getBundle(BUNDLE_NAME, inLocale); + if (inLocale != null) + { + loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, inLocale)); + } + else + { + // locale is null so just use the system default + loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault())); + } + } + catch (MissingResourceException mre) { // ignore error, default to english } - else + } + + /** + * Copy all the translations from the given bundle and store in the Properties object + * overwriting the existing translations if necessary + * @param inBundle bundle object loaded from file + */ + private static void loadFromBundle(ResourceBundle inBundle) + { + Enumeration e = inBundle.getKeys(); + while (e.hasMoreElements()) { - // locale is null so just use the system default - ExtraTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault()); + String key = e.nextElement(); + LocalTexts.setProperty(key, inBundle.getString(key)); } } + /** + * Add a language file + * @param inFilename filename of file + * @throws FileNotFoundException if load failed + */ + public static void addLanguageFile(String inFilename) throws FileNotFoundException + { + FileInputStream fis = null; + boolean fileLoaded = false; + try + { + File file = new File(inFilename); + ExternalPropsFile = new Properties(); + fis = new FileInputStream(file); + ExternalPropsFile.load(fis); + fileLoaded = true; // everything worked + } + catch (IOException ioe) {} + finally { try { fis.close(); + } catch (Exception e) {} + } + // complain if file wasn't loaded, by throwing a filenotfound exception + if (!fileLoaded) throw new FileNotFoundException(); + } + /** * Lookup the given key and return the associated text @@ -47,30 +102,41 @@ public abstract class I18nManager */ public static String getText(String inKey) { - String value = null; - // look in extra texts if available - if (ExtraTexts != null) + // look in external props file if available + if (ExternalPropsFile != null) { - try - { - value = ExtraTexts.getString(inKey); - if (value != null && !value.equals("")) - return value; - } - catch (MissingResourceException mre) {} + String extText = ExternalPropsFile.getProperty(inKey); + if (extText != null) return extText; } - // look in english texts - if (EnglishTexts != null) + // look in texts if available + if (LocalTexts != null) { try { - value = EnglishTexts.getString(inKey); - if (value != null && !value.equals("")) - return value; + String localText = LocalTexts.getProperty(inKey); + if (localText != null) return localText; } catch (MissingResourceException mre) {} } // return the key itself return inKey; } + + /** + * Lookup the given key and return the associated text, formatting with the number + * @param inKey key to lookup (text should contain a %d) + * @param inNumber number to substitute into the %d + * @return associated text, or the key if not found + */ + public static String getTextWithNumber(String inKey, int inNumber) + { + String localText = getText(inKey); + try + { + localText = String.format(localText, inNumber); + } + catch (Exception e) + {} // printf formatting didn't work, maybe the placeholders are wrong? + return localText; + } }