]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/I18nManager.java
Version 4, January 2008
[GpsPrune.git] / tim / prune / I18nManager.java
1 package tim.prune;
2
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 /**
8  * Manager for all internationalization
9  * Responsible for loading property files
10  * and delivering language-specific texts
11  */
12 public abstract class I18nManager
13 {
14         private static final String BUNDLE_NAME = "tim.prune.lang.prune-texts";
15         private static final Locale BACKUP_LOCALE = new Locale("en", "GB");
16
17         private static ResourceBundle EnglishTexts = null;
18         private static ResourceBundle ExtraTexts = null;
19
20
21         /**
22          * Initialize the library using the (optional) locale
23          * @param inLocale locale to use, or null for default
24          */
25         public static void init(Locale inLocale)
26         {
27                 // Load English texts first to use as defaults
28                 EnglishTexts = ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE);
29
30                 // Get bundle for selected locale, if any
31                 if (inLocale != null)
32                 {
33                         ExtraTexts = ResourceBundle.getBundle(BUNDLE_NAME, inLocale);
34                 }
35                 else
36                 {
37                         // locale is null so just use the system default
38                         ExtraTexts = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault());
39                 }
40         }
41
42
43         /**
44          * Lookup the given key and return the associated text
45          * @param inKey key to lookup
46          * @return associated text, or the key if not found
47          */
48         public static String getText(String inKey)
49         {
50                 String value = null;
51                 // look in extra texts if available
52                 if (ExtraTexts != null)
53                 {
54                         try
55                         {
56                                 value = ExtraTexts.getString(inKey);
57                                 if (value != null && !value.equals(""))
58                                         return value;
59                         }
60                         catch (MissingResourceException mre) {}
61                 }
62                 // look in english texts
63                 if (EnglishTexts != null)
64                 {
65                         try
66                         {
67                                 value = EnglishTexts.getString(inKey);
68                                 if (value != null && !value.equals(""))
69                                         return value;
70                         }
71                         catch (MissingResourceException mre) {}
72                 }
73                 // return the key itself
74                 return inKey;
75         }
76 }