]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/I18nManager.java
Version 10, May 2010
[GpsPrune.git] / tim / prune / I18nManager.java
1 package tim.prune;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.util.Locale;
8 import java.util.MissingResourceException;
9 import java.util.Properties;
10 import java.util.ResourceBundle;
11
12 /**
13  * Manager for all internationalization
14  * Responsible for loading property files
15  * and delivering language-specific texts
16  */
17 public abstract class I18nManager
18 {
19         private static final String BUNDLE_NAME = "tim.prune.lang.prune-texts";
20         private static final Locale BACKUP_LOCALE = new Locale("en", "GB");
21
22         private static ResourceBundle EnglishTexts = null;
23         private static ResourceBundle LocalTexts = null;
24
25         /** External properties file for developer testing */
26         private static Properties ExternalPropsFile = null;
27
28
29         /**
30          * Initialize the library using the (optional) locale
31          * @param inLocale locale to use, or null for default
32          */
33         public static void init(Locale inLocale)
34         {
35                 // Load English texts first to use as defaults
36                 EnglishTexts = ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE);
37
38                 // Get bundle for selected locale, if any
39                 try
40                 {
41                         if (inLocale != null)
42                         {
43                                 LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, inLocale);
44                         }
45                         else
46                         {
47                                 // locale is null so just use the system default
48                                 LocalTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault());
49                         }
50                 }
51                 catch (MissingResourceException mre) { // ignore error, default to english
52                 }
53         }
54
55
56         /**
57          * Add a language file
58          * @param inFilename filename of file
59          * @throws FileNotFoundException if load failed
60          */
61         public static void addLanguageFile(String inFilename) throws FileNotFoundException
62         {
63                 FileInputStream fis = null;
64                 boolean fileLoaded = false;
65                 try
66                 {
67                         File file = new File(inFilename);
68                         ExternalPropsFile = new Properties();
69                         fis = new FileInputStream(file);
70                         ExternalPropsFile.load(fis);
71                         fileLoaded = true; // everything worked
72                 }
73                 catch (IOException ioe) {}
74                 finally { try { fis.close();
75                         } catch (Exception e) {}
76                 }
77                 // complain if file wasn't loaded, by throwing a filenotfound exception
78                 if (!fileLoaded) throw new FileNotFoundException();
79         }
80
81
82         /**
83          * Lookup the given key and return the associated text
84          * @param inKey key to lookup
85          * @return associated text, or the key if not found
86          */
87         public static String getText(String inKey)
88         {
89                 // look in external props file if available
90                 if (ExternalPropsFile != null)
91                 {
92                         String extText = ExternalPropsFile.getProperty(inKey);
93                         if (extText != null) return extText;
94                 }
95                 // look in extra texts if available
96                 if (LocalTexts != null)
97                 {
98                         try
99                         {
100                                 String localText = LocalTexts.getString(inKey);
101                                 if (localText != null) return localText;
102                         }
103                         catch (MissingResourceException mre) {}
104                 }
105                 // look in english texts
106                 if (EnglishTexts != null)
107                 {
108                         try
109                         {
110                                 String engText = EnglishTexts.getString(inKey);
111                                 if (engText != null) return engText;
112                         }
113                         catch (MissingResourceException mre) {}
114                 }
115                 // return the key itself
116                 return inKey;
117         }
118 }