]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/I18nManager.java
Version 20.4, May 2021
[GpsPrune.git] / src / 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.Enumeration;
8 import java.util.Locale;
9 import java.util.MissingResourceException;
10 import java.util.Properties;
11 import java.util.ResourceBundle;
12
13 /**
14  * Manager for all internationalization
15  * Responsible for loading property files
16  * and delivering language-specific texts
17  */
18 public abstract class I18nManager
19 {
20         /** Properties object into which all the texts are copied */
21         private static Properties _localTexts = null;
22
23         /** External properties file for developer testing */
24         private static Properties _externalPropsFile = null;
25
26
27         /**
28          * Initialize the library using the (optional) locale
29          * @param inLocale locale to use, or null for default
30          */
31         public static void init(Locale inLocale)
32         {
33                 final String BUNDLE_NAME = "tim.prune.lang.prune-texts";
34                 final Locale BACKUP_LOCALE = new Locale("en", "GB");
35
36                 _localTexts = new Properties();
37                 // Load English texts first to use as defaults
38                 loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, BACKUP_LOCALE));
39
40                 // Get bundle for selected locale, if any
41                 try
42                 {
43                         if (inLocale != null)
44                         {
45                                 loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, inLocale));
46                         }
47                         else
48                         {
49                                 // locale is null so just use the system default
50                                 loadFromBundle(ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault()));
51                         }
52                 }
53                 catch (MissingResourceException mre) { // ignore error, default to english
54                 }
55         }
56
57         /**
58          * Copy all the translations from the given bundle and store in the Properties object
59          * overwriting the existing translations if necessary
60          * @param inBundle bundle object loaded from file
61          */
62         private static void loadFromBundle(ResourceBundle inBundle)
63         {
64                 Enumeration<String> e = inBundle.getKeys();
65                 while (e.hasMoreElements())
66                 {
67                         String key = e.nextElement();
68                         _localTexts.setProperty(key, inBundle.getString(key));
69                 }
70         }
71
72         /**
73          * Add a language file
74          * @param inFilename filename of file
75          * @throws FileNotFoundException if load failed
76          */
77         public static void addLanguageFile(String inFilename) throws FileNotFoundException
78         {
79                 FileInputStream fis = null;
80                 boolean fileLoaded = false;
81                 try
82                 {
83                         File file = new File(inFilename);
84                         _externalPropsFile = new Properties();
85                         fis = new FileInputStream(file);
86                         _externalPropsFile.load(fis);
87                         fileLoaded = true; // everything worked
88                 }
89                 catch (IOException ioe) {}
90                 finally { try { fis.close();
91                         } catch (Exception e) {}
92                 }
93                 // complain if file wasn't loaded, by throwing a filenotfound exception
94                 if (!fileLoaded) throw new FileNotFoundException();
95         }
96
97
98         /**
99          * Lookup the given key and return the associated text
100          * @param inKey key to lookup
101          * @return associated text, or the key if not found
102          */
103         public static String getText(String inKey)
104         {
105                 // look in external props file if available
106                 if (_externalPropsFile != null)
107                 {
108                         String extText = _externalPropsFile.getProperty(inKey);
109                         if (extText != null) return extText;
110                 }
111                 // look in texts if available
112                 if (_localTexts != null)
113                 {
114                         try
115                         {
116                                 String localText = _localTexts.getProperty(inKey);
117                                 if (localText != null) return localText;
118                         }
119                         catch (MissingResourceException mre) {}
120                 }
121                 // return the key itself
122                 return inKey;
123         }
124
125         /**
126          * Lookup the given key and return the associated text, formatting with the number
127          * @param inKey key to lookup (text should contain a %d)
128          * @param inNumber number to substitute into the %d
129          * @return associated text, or the key if not found
130          */
131         public static String getTextWithNumber(String inKey, int inNumber)
132         {
133                 String localText = getText(inKey);
134                 try
135                 {
136                         localText = String.format(localText, inNumber);
137                 }
138                 catch (Exception e)
139                 {} // printf formatting didn't work, maybe the placeholders are wrong?
140                 return localText;
141         }
142 }