]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/I18nManager.java
Version 6, October 2008
[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                 try
57                 {
58                         File file = new File(inFilename);
59                         ExternalPropsFile = new Properties();
60                         ExternalPropsFile.load(new FileInputStream(file));
61                 }
62                 catch (IOException ioe) {}
63         }
64
65
66         /**
67          * Lookup the given key and return the associated text
68          * @param inKey key to lookup
69          * @return associated text, or the key if not found
70          */
71         public static String getText(String inKey)
72         {
73                 String value = null;
74                 // look in external props file if available
75                 if (ExternalPropsFile != null)
76                 {
77                         value = ExternalPropsFile.getProperty(inKey);
78                         if (value != null && !value.equals(""))
79                                 return value;
80                 }
81                 // look in extra texts if available
82                 if (LocalTexts != null)
83                 {
84                         try
85                         {
86                                 value = LocalTexts.getString(inKey);
87                                 if (value != null && !value.equals(""))
88                                         return value;
89                         }
90                         catch (MissingResourceException mre) {}
91                 }
92                 // look in english texts
93                 if (EnglishTexts != null)
94                 {
95                         try
96                         {
97                                 value = EnglishTexts.getString(inKey);
98                                 if (value != null && !value.equals(""))
99                                         return value;
100                         }
101                         catch (MissingResourceException mre) {}
102                 }
103                 // return the key itself
104                 return inKey;
105         }
106 }