X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2FI18nManager.java;h=24a4fde41ab2a73288dcdb7975c9c7791f54cf47;hb=326f489e36aa7f235bc19409a57bf4955cd50f24;hp=5af9898ed2c56f5ae1ee949ae19bb71dd25f3d2c;hpb=da0b1f449260a0b4a94318006382a9039726ef3e;p=GpsPrune.git diff --git a/tim/prune/I18nManager.java b/tim/prune/I18nManager.java index 5af9898..24a4fde 100644 --- a/tim/prune/I18nManager.java +++ b/tim/prune/I18nManager.java @@ -1,7 +1,12 @@ package tim.prune; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Locale; import java.util.MissingResourceException; +import java.util.Properties; import java.util.ResourceBundle; /** @@ -15,7 +20,10 @@ public abstract class I18nManager private static final Locale BACKUP_LOCALE = new Locale("en", "GB"); private static ResourceBundle EnglishTexts = null; - private static ResourceBundle ExtraTexts = null; + private static ResourceBundle LocalTexts = null; + + /** External properties file for developer testing */ + private static Properties ExternalPropsFile = null; /** @@ -28,15 +36,46 @@ public abstract class I18nManager EnglishTexts = 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) + { + LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, inLocale); + } + else + { + // locale is null so just use the system default + LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault()); + } + } + catch (MissingResourceException mre) { // ignore error, default to english } - else + } + + + /** + * 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 { - // locale is null so just use the system default - ExtraTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault()); + 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(); } @@ -47,15 +86,19 @@ public abstract class I18nManager */ public static String getText(String inKey) { - String value = null; + // look in external props file if available + if (ExternalPropsFile != null) + { + String extText = ExternalPropsFile.getProperty(inKey); + if (extText != null) return extText; + } // look in extra texts if available - if (ExtraTexts != null) + if (LocalTexts != null) { try { - value = ExtraTexts.getString(inKey); - if (value != null && !value.equals("")) - return value; + String localText = LocalTexts.getString(inKey); + if (localText != null) return localText; } catch (MissingResourceException mre) {} } @@ -64,13 +107,30 @@ public abstract class I18nManager { try { - value = EnglishTexts.getString(inKey); - if (value != null && !value.equals("")) - return value; + String engText = EnglishTexts.getString(inKey); + if (engText != null) return engText; } 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; + } }