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