1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui.i18n;
20
21 import java.io.File;
22 import java.io.FilenameFilter;
23 import java.text.MessageFormat;
24 import java.util.HashSet;
25 import java.util.Locale;
26 import java.util.MissingResourceException;
27 import java.util.ResourceBundle;
28 import java.util.Set;
29
30 /***
31 * LanguageManager.java Created on 23 janv. 2004
32 *
33 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34 * @since 0.54.16
35 * @since 0.54.17 in case of the main language didn't contain the specified
36 * @since 0.54.18 language extension is no more $lang, but .lang to suit to key,
37 * we search in the secondary one linux users, and so sourceforge web
38 * hosting
39 */
40 public final class LanguageManager {
41
42 /***
43 * Create a new instance of this class.
44 */
45 private LanguageManager() {
46 super();
47 }
48
49 /***
50 * The root directory where resourcebundles are located.
51 */
52 public static final String PATH_ROOT = "";
53
54 /***
55 * The default language used is case of error or undefined message if the user
56 * language
57 */
58 public static final String DEFAULT_LANGUAGE = "en";
59
60 /***
61 * The locale language.
62 */
63 public static final String LOCALE_LANGUAGE = Locale.getDefault()
64 .getLanguage();
65
66 /***
67 * The language extension
68 */
69 public static final String LANGUAGE_EXTENSION = "-lang";
70
71 /***
72 * The current language name
73 */
74 private static Language language;
75
76 /***
77 * Represents the available languages and their specifications
78 */
79 public static Set<Language> languages;
80
81 /***
82 * The resource bundle used for all messages
83 */
84 private static ResourceBundle bundle;
85
86 /***
87 * The resource bundle used if the main one did not contain a message in the
88 * user language
89 */
90 private static ResourceBundle secondaryBundle;
91
92 /***
93 * Return the current language name.
94 *
95 * @return the current language name.
96 */
97 public static Language getLanguage() {
98 return language;
99 }
100
101 /***
102 * Load the preferred language, and set the language field
103 *
104 * @param languageName
105 * the language name to use.
106 */
107 public static void initLanguageManager(String languageName) {
108 FilenameFilter filter = new FilenameFilter() {
109 public boolean accept(File dir, String name) {
110 return name.indexOf(LANGUAGE_EXTENSION + ".properties") > 0;
111 }
112 };
113 if (languages != null)
114
115 return;
116 languages = new HashSet<Language>();
117 String[] languages = new File("").getAbsoluteFile().list(filter);
118 for (String language : languages) {
119 language = language.split("//" + LANGUAGE_EXTENSION + "//.properties")[0];
120 ResourceBundle res = ResourceBundle.getBundle(language
121 + LANGUAGE_EXTENSION);
122 LanguageManager.languages.add(new Language(language, language, res
123 .getString("LanguageName"), res.getString("Author"), res
124 .getString("MoreInfo"), res.getString("Version")));
125 }
126 LanguageManager.languages.add(new Language("auto", LOCALE_LANGUAGE,
127 "Default", "", "", ""));
128 setLanguage(languageName);
129 bundle = getPrimaryBundle(PATH_ROOT);
130 secondaryBundle = getSecondaryBundle(PATH_ROOT);
131 }
132
133 /***
134 * Return a bundle corresponding to the specified language in the root. If
135 * there is bundle suiting to the language, it returns the English one.
136 *
137 * @param root
138 * directory where the looked for bundle is located.
139 * @return a bundle corresponding to the specified language.
140 */
141 public static ResourceBundle getPrimaryBundle(String root) {
142 try {
143 return ResourceBundle.getBundle(root + language.getLocale()
144 + LANGUAGE_EXTENSION);
145 } catch (MissingResourceException e) {
146
147 return ResourceBundle.getBundle(DEFAULT_LANGUAGE + LANGUAGE_EXTENSION);
148 }
149 }
150
151 /***
152 * Return resource bundle used if the main one did not contain a message in
153 * the user language
154 *
155 * @param root
156 * directory where the looked for bundle is located.
157 * @return resource bundle used if the main one did not contain a message in
158 * the user language
159 */
160 public static ResourceBundle getSecondaryBundle(String root) {
161 if (DEFAULT_LANGUAGE == language.getLocale()) {
162 return bundle;
163 }
164 return ResourceBundle.getBundle(root + DEFAULT_LANGUAGE
165 + LANGUAGE_EXTENSION);
166 }
167
168 /***
169 * return the localised message.
170 *
171 * @param key
172 * the message key.
173 * @return the message associated to the key
174 * @since 0.54.17 in case of the main language didn't contain the specified
175 * key, we search in the secondary one
176 */
177 public static String getString(String key) {
178 String value = getNullString(key);
179 if (value != null)
180 return value;
181
182 if (secondaryBundle == null)
183 return '!' + key + '!';
184
185 try {
186 return secondaryBundle.getString(key);
187 } catch (MissingResourceException ex) {
188 return '!' + key + '!';
189 }
190 }
191
192 /***
193 * Return the localised message.
194 *
195 * @param key
196 * the message key.
197 * @return the message associated to the key.<code>null</code> if the key
198 * has not been found.
199 */
200 public static String getNullString(String key) {
201 try {
202 return bundle.getString(key);
203 } catch (MissingResourceException e) {
204 return null;
205 }
206 }
207
208 /***
209 * @param key
210 * the message key.
211 * @param parameters
212 * the parameters to add to resourceBudle.
213 * @return the message associated to the key with replaced parameters.
214 */
215 public static String getString(String key, Object... parameters) {
216
217 return MessageFormat.format(getString(key), parameters);
218 }
219
220 /***
221 * Set the language to use.
222 *
223 * @param languageName
224 * the new language to use
225 */
226 public static void setLanguage(String languageName) {
227 for (Language language : LanguageManager.languages) {
228 if (language.getKey().equalsIgnoreCase(languageName)) {
229 LanguageManager.language = language;
230 break;
231 }
232 }
233 }
234
235 }