]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/I18nManager.java
Version 10, May 2010
[GpsPrune.git] / tim / prune / I18nManager.java
index 87d1df4997c1ab78f5eb0ac4d0ac7dc95552c5c0..349db3fcaff83bace412ebdf02e459b28d1e9199 100644 (file)
@@ -2,6 +2,7 @@ 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;
@@ -35,14 +36,19 @@ public abstract class I18nManager
                EnglishTexts = ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE);
 
                // Get bundle for selected locale, if any
-               if (inLocale != null)
+               try
                {
-                       LocalTexts = 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());
+                       }
                }
-               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
                }
        }
 
@@ -50,16 +56,26 @@ public abstract class I18nManager
        /**
         * Add a language file
         * @param inFilename filename of file
+        * @throws FileNotFoundException if load failed
         */
-       public static void addLanguageFile(String inFilename)
+       public static void addLanguageFile(String inFilename) throws FileNotFoundException
        {
+               FileInputStream fis = null;
+               boolean fileLoaded = false;
                try
                {
                        File file = new File(inFilename);
                        ExternalPropsFile = new Properties();
-                       ExternalPropsFile.load(new FileInputStream(file));
+                       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();
        }
 
 
@@ -70,22 +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)
                {
-                       value = ExternalPropsFile.getProperty(inKey);
-                       if (value != null && !value.equals(""))
-                               return value;
+                       String extText = ExternalPropsFile.getProperty(inKey);
+                       if (extText != null) return extText;
                }
                // look in extra texts if available
                if (LocalTexts != null)
                {
                        try
                        {
-                               value = LocalTexts.getString(inKey);
-                               if (value != null && !value.equals(""))
-                                       return value;
+                               String localText = LocalTexts.getString(inKey);
+                               if (localText != null) return localText;
                        }
                        catch (MissingResourceException mre) {}
                }
@@ -94,9 +107,8 @@ 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) {}
                }